0
点赞
收藏
分享

微信扫一扫

Gateway配置断言路由入门demo

网关项目配置

#设置路由:路由id、路由到微服务的uri、断言
routes:
- id: order_route #路由ID,全局唯一,建议配合服务名
# uri参数可以直接去指定ip端口,也可以指定负载均衡lb开头
#uri: http://localhost:8020 #目标微服务的请求地址和端口
uri: lb://mall-order #lb 整合负载均衡器ribbon,loadbalancer
predicates:
# #Path路径匹配
- Path=/order/**

- id: user_route
uri: lb://mall-user #lb 整合负载均衡器ribbon,loadbalancer
predicates:
- Path=/user/**

说明:
rotes就是设置路由

- id: order_route  #路由ID,全局唯一,建议配合服务名
# uri参数可以直接去指定ip端口,也可以指定负载均衡lb开头
#uri: http://localhost:8020 #目标微服务的请求地址和端口
uri: lb://mall-order #lb 整合负载均衡器ribbon,loadbalancer
predicates:
# #Path路径匹配
- Path=/order/**

id:就是路由ID,全局唯一,
uri:就是路由到目标微服务地址和端口,可以配置成 ​​​http://localhost:8020​​​ ,这样就会自动找对应的地址,
也可以配置服务名lb://mall-order的意思是负载均衡调用mall-order服务
predicates:的意思就是断言, - Path=/order/** 的意思是url匹配了/order/**就会走uri配置的地址

代码地址

代码出自图灵学院,我在这个代码基础上改了改

​​https://gitee.com/zjj19941/ZJJ_Neaten5.10/tree/master/ZJJ_Gateway/demo02​​ ​

测试

启动下面三个服务
MallOrderApplication
MallGatewayApplication
MallUserApplication

发起get请求: 访问下面两个地址 ,都能被正确的路由到指定服务的指定方法上,说明localhost:8888 是 gateway项目的ip端口

​​http://localhost:8888/order/findOrderByUserId/1​​​ 看到下面控制台打了日志,说明访问到了目标方法
Gateway配置断言路由入门demo_gateway

​​http://localhost:8888/user/findOrderByUserId/1​​​ 看到下面控制台打了日志,说明访问到了目标方法
Gateway配置断言路由入门demo_http_02


举报

相关推荐

0 条评论