0%

GateWay断言

GateWay断言

断言Predicate

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

After断言

Loaded RoutePredicateFactory [After]

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

1
2
3
4
5
6
7
8
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匹配在当前日期时间之前发生的请求

1
2
3
4
5
6
7
8
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匹配在两个日期之间发生的请求

1
2
3
4
5
6
7
8
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以及值的正则表达式进行匹配

1
2
3
4
5
6
7
8
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以及值的正则表达式进行匹配

1
2
3
4
5
6
7
8
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模式匹配

1
2
3
4
5
6
7
8
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org

Method断言

Loaded RoutePredicateFactory [Method]

Method 匹配HTTP方法

1
2
3
4
5
6
7
8
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST

Path断言

Loaded RoutePredicateFactory [Path]

Path 进行路径匹配

1
2
3
4
5
6
7
8
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Path=/foo/{segment},/bar/{segment}

Query断言

Loaded RoutePredicateFactory [Query]

Query 查询参数匹配

1
2
3
4
5
6
7
8
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 远程地址匹配

1
2
3
4
5
6
7
8
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两个参数,权重是按照组进行计算的

1
2
3
4
5
6
7
8
9
10
11
12
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]

欢迎关注我的其它发布渠道