環境:springboot2.3.7 + spring cloud Hoxton.SR9
Spring Cloud Gateway工作流程:
用戶端調用spring cloud gateway配置的請求。根據配置的映射确定請求與路由,将其發送到網關Web處理程式。此處理程式通過特定于請求的過濾器運作請求。過濾器被虛線分割的原因是,過濾器可以在代理請求發送之前和之後運作邏輯。執行所有“pre”過濾器邏輯。然後發出代理請求。在發出代理請求之後,運作“post”過濾器邏輯。
一. 路由謂詞
-
After 路由謂詞工廠
作用:After 謂詞工廠接受一個參數datetime(它是java ZonedDateTime)。此謂詞比對在指定日期時間之後發生的請求。以下示例配置After Route謂詞:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: after_route
uri: https://www.baidu.com
predicates:
- After=2021-01-10T17:42:47.789-07:00
所有的請求必須在
2021-01-10T17:42:47.789-07:00之後才能通路。當超過目前配置的這個時間,請求會傳回404。
測試:
目前時間:2021-01-11 17:09
After=2021-01-10T17:42:47.789-07:00
正常調整到了頁面:
設定:
After=2021-01-20T17:42:47.789-07:00
-
Before 路由謂詞工廠
作用:Before 謂詞工廠接受一個參數datetime(它是java ZonedDateTime)。此謂詞比對在指定日期時間之前發生的請求。以下示例配置Before Route謂詞:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: before_route
uri: https://www.qq.com
predicates:
- Before=2021-01-10T17:42:47.789-07:00
運作結果與After正好相反。
-
Between 路由謂詞工廠
作用:Between謂詞工廠接受兩個參數datetime(它是java ZonedDateTime)。此謂詞比對請求必須在兩個日期之間發生。以下示例配置Between Route謂詞:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: between_route
uri: https://www.163.com
predicates:
- Between=2021-01-01T17:42:47, 2021-01-10T17:42:47
請求必須位于2021-01-01T17:42:47到2021-01-10T17:42:47之間,如果超過這個日期範圍傳回
404。
失敗:
配置:Between=2021-01-01T17:42:47, 2021-01-10T17:42:47
-
Cookie 路由謂詞工廠
作用:隻有網關請求中包含指定的cookie 才能比對,否則404。以下示例配置Cookie Route謂詞:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: cookie_route
uri: https://www.baidu.com
predicates:
- Cookie=code,testiptv255
當請求header中包含的Cookie中包含name為code,值為testiptv255時,請求才能被比對。
示例:
當不包含name為code的cookie時請求傳回404。
- Header 路由謂詞工廠
作用:當請求中包含指定的Header資訊時,請求才能比對。以下示例配置Header Route謂詞:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: header_route
uri: https://www.baidu.com
predicates:
- Header=Host,localhost:20000
當網關請求Header中包含Host請求頭,并且值為localhost:20000,請求才會被比對。
示例:
- Host 路由謂詞工廠
作用:當請求Header中的Host頭資訊為配置中的清單中,那麼網關将被比對。以下示例配置Host Route謂詞:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: host_route
uri: https://www.baidu.com
predicates:
- Host=**.xg.com,localhost:20000
當請求Header中的Host取值為:www.xg.com或者xxx.xg.com或者localhost:20000時路由将被比對:
示例:
- Method 路由謂詞工廠
作用:當請求是指定的Method時,路由将被比對。以下示例配置Method Route謂詞:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: method_route
uri: https://www.baidu.com
predicates:
- Method=GET,POST
當請求是GET或者是POST請求時,網關路由将會被比對。
示例:
- Path 路由謂詞工廠
作用:當請求的路徑為指定的Path時,路由将會被比對。以下示例配置Path Route謂詞:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: path_route
uri: https://www.baidu.com
predicates:
- Path=/api-a/{segment},/api-1/{segment}
當請求的位址為:http://xxx/api-a/xxx或者是http://xxx/api-1/xxx時,路由将會被比對;注意:你的目标位址應該有/api-a/xxx和/api-1/xxx這兩個請求位址。
示例:
頁面顯示的404是因為
http://www.baidu.com/api-a/1沒有這樣的請求位址是baidu傳回的404,并不是我們伺服器傳回的404錯誤。
- Query 路由謂詞工廠
作用:當請求中包含指定的查詢參數或者是包含指定的查詢參數及值時才比對路由。以下示例配置Query Route謂詞:
1、方式1:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: query_route
uri: https://www.baidu.com
predicates:
- Query=name
2、方式2:
spring:
cloud:
gateway:
enabled: true
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: query_route
uri: https://www.baidu.com
predicates:
- Query=name,admin
方式1:當請求中隻要包含name參數即可。
方式2:當請求中包含name參數名并且值為admin時比對路由。
當值非admin時傳回404。
下節:《Spring Cloud Gateway應用詳解2之Filter》
關注+轉發謝謝