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
https://zhhll.icu/2021/框架/微服务/springcloud/网关/GateWay/2.GateWay断言/
网友评论