java B2B2C Springcloud多租戶電子商城系統- Gateway 之Predict篇

predicate簡介html

電子商務平臺源碼請加企鵝求求:一零三八七七四六二六。Predicate來自於java8的接口。Predicate 接受一個輸入參數,返回一個布爾值結果。該接口包含多種默認方法來將Predicate組合成其餘複雜的邏輯(好比:與,或,非)。能夠用於接口請求參數校驗、判斷新老數據是否有變化須要進行更新操做。add–與、or–或、negate–非。java

Spring Cloud Gateway內置了許多Predict,這些Predict的源碼在org.springframework.cloud.gateway.handler.predicate包中,若是讀者有興趣能夠閱讀一下。web

predicate實戰正則表達式

如今以案例的形式來說解predicate,本文中的案例基原本源於官方文檔,官方文檔地址:cloud.spring.io/spring-clou…spring

建立一個工程,在工程的pom文件引入spring cloud gateway 的起步依賴spring-cloud-starter-gateway,spring cloud版本和spring boot版本,代碼以下:瀏覽器

<parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.5.RELEASE</version>
   </parent>

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Finchley.SR1</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-gateway</artifactId>
 </dependency>

複製代碼

After Route Predicate Factorybash

AfterRoutePredicateFactory,可配置一個時間,當請求的時間在配置時間以後,才交給 router去處理。不然則報錯,不經過路由。cookie

在工程的application.yml配置以下:app

server:
  port: 8081
spring:
  profiles:
    active: after_route

---
spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: http://httpbin.org:80/get
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
  profiles: after_route

複製代碼

在上面的配置文件中,配置了服務的端口爲8081,配置spring.profiles.active:after_route指定了程序的spring的啓動文件爲after_route文件。在application.yml再建一個配置文件,語法是三個橫線,在此配置文件中經過spring.profiles來配置文件名,和spring.profiles.active一致,而後配置spring cloud gateway 相關的配置,id標籤配置的是router的id,每一個router都須要一個惟一的id,uri配置的是將請求路由到哪裏,本案例所有路由到http://httpbin.org:80/get。框架

predicates:

After=2017-01-20T17:42:47.789-07:00[America/Denver] 會被解析成PredicateDefinition對象 (name =After ,args= 2017-01-20T17:42:47.789-07:00[America/Denver])。在這裏須要注意的是predicates的After這個配置,遵循的契約大於配置的思想,它實際被AfterRoutePredicateFactory這個類所處理,這個After就是指定了它的Gateway web handler類爲AfterRoutePredicateFactory,同理,其餘類型的predicate也遵循這個規則。

當請求的時間在這個配置的時間以後,請求會被路由到http://httpbin.org:80/get。

啓動工程,在瀏覽器上訪問http://localhost:8081/,會顯示http://httpbin.org:80/get返回的結果,此時gateway路由到了配置的uri。若是咱們將配置的時間設置到當前時以後,瀏覽器會顯示404,此時證實沒有路由到配置的uri.

跟時間相關的predicates還有Before Route Predicate Factory、Between Route Predicate Factory,讀者能夠自行查閱官方文檔,再次再也不演示。

Header Route Predicate Factory

Header Route Predicate Factory須要2個參數,一個是header名,另一個header值,該值能夠是一個正則表達式。當此斷言匹配了請求的header名和值時,斷言經過,進入到router的規則中去。

在工程的配置文件加上如下的配置:

spring:
 profiles:
   active: header_route

---
spring:
 cloud:
   gateway:
     routes:
     - id: header_route
       uri: http://httpbin.org:80/get
       predicates:
       - Header=X-Request-Id, \d+
 profiles: header_route


複製代碼

在上面的配置中,當請求的Header中有X-Request-Id的header名,且header值爲數字時,請求會被路由到配置的 uri. 使用curl執行如下命令:

$ curl -H 'X-Request-Id:1' localhost:8081

複製代碼

執行命令後,會正確的返回請求結果,結果省略。若是在請求中沒有帶上X-Request-Id的header名,而且值不爲數字時,請求就會報404,路由沒有被正確轉發。

Cookie Route Predicate Factory

Cookie Route Predicate Factory須要2個參數,一個時cookie名字,另外一個時值,能夠爲正則表達式。它用於匹配請求中,帶有該名稱的cookie和cookie匹配正則表達式的請求。

