[圖片上傳失敗...(image-709918-1615378255597)]
客戶端發(fā)送請求到gateway茵汰,由gateway handler mapping進行路由,發(fā)送到gateway web handler缀匕。這個handler處理請求相關(guān)的filter鏈。filter分“pre”和“post”兩種處理邏輯。
配置
配置predicates和filters有兩種方式:簡寫和全參數(shù)展開贺嫂。
簡寫方式
公式:name=name,regexp雁乡,例如
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Cookie=mycookie,mycookievalue
全參數(shù)展開方式
如果yaml文件標(biāo)準(zhǔn)一樣進行配置第喳,通常會有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
路由工廠
每種路由的判斷依據(jù)都是根據(jù)Http請求的不同屬性曲饱。
- 匹配某時間之后的請求
- 匹配某個時間之前的請求
- 匹配兩個時間之間的請求
- 匹配請求包含指定Cookie
- 匹配請求包含指定Header
- 匹配特定Host
- 匹配請求方法
- 匹配請求路徑
- 匹配查詢參數(shù)
- 匹配遠程地址
- 根據(jù)權(quán)重路由
匹配某時間之后的請求
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或者任意數(shù)量的字符 |
** | 匹配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
匹配請求路徑
參數(shù)包括一個Spring PathMatcherpatterns和一個可選的matchOptionalTrailingSeparator分隔符
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
上面的規(guī)則可以匹配諸如/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");
匹配查詢參數(shù)
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是子網(wǎng)掩碼象对。
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
根據(jù)權(quán)重路由
配置兩個參數(shù):group和weight(數(shù)值)。
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%的請求發(fā)送到weighthigh.org宴抚,20%的請求發(fā)送到weightlow.org勒魔。
網(wǎng)關(guān)過濾器工廠
AddRequestHeader
增加請求頭參數(shù)甫煞,可以使用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
增加查詢參數(shù),可以使用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
增加響應(yīng)頭中參數(shù)沥邻,可以使用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
去掉重復(fù)的響應(yīng)頭參數(shù)
spring:
cloud:
gateway:
routes:
- id: dedupe_response_header_route
uri: https://example.org
filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
上面會去掉重復(fù)的Access-Control-Allow-Credentials危虱,Access-Control-Allow-Origin參數(shù)值。
可以設(shè)置strategy值修改默認(rèn)刪除策略唐全,默認(rèn)為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
可以替換頭部的參數(shù)的名稱,如將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將被發(fā)送到/mypath/hello
The RequestRateLimiter GatewayFilter Factory
通過實現(xiàn)RateLimiter接口配置限流規(guī)則剪勿,可通過keyResolver參數(shù)設(shè)置具體的限流的key。現(xiàn)在默認(rèn)的是PrincipalNameKeyResolver方庭,調(diào)用的是ServerWebExchange
中的Principal.getName()厕吉。
如果key解析后為空,請求會被拒絕械念,可以通過配置下面參數(shù)進行自定義策略
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
每個請求消耗的令牌數(shù)希停,默認(rèn)1.
如果想保持穩(wěn)定的請求頻率烁巫,可以設(shè)置replenishRate
和burstCapacity
為相同值,如果有突發(fā)的大量請求宠能,則需要設(shè)置burstCapacity
比replenishRate
大亚隙。
如果想設(shè)置每分鐘1個請求,可以通過以下配置實現(xiàn)
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"));
}
也可以實現(xiàn)自己的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
配置參數(shù)status
and url
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- RedirectTo=302, https://acme.org
The RemoveRequestHeader GatewayFilter Factory
刪除指定的請求頭參數(shù)
spring:
cloud:
gateway:
routes:
- id: removerequestheader_route
uri: https://example.org
filters:
- RemoveRequestHeader=X-Request-Foo
RemoveResponseHeader GatewayFilter Factory
刪除指定響應(yīng)頭參數(shù)
spring:
cloud:
gateway:
routes:
- id: removeresponseheader_route
uri: https://example.org
filters:
- RemoveResponseHeader=X-Response-Foo
The RemoveRequestParameter GatewayFilter Factory
刪除請求參數(shù)
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變?yōu)?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會變?yōu)?blue
The SetRequestHeader GatewayFilter Factory
替換請求頭參數(shù)值
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
替換響應(yīng)頭參數(shù)值
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
設(shè)置狀態(tài)碼
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
上面兩種配置都會設(shè)置為401
The StripPrefix GatewayFilter Factory
刪除前綴
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: https://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
上面的配置將使/name/blue/red變?yōu)閚ameservice/red违崇。
參考
https://cloud.spring.io/spring-cloud-gateway/reference/html/#the-path-route-predicate-factory