安徽网站设计定制,丽水做企业网站的公司,做微博网站好不好,手机网站建设合同#x1f600;前言 本篇博文是关于SpringCloud Gateway–Predicate/断言#xff08;详细介绍#xff09;中#xff0c;希望你能够喜欢 #x1f3e0;个人主页#xff1a;晨犀主页 #x1f9d1;个人简介#xff1a;大家好#xff0c;我是晨犀#xff0c;希望我的文章可以… 前言 本篇博文是关于SpringCloud Gateway–Predicate/断言详细介绍中希望你能够喜欢 个人主页晨犀主页 个人简介大家好我是晨犀希望我的文章可以帮助到大家您的满意是我的动力
欢迎大家这里是CSDN我总结知识的地方欢迎来到我的博客感谢大家的观看 如果文章有什么需要改进的地方还请大佬不吝赐教 先在此感谢啦 文章目录 SpringCloud GatewayCookie Route Predicate需求分析/图解代码实现测试 Header Route Predicate需求分析/图解代码实现测试 Host Route Predicate需求分析/图解代码实现测试 SpringCloud Gateway
Cookie Route Predicate
需求分析/图解
需求: 请求带有cookie 键: user 值: abc才匹配/断言成功
代码实现
参考文档: https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gateway-request-predicates-factories 解读:chocolate 是cookie 名字ch.p 是cookie 的值是按照正则表达式来匹配的 2. 修改e-commerce-gateway-20000 的application.yml routes: #配置路由,可以配置多个路由 ListRouteDefinition routes- id: member_route01 #路由的id, 程序员自己配置,要求唯一#gateway 最终访问的url 是 urluriPath#匹配后提供服务的路由地址: 也可以是外网 http://www.baidu.com#比如 客户端/浏览器请求 url http://localhost:20000/member/get/1#如果根据Path匹配成功 最终访问的url/转发url 就是 urlhttp://localhost:10000/member/get/1#如果匹配失败, 则有gateway返回404信息#疑问: 这里配置的 uri 是固定,在当前这种情况其实可以没有有Eureka Server,后面会使用灵活方式# 配置就会使用到Eureka Server#uri: http://localhost:10000#解读#1. lb: 协议名 , member-service-provider 注册到eureka server 服务名(小写)#2. 默认情况下负载均衡算法是轮询uri: lb://member-service-providerpredicates: #断言,可以有多种形式- Path/member/get/**- Cookieuser, abc- id: member_route02 #路由的id, 程序员自己配置,要求唯一#uri: http://localhost:10000uri: lb://member-service-providerpredicates: #断言,可以有多种形式#这时如果客户端/浏览器 访问gateway 的url http://localhost:20000/member/save#匹配Path成功 最终访问的url 就是 http://localhost:10000/member/save- Path/member/save测试
启动e-commerce-eureka-server-9001 启动member-service-provider-10000/10002 启动e-commerce-gateway-20000 Postman 测试 输入: http://localhost:20000/member/get/1 Header Route Predicate
需求分析/图解
需求: 请求头Header 有X-Request-Id 并且值hello 才匹配/断言成功
代码实现
参考文档: https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gateway-request-predicates-factories 解读: X-Request-Id 是header 的名称, \d 是一个正则表达式
修改e-commerce-gateway-20000 的application.yml routes: #配置路由,可以配置多个路由 ListRouteDefinition routes- id: member_route01 #路由的id, 程序员自己配置,要求唯一#gateway 最终访问的url 是 urluriPath#匹配后提供服务的路由地址: 也可以是外网 http://www.baidu.com#比如 客户端/浏览器请求 url http://localhost:20000/member/get/1#如果根据Path匹配成功 最终访问的url/转发url 就是 urlhttp://localhost:10000/member/get/1#如果匹配失败, 则有gateway返回404信息#疑问: 这里配置的 uri 是固定,在当前这种情况其实可以没有有Eureka Server,后面会使用灵活方式# 配置就会使用到Eureka Server#uri: http://localhost:10000#解读#1. lb: 协议名 , member-service-provider 注册到eureka server 服务名(小写)#2. 默认情况下负载均衡算法是轮询uri: lb://member-service-providerpredicates: #断言,可以有多种形式- Path/member/get/**- HeaderX-Request-Id, hello- id: member_route02 #路由的id, 程序员自己配置,要求唯一#uri: http://localhost:10000uri: lb://member-service-providerpredicates: #断言,可以有多种形式#这时如果客户端/浏览器 访问gateway 的url http://localhost:20000/member/save#匹配Path成功 最终访问的url 就是 http://localhost:10000/member/save- Path/member/save测试
启动e-commerce-eureka-server-9001 启动member-service-provider-10000/10002 启动e-commerce-gateway-20000 Postman 测试 输入: http://localhost:20000/member/get/1 Host Route Predicate
需求分析/图解
需求: 请求Host 是**.my.** 才匹配/断言成功, 比如Host www.my.com
代码实现
参考文档: https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gateway-request-predicates-factories 解读: Host 可以有多个, 使用逗号间隔
修改e-commerce-gateway-20000 的application.yml routes: #配置路由,可以配置多个路由 ListRouteDefinition routes- id: member_route01 #路由的id, 程序员自己配置,要求唯一#gateway 最终访问的url 是 urluriPath#匹配后提供服务的路由地址: 也可以是外网 http://www.baidu.com#比如 客户端/浏览器请求 url http://localhost:20000/member/get/1#如果根据Path匹配成功 最终访问的url/转发url 就是 urlhttp://localhost:10000/member/get/1#如果匹配失败, 则有gateway返回404信息#疑问: 这里配置的 uri 是固定,在当前这种情况其实可以没有有Eureka Server,后面会使用灵活方式# 配置就会使用到Eureka Server#uri: http://localhost:10000#解读#1. lb: 协议名 , member-service-provider 注册到eureka server 服务名(小写)#2. 默认情况下负载均衡算法是轮询uri: lb://member-service-providerpredicates: #断言,可以有多种形式- Path/member/get/**- Host**.hhh.**- id: member_route02 #路由的id, 程序员自己配置,要求唯一#uri: http://localhost:10000uri: lb://member-service-providerpredicates: #断言,可以有多种形式#这时如果客户端/浏览器 访问gateway 的url http://localhost:20000/member/save#匹配Path成功 最终访问的url 就是 http://localhost:10000/member/save- Path/member/save测试
启动e-commerce-eureka-server-9001 启动member-service-provider-10000/10002 启动e-commerce-gateway-20000 Postman 测试 输入: http://localhost:20000/member/get/1 文章到这里就结束了如果有什么疑问的地方请指出诸大佬们一起来评论区一起讨论 希望能和诸大佬们一起努力今后我们一起观看感谢您的阅读 如果帮助到您不妨3连支持一下创造不易您们的支持是我的动力