在配置文件添加如下配置:

spring:
  profiles:
    active: cookie_route


---
spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: http://httpbin.org:80/get
        predicates:
        - Cookie=name, forezp
  profiles: cookie_route

複製代碼

在上面的配置中,請求帶有cookie名爲 name, cookie值爲forezp 的請求將都會轉發到uri爲 httpbin.org:80/get的地址上。 使用curl命令進行請求,在請求中帶上 cookie,會返回正確的結果,不然,請求報404錯誤。

$ curl -H 'Cookie:name=forezp' localhost:8081

Host Route Predicate Factory

Host Route Predicate Factory須要一個參數即hostname,它能夠使用. * 等去匹配host。這個參數會匹配請求頭中的host的值,一致,則請求正確轉發。

在工程的配置文件,加上如下配置:

spring:
  profiles:
    active: host_route
---
spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://httpbin.org:80/get
        predicates:
        - Host=**.fangzhipeng.com
  profiles: host_route
複製代碼

在上面的配置中,請求頭中含有Host爲fangzhipeng.com的請求將會被路由轉發轉發到配置的uri。 啓動工程,執行如下的curl命令,請求會返回正確的請求結果:

curl -H 'Host:www.fangzhipeng.com' localhost:8081
複製代碼

Method Route Predicate Factory

Method Route Predicate Factory 須要一個參數,即請求的類型。好比GET類型的請求都轉發到此路由。在工程的配置文件加上如下的配置:

spring:
  profiles:
    active: method_route

---
spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://httpbin.org:80/get
        predicates:
        - Method=GET
  profiles: method_route


複製代碼

在上面的配置中,全部的GET類型的請求都會路由轉發到配置的uri。使用 curl命令模擬 get類型的請求,會獲得正確的返回結果。

$ curl localhost:8081

複製代碼

使用 curl命令模擬 post請求,則返回404結果。

$ curl -XPOST localhost:8081

複製代碼

Path Route Predicate Factory

Path Route Predicate Factory 須要一個參數: 一個spel表達式,應用匹配路徑。

在工程的配置文件application.yml文件中,作如下的配置:

spring:
  profiles:
    active: path_route
---
spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: http://httpbin.org:80/get
        predicates:
        - Path=/foo/{segment}
  profiles: path_route

複製代碼

在上面的配置中,全部的請求路徑知足/foo/{segment}的請求將會匹配並被路由,好比/foo/1 、/foo/bar的請求,將會命中匹配,併成功轉發。

使用curl模擬一個請求localhost:8081/foo/dew,執行以後會返回正確的請求結果。

$ curl localhost:8081/foo/dew

複製代碼

Query Route Predicate Factory

Query Route Predicate Factory 須要2個參數:一個參數名和一個參數值的正則表達式。在工程的配置文件application.yml作如下的配置:

spring:
  profiles:
    active: query_route
---
spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://httpbin.org:80/get
        predicates:
        - Query=foo, ba.
  profiles: query_route

複製代碼

在上面的配置文件中,配置了請求中含有參數foo,而且foo的值匹配ba.,則請求命中路由,好比一個請求中含有參數名爲foo,值的爲bar,可以被正確路由轉發。

模擬請求的命令以下:

$ curl localhost:8081?foo=bar


複製代碼

Query Route Predicate Factory也能夠只填一個參數,填一個參數時,則只匹配參數名,即請求的參數中含有配置的參數名,則命中路由。好比如下的配置中,配置了請求參數中含有參數名爲foo 的參數將會被請求轉發到uri爲http://httpbin.org:80/get。

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: http://httpbin.org:80/get
        predicates:
        - Query=foo
  profiles: query_route

複製代碼

總結

在本篇文章中,首先介紹了Spring Cloud Gateway的工做流程和原理,而後介紹了gateway框架內置的predict及其分類,最後以案例的形式重點講解了幾個重要的Predict。Predict做爲斷言,它決定了請求會被路由到哪一個router 中。在斷言以後,請求會被進入到filter過濾器的邏輯,下篇文章將會爲你們介紹Spring Cloud Gateway過濾器相關的內容。

 java B2B2C Springcloud多租戶電子商城系統 

相關文章
相關標籤/搜索