TIPS
本文基於Spring Cloud Greenwich SR2編寫,兼容Spring Cloud Finchley及更高版本。
這一節來詳細探討Spring Cloud Gateway的路由謂詞工廠 (Route Predicate Factories),路由謂詞工廠的作用是:符合Predicate的條件,就使用該路由的配置,否則就不管。 只要掌握這一句,掌握路由謂詞工廠就比較輕鬆了。
TIPS
Predicate是Java 8提供的一個函數式編程接口。
本文探討了Spring Cloud Gateway中內置的謂詞工廠,包括:
謂詞工廠
After
Before
Between
Cookie
Header
Host
Method
Path
Query
RemoteAddr
路由配置的兩種形式
先來探討Spring Cloud Gateway路由配置的兩種姿勢:
路由到指定URL
示例1:通配
spring:
cloud:
gateway:
routes:
- id: {唯一標識}
uri: http://www.itmuch.com
表示訪問 GATEWAY_URL/** 會轉發到 http://www.itmuch.com/**
TIPS
這段配置不能直接使用,需要和下面的Predicate配合使用才行。
示例2:精確匹配
spring:
cloud:
gateway:
routes:
- id: {唯一標識}
uri: http://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/
表示訪問 GATEWAY_URL/spring-cloud/spring-cloud-stream-pan-ta/ 會轉發到 http://www.itmuch.com/spring-cloud/spring-cloud-stream-pan-ta/
TIPS
這段配置不能直接使用,需要和下面的Predicate配合使用才行。
路由到服務發現組件上的微服務
示例1:通配
spring:
cloud:
gateway:
routes:
- id: {唯一標識}
uri: lb://user-center
表示訪問 GATEWAY_URL/** 會轉發到 user-center 微服務的 /**
TIPS
這段配置不能直接使用,需要和下面的Predicate配合使用才行。
示例2:精確匹配
spring:
cloud:
gateway:
routes:
- id: {唯一標識}
uri: lb://user-center/shares/1
表示訪問 GATEWAY_URL/shares/1 會轉發到 user-center 微服務的 /shares/1
TIPS
這段配置不能直接使用,需要和下面的Predicate配合使用才行。
謂詞工廠詳解
下面正式探討路由謂詞工廠。Spring Cloud Gateway提供了十來種路由謂詞工廠。為網關實現靈活的轉發提供了基石。
After
示例:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: lb://user-center
predicates:
# 當且僅當請求時的時間After配置的時間時,才會轉發到用戶微服務
# 目前配置不會進該路由配置,所以返回404
# 將時間改成 < now的時間,則訪問localhost:8040/** -> user-center/**
# eg. 訪問http://localhost:8040/users/1 -> user-center/users/1
- After=2030-01-20T17:42:47.789-07:00[America/Denver]
TIPS
- 技巧:時間可使用 System.out.println(ZonedDateTime.now()); 打印,然後即可看到時區。例如:2019-08-10T16:50:42.579+08:00[Asia/Shanghai]
- 時間格式的相關邏輯:
- 默認時間格式:org.springframework.format.support.DefaultFormattingConversionService#addDefaultFormatters
- 時間格式註冊:org.springframework.format.datetime.standard.DateTimeFormatterRegistrar#registerFormatters
Before
示例:
spring:
cloud:
gateway:
routes:
- id: before_route
uri: lb://user-center
predicates:
# 當且僅當請求時的時間Before配置的時間時,才會轉發到用戶微服務
# 目前配置不會進該路由配置,所以返回404
# 將時間改成 > now的時間,則訪問localhost:8040/** -> user-center/**
# eg. 訪問http://localhost:8040/users/1 -> user-center/users/1
- Before=2018-01-20T17:42:47.789-07:00[America/Denver]
Between
示例:
spring:
cloud:
gateway:
routes:
- id: between_route
uri: lb://user-center
predicates:
# 當且僅當請求時的時間Between配置的時間時,才會轉發到用戶微服務
# 因此,訪問localhost:8040/** -> user-center/**
# eg. 訪問http://localhost:8040/users/1 -> user-center/users/1
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2027-01-21T17:42:47.789-07:00[America/Denver]
Cookie
示例:
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: lb://user-center
predicates:
# 當且僅當帶有名為somecookie,並且值符合正則ch.p的Cookie時,才會轉發到用戶微服務
# 如Cookie滿足條件,則訪問http://localhost:8040/** -> user-center/**
# eg. 訪問http://localhost:8040/users/1 -> user-center/users/1
- Cookie=somecookie, ch.p
Header
spring:
cloud:
gateway:
routes:
- id: header_route
uri: lb://user-center
predicates:
# 當且僅當帶有名為X-Request-Id,並且值符合正則\d+的Header時,才會轉發到用戶微服務
# 如Header滿足條件,則訪問http://localhost:8040/** -> user-center/**
# eg. 訪問http://localhost:8040/users/1 -> user-center/users/1
- Header=X-Request-Id, \d+
Host
spring:
cloud:
gateway:
routes:
- id: host_route
uri: lb://user-center
predicates:
# 當且僅當名為Host的Header符合**.somehost.org或**.anotherhost.org時,才會轉發用戶微服務
# 如Host滿足條件,則訪問http://localhost:8040/** -> user-center/**
# eg. 訪問http://localhost:8040/users/1 -> user-center/users/1
- Host=**.somehost.org,**.anotherhost.org
Method
spring:
cloud:
gateway:
routes:
- id: method_route
uri: lb://user-center
predicates:
# 當且僅當HTTP請求方法是GET時,才會轉發用戶微服務
# 如請求方法滿足條件,訪問http://localhost:8040/** -> user-center/**
# eg. 訪問http://localhost:8040/users/1 -> user-center/users/1
- Method=GET
Path
spring:
cloud:
gateway:
routes:
- id: path_route
uri: lb://user-center
predicates:
# 當且僅當訪問路徑是/users/*或者/some-path/**,才會轉發用戶微服務
# segment是一個特殊的佔位符,單層路徑匹配
# eg. 訪問http://localhost:8040/users/1 -> user-center/users/1
- Path=/users/{segment},/some-path/**
TIPS
建議大家看下這一部分的官方文檔,裡面有個segment編程技巧。比較簡單,留個印象。
https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_path_route_predicate_factory
Query
示例1:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: lb://user-center
predicates:
# 當且僅當請求帶有baz的參數,才會轉發到用戶微服務
# eg. 訪問http://localhost:8040/users/1?baz=xx -> user-center的/users/1
- Query=baz
示例2:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: lb://user-center
predicates:
# 當且僅當請求帶有名為foo的參數,且參數值符合正則ba.,才會轉發到用戶微服務
# eg. 訪問http://localhost:8040/users/1?baz=baz -> user-center的/users/1?baz=baz
- Query=foo, ba.
RemoteAddr
示例:
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: lb://user-center
predicates:
# 當且僅當請求IP是192.168.1.1/24網段,例如192.168.1.10,才會轉發到用戶微服務
# eg. 訪問http://localhost:8040/users/1 -> user-center的/users/1
- RemoteAddr=192.168.1.1/24
TIPS
建議大家看下這一部分的官方文檔,有個小編程技巧。比較簡單,留個印象。
https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_remoteaddr_route_predicate_factory
相關代碼
本文首發
http://www.itmuch.com/spring-cloud-gateway/route-predicate-factory/
作者:大目
鏈接:http://www.imooc.com/article/290804
來源:慕課網
本文原創發佈於慕課網 ,轉載請註明出處,謝謝合作