一、斷言(Predicate)的意義
斷言是路由配置的一部分,當(dāng)斷言條件滿足,即執(zhí)行Filter的邏輯,如下例所示
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: https://example.org
predicates:
- Path=/red/{segment}
filters:
- AddRequestHeader=X-Request-Red, Blue-{segment}
當(dāng)請求路徑滿足條件/red/,即添加頭信息:X-Request-Red,value為Blue-{segment},segment是路徑里面帶的信息。
gateWay的主要功能之一是轉(zhuǎn)發(fā)請求,轉(zhuǎn)發(fā)規(guī)則的定義主要包含三個部分
Route(路由) | 路由是網(wǎng)關(guān)的基本單元,由ID、URI、一組Predicate、一組Filter組成,根據(jù)Predicate進行匹配轉(zhuǎn)發(fā)。 |
Predicate(謂語、斷言) | 路由轉(zhuǎn)發(fā)的判斷條件,目前SpringCloud Gateway支持多種方式,常見如:Path、Query、Method、Header等,寫法必須遵循 key=vlue的形式 |
Filter(過濾器) | 過濾器是路由轉(zhuǎn)發(fā)請求時所經(jīng)過的過濾邏輯,可用于修改請求、響應(yīng)內(nèi)容 |
其中Route和Predicate必須同時申明
以下內(nèi)容,來自于SpringCloud Gateway官網(wǎng),經(jīng)過整理得來。
二、配置路由斷言
注意:以下的配置,只寫了routes配置的predicates的部分,實際上,predicates單獨用沒有意義,一般要配置filter來用。如上面的添加頭信息的示例。
1.1 簡寫配置
由過濾器名稱,后跟等號,后跟逗號分割的參數(shù)值
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Cookie=mycookie,mycookievalue
1.2 展開的寫法
spring:
cloud:
gateway:
routes:
- id: test_cookie
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
以上兩個寫法,效果是一樣的。
問題1:展開的predicates的展開寫法為什么是這么寫?
原因是,這個配置對應(yīng)的配置類是這么寫的
問題2:name:Cookie是怎么來的?
根據(jù)類名來的,CookieRoutePredicateFactory,取的Cookie前綴,還有很多斷言工廠,如下:
問題3:為啥args下面是name和regexp?
三、路由斷言工廠示例
Spring Cloud Gateway 路由匹配作為Spring WebFlux HandlerMapping 基礎(chǔ)設(shè)施的一部分。Spring Cloud Gateway內(nèi)置了很多路由斷言工廠。用于匹配HTTP請求的不同屬性。
注意:以下的配置,只寫了routes配置的predicates的部分,實際上,predicates單獨用沒有意義,一般要配置filter來用。如上面的添加頭信息的示例。
2.1 The After Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
匹配2017-01-20T17:42:47.789-07:00之后的請求
2.2 The Before Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
匹配2017-01-20T17:42:47.789-07:00之前的請求
2.3 The Between Route Predicate Factory
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]
表示在第一個時間之后,第二個時間之前的請求才能正確匹配路由
2.4 The Cookie Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
接收兩個參數(shù),分別為name 和 regexp(Java正則表達(dá)式),表示cookie中攜帶的name值滿足正則表達(dá)式regexp,則被路由
2.5 The Header Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
接收兩個參數(shù),header 和 regexp(Java正則表達(dá)式),表示header中攜帶的name值滿足正則表達(dá)式regexp,則被路由
2.6 The Host Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
支持URI模板變量(如{sub}.myhost.org)。當(dāng)Host 頭的值為 www.somehost.org 或 beta.somehost.org 或 www.anotherhost.org 都能匹配該路由。
Predicate 會提取URI模板變量作為map集合,并放置在 ServerWebExchange.getAttributes() 中,key定義為 ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE ,這些值能在 GatewayFilter工廠中使用。
2.7 The Method Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
上面示例表示匹配 GET ,POST請求
2.8 The Path Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
如果請求地址是 /red/1 或 /red/1/ 或 /red/blue 或 /blue/green ,那么路由將會被匹配。
如果 matchTrailingSlash 設(shè)置為 false ,那么 /red/1/ 不會被匹配。
Predicate 會提取URI模板變量作為map集合,并放置在 ServerWebExchange.getAttributes() 中,key定義
為 ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE ,這些值能在 GatewayFilter 工廠中使用。
可以使用個實用法(調(diào)用get)來簡化對這些變量的訪問。下面的例示展示了如何使用get方法:
Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
String segment = uriVariables.get("segment");
2.9 The Query Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
接收兩個參數(shù),分別是一個必須的param和一個可選的regexp。
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.
red和gree兩個參數(shù)都有才滿足
2.10 The RemoteAddr Route Predicate Factory
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24.
接收?個sources列表(最小1個),CIDR表示法(IPv4或IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是
?個IP地址,16是?個?網(wǎng)掩碼)。
如果請求的遠(yuǎn)程地址是 192.168.1.10 ,則此路由被匹配文章來源:http://www.zghlxwxcb.cn/news/detail-696391.html
2.11 The Weight Route Predicate Factory
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 。文章來源地址http://www.zghlxwxcb.cn/news/detail-696391.html
到了這里,關(guān)于【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!