> 文档中心 > Gateway(三)Spring Cloud Gateway内置各类型Predicate(断言)使用说明

Gateway(三)Spring Cloud Gateway内置各类型Predicate(断言)使用说明

目录

  • 1、Path Route Predicate
  • 2、DateTimePredicate(匹配请求时间)
    • 2.1 After Route Predicate(匹配时间后的请求)
    • 2.2 Before Route Predicate(匹配时间前的请求)
    • 2.3 Between Route Predicate(匹配时间之间的请求)
  • 3、Cookie Route Predicate
  • 4、Header Route Predicate
  • 5、Host Route Predicate
  • 6、Method Route Predicate
  • 7、Query Route Predicate
  • 8、RemoteAddr Route Predicate
  • 9、Weight Route Predicate

《SpringCloudAlibaba+Nacos整合Gateway网关》
《SpringCloudGateway结合Sentienl实现网关限流机制》
《Spring Cloud Gateway过滤器(GatewayFilter)工厂》

Spring Cloud Gateway 包含许多内置的路由断言工厂。所有这些断言都匹配 HTTP 请求的不同属性。您可以将多个路由断言工厂与逻辑and语句结合起来。

1、Path Route Predicate

Path是最常见的断言请求,匹配指定路径下的请求,可以是具体的请求,也可使用/**表示匹配所有子级请求,配置如下。

spring:  cloud:    gateway:      routes:      - id: after_route uri: https://example.org predicates: - Path=/user/** - Path=/wage/list

配置中匹配了以/user开头或者URL为/wage/list的请求,如果是其他URL的请求进入系统,会出现错误。

2、DateTimePredicate(匹配请求时间)

2.1 After Route Predicate(匹配时间后的请求)

After Route Predicate可以匹配ZonedDateTime类型的时间,表示:匹配在指定日期时间之后发生的请求,配置如下:

spring:  cloud:    gateway:      routes:      - id: after_route uri: https://example.org predicates: - Path=/user/** - After=2022-02-26T13:00:00+08:00[Asia/Shanghai]

配置中匹配了2022-02-26 13:00:00后的请求,如果是在指定时间之前进入系统的请求,会出现错误。

2.2 Before Route Predicate(匹配时间前的请求)

Before Route Predicate可以匹配ZonedDateTime类型的时间,表示:匹配在指定日期时间之前发生的请求,配置如下:

spring:  cloud:    gateway:      routes:      - id: after_route uri: https://example.org predicates: - Path=/user/** - Before=2022-02-26T13:00:00+08:00[Asia/Shanghai]

配置中匹配了2022-02-26 13:00:00之前的请求,如果是在指定时间之后进入系统的请求,会出现错误。

2.3 Between Route Predicate(匹配时间之间的请求)

Between Route Predicate可以匹配ZonedDateTime类型的时间,由两个ZonedDateTime参数组成,第一个参数为开始时间,第二参数为结束时间,表示:匹配在指定的开始时间结束时间之内发生的请求,配置如下:

spring:  cloud:    gateway:      routes:      - id: after_route uri: https://example.org predicates: - Path=/user/** - Between=2022-02-26T13:00:00+08:00[Asia/Shanghai],2022-02-27T13:00:00+08:00[Asia/Shanghai]

配置中匹配了2022-02-26 13:00:002022-02-27 13:00:00之内时间段的请求,如果是在指定时间段外的进入系统的请求,会出现错误。

3、Cookie Route Predicate

CookieRoutePredicate由两个参数组成,第一个参数为cookie的Key,第二参数为cookie的Value,表示:匹配指定名称且其值与正则表达式匹配的cookie的请求,配置如下:

spring:  cloud:    gateway:      routes:      - id: cookie_route uri: https://example.org predicates: - Path=/user/** - Cookie=cokieName, \d+

配置中匹配了cookie的Key为cookieName,值为满足\d+的正则表达式请求,如果满足cookieName不满足\d+的请求,会出现错误。
Gateway(三)Spring Cloud Gateway内置各类型Predicate(断言)使用说明

4、Header Route Predicate

HeaderRoutePredicate由两个参数组成,第一个参数为Header名称,第二参数为Header的Value值,表示:匹配指定名称且其值与正则表达式匹配的Header的请求,配置如下:

spring:  cloud:    gateway:      routes:      - id: cookie_route uri: https://example.org predicates: - Path=/user/** - Header=headerName, \d+

配置中匹配了Header的名称为headerName,值为满足\d+的正则表达式请求,如果满足headerName不满足\d+的请求,会出现错误。
Gateway(三)Spring Cloud Gateway内置各类型Predicate(断言)使用说明

5、Host Route Predicate

HostRoutePredicate参数为请求的Host地址,多个参数使用逗号分割,设置的Host地址可以使用**表示通配符,配置如下:

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

配置中匹配的Host,可以匹配以somehost.org或者anotherhost.org结尾的Host地址,其他Host地址访问会出现错误。
Gateway(三)Spring Cloud Gateway内置各类型Predicate(断言)使用说明在这里插入图片描述

6、Method Route Predicate

MethodRoutePredicate由一个或多个HTTP Method组成,比如:POST、PUT、GET、DELETE,配置如下:

spring:  cloud:    gateway:      routes:      - id: cookie_route uri: https://example.org predicates: - Path=/user/** - Method=GET,POST

配置中匹配了HTTP Method的类型为GET和POST,如果是其他类型的HTTP Method,会出现错误。
Gateway(三)Spring Cloud Gateway内置各类型Predicate(断言)使用说明
在这里插入图片描述

7、Query Route Predicate

QueryRoutePredicate由两个参数组成,第一个参数为参数名称,第二参数为参数的值(满足正则即可),表示:匹配指定名称且其值与正则表达式匹配的带参的请求,配置如下:

spring:  cloud:    gateway:      routes:      - id: cookie_route uri: https://example.org predicates: - Path=/user/** - Query=name,\d+

配置中匹配了参数名称叫做name,值满足\d+的请求,如果不满足\d+,会出现错误。
Gateway(三)Spring Cloud Gateway内置各类型Predicate(断言)使用说明

8、RemoteAddr Route Predicate

RemoteAddrRoutePredicate的参数由CIDR 表示法(IPv4 或 IPv6)字符串组成,配置如下:

spring:  cloud:    gateway:      routes:      - id: cookie_route uri: https://example.org predicates: - Path=/user/**- RemoteAddr=192.168.1.1/24

配置中可以匹配IP为192.168.1.100的值,如果不满足192.168.1.1/24的IP规则,会出现错误。

9、Weight Route Predicate

WeightAddrRoutePredicategroupweight(权重数值)组成,表示将相同的请求根据权重跳转到不同的uri地址,要求group的名称必须一致,配置如下:

spring:  cloud:    gateway:      routes:      - id: weight_high uri: https://weighthigh.org predicates: - Weight=groupName, 8      - id: weight_low uri: https://weightlow.org predicates: - Weight=groupName, 2

该路由会将约 80% 的流量转发到weighthigh.org,将约 20% 的流量转发到weightlow.org