Spring Cloud全解析:网关之GateWay断言



GateWay断言

断言Predicate

gateWay网关中提供了多种断言方式

After断言

Loaded RoutePredicateFactory [After]

After匹配在当前日期时间之后发生的请求

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2021-09-06T16:02:25.738+08:00[Asia/Shanghai]
Before断言

Loaded RoutePredicateFactory [Before]

Before匹配在当前日期时间之前发生的请求

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2021-09-06T16:02:25.738+08:00[Asia/Shanghai]
Between断言

Loaded RoutePredicateFactory [Between]

Between匹配在两个日期之间发生的请求

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2021-09-06T16:02:25.738+08:00[Asia/Shanghai], 2021-09-07T16:02:25.738+08:00[Asia/Shanghai]
Cookie断言

Loaded RoutePredicateFactory [Cookie]

Cookie会根据cookie的name以及值的正则表达式进行匹配

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=userName, zhangsan  # userName为Cookie的name,zhangsan为cookie的值的正则表达式
Header断言

Loaded RoutePredicateFactory [Header]

Header是匹配请求头的name以及值的正则表达式进行匹配

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+  #X-Request-Id是请求头的name,\d+为请求头所对应的值的正则表达式
Host断言

Loaded RoutePredicateFactory [Host]

Host进行域名匹配,Ant模式匹配

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

Loaded RoutePredicateFactory [Method]

Method 匹配HTTP方法

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

Loaded RoutePredicateFactory [Path]

Path 进行路径匹配

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Path=/foo/{segment},/bar/{segment}
Query断言

Loaded RoutePredicateFactory [Query]

Query 查询参数匹配

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=foo, ba.  #如果查询参数为foo并且其值符合ba.正则表达式
ReadBodyPredicateFactory断言

Loaded RoutePredicateFactory [ReadBodyPredicateFactory]

RemoteAddr断言

Loaded RoutePredicateFactory [RemoteAddr]

RemoteAddr 远程地址匹配

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

Loaded RoutePredicateFactory [Weight]

Weight 权重匹配,接收group和weight两个参数,权重是按照组进行计算的

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2     #group1是组名, 会有80%的请求访问 https://weighthigh.org,20%的请求访问https://weightlow.org
CloudFoundryRouteService断言

Loaded RoutePredicateFactory [CloudFoundryRouteService]

配置方式

gateway配置断言有两种方式

以Cookie断言为例,Cookie断言的配置类如下

public static class Config {

   @NotEmpty
   private String name;

   @NotEmpty
   private String regexp;

   public String getName() {
      return name;
   }

   public Config setName(String name) {
      this.name = name;
      return this;
   }

   public String getRegexp() {
      return regexp;
   }

   public Config setRegexp(String regexp) {
      this.regexp = regexp;
      return this;
   }

}
  • 简短配置

    断言名称和参数之间是=,多个参数之间,相隔,按照定义的顺序依次赋值

    spring:
      cloud:
        gateway:
          routes:
          - id: after_route
            uri: https://example.org
            predicates:
            - Cookie=mycookie,mycookievalue
    
  • 全面扩展参数

    每个参数都使用 字段名:字段值 表示

    spring:
      cloud:
        gateway:
          routes:
          - id: after_route
            uri: https://example.org
            predicates:
            - name: Cookie
              args:
                name: mycookie
                regexp: mycookievalue
    

参考文献

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾光师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值