阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

测试多种路由规则匹配优先级

119次阅读
没有评论

共计 1925 个字符,预计需要花费 5 分钟才能阅读完成。

1、测试多种路由规则匹配优先级

1.1、编写综合路由规则

spring.application.name=gateway-java-api
server.port=50010
#id: 自定义路由 ID
spring.cloud.gateway.routes[0].id=weight_route1
#uri: 目标服务地址
spring.cloud.gateway.routes[0].uri=http://localhost:50020
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[0].predicates[0]=Path=/hello
spring.cloud.gateway.routes[0].predicates[1]=Weight=Weight,4
spring.cloud.gateway.routes[0].predicates[2]=Query=name
#id: 自定义路由 ID
spring.cloud.gateway.routes[1].id=weight_route2
#uri: 目标服务地址
spring.cloud.gateway.routes[1].uri=http://localhost:50021
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[1].predicates[0]=Path=/hello
spring.cloud.gateway.routes[1].predicates[1]=Weight=Weight,6
#id: 自定义路由 ID
spring.cloud.gateway.routes[2].id=path_route1
#uri: 目标服务地址
spring.cloud.gateway.routes[2].uri=http://localhost:50022
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[2].predicates[0]=Path=/test
#id: 自定义路由 ID
spring.cloud.gateway.routes[3].id=path_route2
#uri: 目标服务地址
spring.cloud.gateway.routes[3].uri=http://localhost:50023
#predicates: 路由条件。Predicate 根据输入参数返回一个布尔值。其包含多种默认方法来将 Predicate 组合成复杂的路由逻辑
spring.cloud.gateway.routes[3].predicates[0]=Path=/test

1.2、修改服务提供者代码

@RestController public class HelloController {@Value("${provider.name}") private String name; @GetMapping("/hello") public String hello(@RequestParam(value = "name",required = false) String name){if (StringUtils.isEmpty(name)){name=this.name; } return name; } @GetMapping("/hello2") public String hello2(){return name; } @GetMapping("/test") public String test(){return name; } }

1.3、启动 4 个服务提供者

端口号分别是 50020,50021,50022,50023

名称分别是 provider-1,provider-2,provider-3,provider-4

1.4、测试路由规则匹配情况

1、访问 http://localhost:50010/hello?name=liu

测试多种路由规则匹配优先级

2、访问 http://localhost:50010/hello

测试多种路由规则匹配优先级

3、访问 http://localhost:50010/test

测试多种路由规则匹配优先级

总结:

  • 根据权重匹配:同一组路由的优先级由权重决定

  • 根据路由 id 值匹配:不同组路由的优先级根据路由 ID 来计算。优先匹配 ID 小的路由。即,当一个请求满足多个路由谓词条件时,请求只会被首个成功匹配的路由转发

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-06-06发表,共计1925字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中