[图片上传失败...(image-709918-1615378255597)]
客户端发送请求到gateway,由gateway handler mapping进行路由,发送到gateway web handler。这个handler处理请求相关的filter链。filter分“pre”和“post”两种处理逻辑。
配置
配置predicates和filters有两种方式:简写和全参数展开。
简写方式
公式:name=name,regexp,例如
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Cookie=mycookie,mycookievalue
全参数展开方式
如果yaml文件标准一样进行配置,通常会有name和args两个键,args是一个map的键值对组配置predicate或者filter。如下:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
路由工厂
每种路由的判断依据都是根据Http请求的不同属性。
- 匹配某时间之后的请求
- 匹配某个时间之前的请求
- 匹配两个时间之间的请求
- 匹配请求包含指定Cookie
- 匹配请求包含指定Header
- 匹配特定Host
- 匹配请求方法
- 匹配请求路径
- 匹配查询参数
- 匹配远程地址
- 根据权重路由
匹配某时间之后的请求
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
**
匹配某个时间之前的请求
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
匹配两个时间之间的请求
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
匹配请求包含指定Cookie
value为正则表达式
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
匹配请求包含指定Header
value为正则表达式
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
匹配特定Host
每个Host为Ant-style格式,以.号分割。
Ant-style的匹配原则
Apache Ant样式的路径有三种通配符匹配方法(在下面的表格中列出)
路径 | 描述 |
---|---|
? | 匹配任何单字符 |
* | 匹配0或者任意数量的字符 |
** | 匹配0或者更多的目录 |
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
匹配请求方法
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
匹配请求路径
参数包括一个Spring PathMatcherpatterns和一个可选的matchOptionalTrailingSeparator分隔符
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
上面的规则可以匹配诸如/red/1/red/blue/blue/green等路径。
抽取URI中的模板变量(如segment),作为键值对存储在ServerWebExchange.getAttributes(),key为ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE。这些值可以被GatewayFilter factories获取到。有个工具方法可以更简单地获取到这些值。如下
Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
String segment = uriVariables.get("segment");
匹配查询参数
value为正则表达式
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
匹配远程地址
格式为CIDR-notation,例如192.168.0.1/16,192.168.0.1是ip,16是子网掩码。
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
根据权重路由
配置两个参数: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
上面的配置会让80%的请求发送到weighthigh.org,20%的请求发送到weightlow.org。
网关过滤器工厂
AddRequestHeader
增加请求头参数,可以使用URI变量
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: https://example.org
predicates:
- Path=/red/{segment}
filters:
- AddRequestHeader=X-Request-Red, Blue-{segment}
AddRequestParameter
增加查询参数,可以使用URI变量
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- AddRequestParameter=foo, bar-{segment}
AddResponseHeader
增加响应头中参数,可以使用URI变量
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- AddResponseHeader=foo, bar-{segment}
DedupeResponseHeader
去掉重复的响应头参数
spring:
cloud:
gateway:
routes:
- id: dedupe_response_header_route
uri: https://example.org
filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
上面会去掉重复的Access-Control-Allow-Credentials,Access-Control-Allow-Origin参数值。
可以设置strategy值修改默认删除策略,默认为RETAIN_FIRST,即保留第一个。其他有RETAIN_LAST,RETAIN_UNIQUE。
Spring Cloud CircuitBreaker GatewayFilter Factory
轻量的断路由
The MapRequestHeader GatewayFilter Factory
spring:
cloud:
gateway:
routes:
- id: map_request_header_route
uri: https://example.org
filters:
- MapRequestHeader=Blue, X-Request-Red
可以替换头部的参数的名称,如将Blue:abc替换为X-Request-Red:abc
The PrefixPath GatewayFilter Factory
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- PrefixPath=/mypath
增加前缀,例如/hello将被发送到/mypath/hello
The RequestRateLimiter GatewayFilter Factory
通过实现RateLimiter接口配置限流规则,可通过keyResolver参数设置具体的限流的key。现在默认的是PrincipalNameKeyResolver,调用的是ServerWebExchange
中的Principal.getName()。
如果key解析后为空,请求会被拒绝,可以通过配置下面参数进行自定义策略
spring.cloud.gateway.filter.request-rate-limiter.deny-empty-key
(true
or false
) spring.cloud.gateway.filter.request-rate-limiter.empty-key-status-code
The Redis RateLimiter
redis限流,使用的是令牌桶算法。
redis-rate-limiter.replenishRate
每秒多少个请求,也是令牌入桶的频率。
redis-rate-limiter.burstCapacity
峰值请求。
redis-rate-limiter.requestedTokens
每个请求消耗的令牌数,默认1.
如果想保持稳定的请求频率,可以设置replenishRate
和burstCapacity
为相同值,如果有突发的大量请求,则需要设置burstCapacity
比replenishRate
大。
如果想设置每分钟1个请求,可以通过以下配置实现
replenishRate=1
requestedTokens=60
burstCapacity=60
spring:
cloud:
gateway:
routes:
- id: requestratelimiter_route
uri: https://example.org
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
redis-rate-limiter.requestedTokens: 1
@Bean
KeyResolver userKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
}
也可以实现自己的RateLimiter
和KeyResolver
spring:
cloud:
gateway:
routes:
- id: requestratelimiter_route
uri: https://example.org
filters:
- name: RequestRateLimiter
args:
rate-limiter: "#{@myRateLimiter}"
key-resolver: "#{@userKeyResolver}"
The RedirectTo GatewayFilter Factory
配置参数status
and url
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- RedirectTo=302, https://acme.org
The RemoveRequestHeader GatewayFilter Factory
删除指定的请求头参数
spring:
cloud:
gateway:
routes:
- id: removerequestheader_route
uri: https://example.org
filters:
- RemoveRequestHeader=X-Request-Foo
RemoveResponseHeader GatewayFilter Factory
删除指定响应头参数
spring:
cloud:
gateway:
routes:
- id: removeresponseheader_route
uri: https://example.org
filters:
- RemoveResponseHeader=X-Response-Foo
The RemoveRequestParameter GatewayFilter Factory
删除请求参数
spring:
cloud:
gateway:
routes:
- id: removerequestparameter_route
uri: https://example.org
filters:
- RemoveRequestParameter=red
The RewritePath GatewayFilter Factory
重写路径
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: https://example.org
predicates:
- Path=/red/**
filters:
- RewritePath=/red(?<segment>/?.*), $\{segment}
上面配置会把/red/blue变为/blue
RewriteLocationResponseHeader GatewayFilter Factory
spring:
cloud:
gateway:
routes:
- id: rewritelocationresponseheader_route
uri: http://example.org
filters:
- RewriteLocationResponseHeader=AS_IN_REQUEST, Location, ,
The SetPath GatewayFilter Factory
spring:
cloud:
gateway:
routes:
- id: setpath_route
uri: https://example.org
predicates:
- Path=/red/{segment}
filters:
- SetPath=/{segment}
/red/blue会变为/blue
The SetRequestHeader GatewayFilter Factory
替换请求头参数值
spring:
cloud:
gateway:
routes:
- id: setrequestheader_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- SetRequestHeader=foo, bar-{segment}
The SetResponseHeader GatewayFilter Factory
替换响应头参数值
spring:
cloud:
gateway:
routes:
- id: setresponseheader_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- SetResponseHeader=foo, bar-{segment}
The SetStatus GatewayFilter Factory
设置状态码
spring:
cloud:
gateway:
routes:
- id: setstatusstring_route
uri: https://example.org
filters:
- SetStatus=BAD_REQUEST
- id: setstatusint_route
uri: https://example.org
filters:
- SetStatus=401
上面两种配置都会设置为401
The StripPrefix GatewayFilter Factory
删除前缀
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
上面的配置将使/name/blue/red变为nameservice/red。
参考
https://cloud.spring.io/spring-cloud-gateway/reference/html/#the-path-route-predicate-factory
网友评论