GateWay系列之路由规则

Spring Cloud GateWay 帮我们内置了很多 Predicates功能,实现了各种路由匹配规
则(通过 Header、请求参数等作为条件)匹配到对应的路由

1、时间点后匹配

spring:
 cloud:
  gateway:
   routes:
    - id: after_route
      uri: https://example.org
      predicates:
       - After=2022-02-20T17:42:47.789-07:00[America/Denver]

2、时间点前匹配

spring:
 cloud:
  gateway:
   routes:
    - id: before_route
      uri: https://example.org
      predicates:
       - Before=2022-02-20T17:42:47.789-07:00[America/Denver]

3、时间区间匹配

spring:
 cloud:
  gateway:
   routes:
    - id: between_route
      uri: https://example.org
      predicates:
       - Between=2022-01-20T17:42:47.789-07:00[America/Denver],
2022-01-21T17:42:47.789-07:00[America/Denver]

4、指定Cookie正则匹配指定值

spring:
 cloud:
  gateway:
   routes:
    - id: cookie_route
      uri: https://example.org
      predicates:
       - Cookie=cookie,china

5、指定Header正则匹配指定值

spring:
 cloud:
  gateway:
   routes:
    - id: header_route
      uri: https://example.org
      predicates:
       - Header=X-Request-Id

6、请求Host匹配指定值

spring:
 cloud:
  gateway:
   routes:
    - id: host_route
      uri: https://example.org
      predicates:
       - Host=**.somehost.org,**.anotherhost.org

7、请求Method匹配指定请求方式

spring:
 cloud:
  gateway:
   routes:
    - id: method_route
      uri: https://example.org
      predicates:
       - Method=GET,POST

8、请求路径正则匹配

spring:
 cloud:
  gateway:
   routes:
    - id: path_route
      uri: https://example.org
      predicates:
       - Path=/red/{segment},/blue/{segment}

9、请求包含某参数

spring:
 cloud:
  gateway:
   routes:
    - id: query_route
      uri: https://example.org
      predicates:
       - Query=green

10、请求包含某参数并且参数值匹配正则表达式

spring:
 cloud:
  gateway:
   routes:
    - id: query_route
      uri: https://example.org
      predicates:
       - Query=red, gree.

11、远程地址匹配

spring:
 cloud:
  gateway:
  routes:
   - id: remoteaddr_route
     uri: https://example.org
     predicates:
      - RemoteAddr=192.168.1.1/24

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值