【SpringCloud】Gateway常用的Predicate(十八)


Predicate介紹

  Spring Cloud Gateway將路由作為Spring WebFlux HandlerMapping基礎架構的一部分進行匹配Spring Cloud Gateway包括許多內置的路由斷言工廠。所有這些斷言都與HTTP請求的不同屬性匹配。您可以將多個路由斷言工廠與邏輯and語句結合使用

  官網:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/#gateway-request-predicates-factories

  常用的Predicate:

  

 

常用的Predicate

  演示項目搭建參考:【SpringCloud】Gateway路由配置(十七),演示請求使用curl命令

  1、After Route Predicate

  所述After斷言有一個參數,一個datetime(其是Java ZonedDateTime)。該斷言匹配在指定日期時間之后發生的請求。下面的示例配置路由后斷言:

  例子1. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: after_route
6           uri: http://localhost:80017           predicates:
8             - After=2020-04-20T23:57:57.308+08:00[Asia/Shanghai]

  這路由符合2020年4月20日23:57時區時間(上海)之后的任何請求。

  測試請求命令:curl http://localhost:9527/payment/get/1

  2、Before Route Predicate

  所述Before斷言有一個參數,一個datetime(其是Java ZonedDateTime)。該斷言匹配在指定之前發生的請求datetime以下示例配置了路由前斷言:

  例子2. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: before_route
6           uri: http://localhost:80017           predicates:
8             - Before=2020-04-21T23:57:57.308+08:00[Asia/Shanghai]

  這路由符合2020年4月21日23:57時區時間(上海)之前的任何請求。

  3、Between Route Predicate

  該Between斷言有兩個參數,datetime1並且datetime2 這是Java ZonedDateTime對象。該斷言匹配在之后datetime1和之前發生的請求datetime2datetime2參數必須是后datetime1以下示例配置了路由之間的斷言:

  例子3. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: between_route
6           uri: http://localhost:8001
7           predicates:
8             - Between=2020-04-20T23:57:57.308+08:00[Asia/Shanghai], 2020-04-21T23:57:57.308+08:00[Asia/Shanghai]

  4、Cookie Route Predicate

  所述Cookie斷言采用兩個參數,該cookie nameregexp(其是Java正則表達式)。該斷言匹配具有給定名稱且其值與正則表達式匹配的cookie。以下示例配置Cookie路由斷言:

  例子4. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: cookie_route
6           uri: http://localhost:8001
7           predicates:
8             - Cookie=username, xiaoming

  此路由匹配具有名稱為usernamexiaoming正則表達式匹配的cookie的請求。

  測試請求命令:curl http://localhost:9527/payment/get/1 --cookie "username=xiaoming"    

  5、Header Route Predicate

  所述Header斷言采用兩個參數,報頭name和一個regexp(其是Java正則表達式)。該斷言與具有給定名稱且其值與正則表達式匹配的標頭匹配。以下示例配置標頭路由斷言:

  例子5. application.yml

1 spring:
2  cloud: 3  gateway: 4  routes: 5 - id: header_route 6 uri: http://localhost:8001 7  predicates: 8 - Header=X-Request-Id, \d+

  如果請求具有名為X-Request-Id其值與\d+正則表達式匹配的標頭(即,其值為一個或多個數字),則此路由匹配

  測試請求命令:curl http://localhost:9527/payment/get/1 -H "X-Request-Id:123"

  6、Host Route Predicate

  Host斷言需要一個參數:主機名的列表patterns該模式是帶有.分隔符的Ant樣式的模式。斷言Host匹配模式標頭匹配。以下示例配置主機路由斷言:

  例子6. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: host_route
6           uri: http://localhost:8001
7           predicates:
8             - Host=**.x.com

  如果請求具有這種路由匹配Host用的頭值**.x.com

  測試請求命令:curl http://localhost:9527/payment/get/1 -H "Host:demo1.x.com"

  7、Method Route Predicate

  所述Method斷言需要methods的參數,它是一個或多個參數:HTTP方法來匹配。以下示例配置方法路由斷言:

  例子7. application.yml
1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: method_route
6           uri: http://localhost:8001
7           predicates:
8             - Method=GET

  測試請求命令:curl http://localhost:9527/payment/get/1 -X GET

  8、Path Route Predicate

  Path斷言有兩個參數:春天的列表PathMatcher patterns和一個可選的標志叫matchOptionalTrailingSeparator以下示例配置路徑路由斷言:

  例子8. application.yml
1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: path_route
6           uri: http://localhost:8001
7           predicates:
8             - Path=/payment/get/**

  測試請求命令:curl http://localhost:9527/payment/get/1 

  9、Query Route Predicate

  所述Query斷言采用兩個參數:所要求的param和可選的regexp(其是Java正則表達式)。以下示例配置查詢路由斷言:

  例子9. application.yml

1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: query_route
6           uri: http://localhost:8001
7           predicates:
8             - Query=green

  如果請求包含green查詢參數,則前面的路由匹配

  測試請求命令:curl http://localhost:9527/payment/get/1?green=1

  10、RemoteAddr Route Predicate

  所述RemoteAddr斷言需要的列表sources,其是CIDR的表示法(IPv4或IPv6)的字符串,如192.168.0.1/16(其中192.168.0.1是一個IP地址和16一個子網掩碼)。以下示例配置一個RemoteAddr路由謂詞:

  例子10. application.yml
1 spring:
2   cloud:
3     gateway:
4       routes:
5         - id: query_route
6           uri: http://localhost:8001
7           predicates:
8             - RemoteAddr=192.168.1.1/24

  請求測試命令:curl http://192.168.1.4:9527/payment/get/1

  11、Weight Route Predicate

  Weight斷言有兩個參數:groupweight(一個int)。權重是按組計算的。以下示例配置權重路由斷言:

  例子11. application.yml

 1 spring:
 2   gateway:
 3     discovery:
 4       routes:
 5         - id: weight_high
 6           uri: http://localhost:8001
 7           predicates:
 8             - Weight=group1, 8
 9         - id: weight_low
10           uri: http://localhost:8002
11           predicates:
12             - Weight=group1, 2

  這條路線會將大約80%的流量轉發到http://localhost:8001,將大約20%的流量轉發到http://localhost:8001

  請求測試命令:curl http://localhost:9527/payment/get/1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM