共计 1925 个字符,预计需要花费 5 分钟才能阅读完成。
1、测试多种路由规则匹配优先级
1.1、编写综合路由规则
spring.application.name=gateway-java-api | |
server.port=50010 | |
spring.cloud.gateway.routes[0].id=weight_route1 | |
spring.cloud.gateway.routes[0].uri=http://localhost:50020 | |
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 | |
spring.cloud.gateway.routes[1].id=weight_route2 | |
spring.cloud.gateway.routes[1].uri=http://localhost:50021 | |
spring.cloud.gateway.routes[1].predicates[0]=Path=/hello | |
spring.cloud.gateway.routes[1].predicates[1]=Weight=Weight,6 | |
spring.cloud.gateway.routes[2].id=path_route1 | |
spring.cloud.gateway.routes[2].uri=http://localhost:50022 | |
spring.cloud.gateway.routes[2].predicates[0]=Path=/test | |
spring.cloud.gateway.routes[3].id=path_route2 | |
spring.cloud.gateway.routes[3].uri=http://localhost:50023 | |
spring.cloud.gateway.routes[3].predicates[0]=Path=/test |
1.2、修改服务提供者代码
public class HelloController { ("${provider.name}") | |
private String name; | |
"/hello") | (|
public String hello("name",required = false) String name){ (value = if (StringUtils.isEmpty(name)){name=this.name; | |
} | |
return name; | |
} | |
"/hello2") | (|
public String hello2(){return name; | |
} | |
"/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 小的路由。即,当一个请求满足多个路由谓词条件时,请求只会被首个成功匹配的路由转发
正文完
星哥玩云-微信公众号
