參考:官方文檔
https://www.cnblogs.com/babycomeon/p/11161073.html
Spring Cloud
版本:Hoxton.SR5
Spring Cloud Gateway
版本:2.2.3.RELEASE
1 簡介
Spring Cloud Gateway
內置了很多Predicate
,用來制定路由匹配規則。
Predicate
來源於Java 8
,是Java 8
中引入的一個函數,Predicate
接受一個輸入參數,返回一個布爾值結果。該接口包含多種默認方法來將Predicate
組合成其他複雜的邏輯(比如:與、或、非)。可以用於接口請求參數校驗、判斷新老數據是否有變化需要進行更新操作。
在Spring Cloud Gateway
中Spring
利用Predicate
的特性實現了各種路由匹配規則,有通過Header
、請求參數等不同的條件來進行作爲條件匹配到對應的路由。
下圖總結了內置的Predicate
:
2 內置Predicate
2.1 匹配請求時間
2.1.1 AfterRoutePredicateFactory
AfterRoutePredicateFactory
可以配置一個時間datetime
,類型是ZonedDateTime
。這個Predicate
會匹配那些在設定的時間之後到達的請求。
ZonedDateTime
是Java 8
中日期時間功能類,用於表示帶時區的日期與時間信息的類,ZonedDateTime
支持通過時區來設置時間,中國的時區是:Asia/Shanghai
。
下面是在application.yml
文件中配置該Predicate
的例子:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2020-06-26T00:00:00+08:00[Asia/Shanghai]
2.1.2 BeforeRoutePredicateFactory
BeforeRoutePredicateFactory
配置一個datetime
時間。這個Predicate
會匹配那些在設定時間之前到達的請求。
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2020-06-26T00:00:00+08:00[Asia/Shanghai]
2.1.3 BetweenRoutePredicateFactory
BetweenRoutePredicateFactory
可以配置一個時間段。這個Predicate
會匹配在時間段之間到達的請求。
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2020-06-26T00:00:00+08:00[Asia/Shanghai], 2020-06-27T00:00:00+08:00[Asia/Shanghai]
2.2 匹配Cookie
2.2.1 CookieRoutePredicateFactory
CookieRoutePredicateFactory
接受兩個參數,一個是Cookie
的名稱name
,一個是正則表達式regexp
。該Predicate
將會根據name
獲取請求中對應的Cookie
值,並與regexp
正則表達式進行匹配,匹配上了就會路由該請求。
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=sessionId, c52e52258f86b61ab25fa0268be34f08
2.3 匹配Header
2.3.1 HeaderRoutePredicateFactory
HeaderRoutePredicateFactory
接受兩個參數,一個是Header
中的屬性名name
,一個是正則表達式regexp
。該Predicate
將會根據name
獲取請求的Header
中對應的屬性名,並與regexp
正則表達式進行匹配,匹配上了就會路由該請求。
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
2.4 匹配Host
2.4.1 HostRoutePredicateFactory
HostRoutePredicateFactory
接收一組參數,一組域名模板。這個模板是一個 Ant
風格的模板,用.
號作爲分隔符,組與組之間用,
分隔。它將會去匹配請求Header
中的Host
屬性值。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
如上的配置,www.somehost.org
、 beta.somehost.org
、 www.anotherhost.org
將會匹配成功。
2.5 匹配請求方法
2.5.1 MethodRoutePredicateFactory
MethodRoutePredicateFactory
接受一組請求方法,包括GET
、POST
、PUT
、DELETE
等。該Predicate
將會匹配被指定的請求方法的請求。
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
2.6 匹配請求路徑
2.6.1 PathRoutePredicateFactory
PathRoutePredicateFactory
接受一組參數,一個path
列表。該Predicate
匹配列表中的path
。
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
/red/1
、/red/blue
、/blue/green
將會被匹配。
2.7 匹配請求參數
2.7.1 QueryRoutePredicateFactory
QueryRoutePredicateFactory
接受兩個參數,一個是參數名name
,一個是正則表達式regexp
。該Predicate
將會匹配指定名稱的參數值。
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
也可以只指定參數名。如上,這樣請求中有參數green
的,都將會匹配成功。
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green, pu.
如上,將會匹配請求中的參數green
的值,滿足pu.
表達式。
2.8 匹配請求者的ip地址
2.8.1 RemoteAddrRoutePredicateFactory
RemoteAddrRoutePredicateFactory
接受一組地址,地址是cidr
符號(IPv4
或IPv6
)字符串,例如 192.168.0.1/16
(其中 192.168.0.1
是 IP
地址,16
是子網掩碼)。
spring:
cloud:
gateway:
routes:
- id: remote_addr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
如上的配置,192.168.1.10
的IP
地址將會被匹配。
2.9 匹配路由權重(Weight)
2.9.1 WeightRoutePredicateFactory
WeightRoutePredicateFactory
接受兩個參數,一個是分組名,一個是權重(int
值)。
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weightHigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightLow.org
predicates:
- Weight=group2, 2
基於以上配置,將會有80%
的請求路由到https://weightHigh.org
,20%
的請求路由到https://weightLow.org
。