Gateway入門案例


Zuul 1.x 是一個基於阻塞 IO 的 API Gateway 以及 Servlet;直到 2018 年 5 月,Zuul 2.x(基於Netty,也是非阻塞的,支持長連接)才發布,但 Spring Cloud 暫時還沒有整合計划。Spring CloudGateway 比 Zuul 1.x 系列的性能和功能整體要好。

Gateway簡介

簡介

Spring Cloud Gateway 是 Spring 官方基於 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技術開發的網關,旨在為微服務架構提供一種簡單而有效的統一的 API 路由管理方式,統一訪問接口。SpringCloud Gateway 作為 Spring Cloud 生態系中的網關,目標是替代 Netflflix ZUUL,其不僅提供統一的路由方式,並且基於 Filter 鏈的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等。它是基於Nttey的響應式開發模式。
上表為Spring Cloud Gateway與Zuul的性能對比,從結果可知,Spring Cloud Gateway的RPS是Zuul的1.6倍 

核心概念

1. 路由(route) 路由是網關最基礎的部分,路由信息由一個ID、一個目的URL、一組斷言工廠和一組Filter組成。如果斷言為真,則說明請求URL和配置的路由匹配。
2. 斷言(predicates) Java8中的斷言函數,Spring Cloud Gateway中的斷言函數輸入類型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的斷言函數允許開發者去定義匹配來自Http Request中的任何信息,比如請求頭和參數等。
3. 過濾器(fifilter) 一個標准的Spring webFilter,Spring Cloud Gateway中的Filter分為兩種類型,分別是Gateway Filter和Global Filter。過濾器Filter可以對請求和響應進行處理。 

入門案例

(1) 創建工程導入依賴

在項目中添加新的模塊 shop_gateway_server ,並導入依賴
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
  </dependency>
注意SpringCloud Gateway使用的web框架為webflflux,和SpringMVC不兼容。引入的限流組件是hystrix。redis底層不再使用jedis,而是lettuce。

(2) 配置啟動類

@SpringBootApplication
public class GatewayServerApplication {
  public static void main(String[] args) {
      SpringApplication.run(GatewayServerApplication.class, args);
  }
}

(3) 編寫配置文件

創建 application.yml 配置文件
  server:
    port: 8080 #服務端口
  spring:
    application:
      name: api-gateway #指定服務名
    cloud:
      gateway:
        routes:
        - id: product-service
          uri: http://127.0.0.1:9002
          predicates:
          - Path=/product/**
id:我們自定義的路由 ID,保持唯一
uri:目標服務地址
predicates:路由條件,Predicate 接受一個輸入參數,返回一個布爾值結果。該接口包含多種默認方法來將 Predicate 組合成其他復雜的邏輯(比如:與,或,非)。
fifilters:過濾規則,暫時沒用。
上面這段配置的意思是,配置了一個 id 為 product-service的路由規則,當訪問網關請求地址以product 開頭時,會自動轉發到地址: http://127.0.0.1:9002/ 。配置完成啟動項目即可在瀏覽器訪問進行測試,當我們訪問地址 http://localhost:8080/product/1 時會展示頁面展示如下:

路由規則

Spring Cloud Gateway 的功能很強大,前面我們只是使用了 predicates 進行了簡單的條件匹配,其實Spring Cloud Gataway 幫我們內置了很多 Predicates 功能。在 Spring Cloud Gateway 中 Spring 利用Predicate 的特性實現了各種路由匹配規則,有通過 Header、請求參數等不同的條件來進行作為條件匹配到對應的路由。 

示例

  #路由斷言之后匹配
  spring:
    cloud:
      gateway:
        routes:
        - id: after_route
          uri: https://xxxx.com
          #路由斷言之前匹配
          predicates:
          - After=xxxxx
  #路由斷言之前匹配
  spring:
    cloud:
      gateway:
        routes:
        - id: before_route
          uri: https://xxxxxx.com
          predicates:
          - Before=xxxxxxx
  #路由斷言之間
  spring:
    cloud:
      gateway:
        routes:
        - id: between_route
          uri: https://xxxx.com
          predicates:
          - Between=xxxx,xxxx
  #路由斷言Cookie匹配,此predicate匹配給定名稱(chocolate)和正則表達式(ch.p)
  spring:
    cloud:
      gateway:
        routes:
        - id: cookie_route
          uri: https://xxxx.com
          predicates:
          - Cookie=chocolate, ch.p
  #路由斷言Header匹配,header名稱匹配X-Request-Id,且正則表達式匹配\d+
spring:
    cloud:
      gateway:
        routes:
        - id: header_route
          uri: https://xxxx.com
          predicates:
          - Header=X-Request-Id, \d+
  #路由斷言匹配Host匹配,匹配下面Host主機列表,**代表可變參數
  spring:
    cloud:
      gateway:
        routes:
        - id: host_route
          uri: https://xxxx.com
          predicates:
          - Host=**.somehost.org,**.anotherhost.org
  #路由斷言Method匹配,匹配的是請求的HTTP方法
  spring:
    cloud:
      gateway:
        routes:
        - id: method_route
          uri: https://xxxx.com
          predicates:
          - Method=GET
  #路由斷言匹配,{segment}為可變參數
  spring:
    cloud:
      gateway:
        routes:
        - id: host_route
          uri: https://xxxx.com
          predicates:
          - Path=/foo/{segment},/bar/{segment}
  #路由斷言Query匹配,將請求的參數param(baz)進行匹配,也可以進行regexp正則表達式匹配 (參數包含
  foo,並且foo的值匹配ba.)
  spring:
    cloud:
      gateway:
        routes:
        - id: query_route
          uri: https://xxxx.com
          predicates:
          - Query=baz 或 Query=foo,ba.

  #路由斷言RemoteAddr匹配,將匹配192.168.1.1~192.168.1.254之間的ip地址,其中24為子網掩碼位
  數即255.255.255.0
  spring:
    cloud:
      gateway:
        routes:
        - id: remoteaddr_route
          uri: https://example.org
          predicates:
          - RemoteAddr=192.168.1.1/24

動態路由

和zuul網關類似,在SpringCloud GateWay中也支持動態路由:即自動的從注冊中心中獲取服務列表並訪問。

(1)添加注冊中心依賴

在工程的pom文件中添加注冊中心的客戶端依賴(這里以Eureka為例)
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

(2)配置動態路由

修改 application.yml 配置文件,添加eureka注冊中心的相關配置,並修改訪問映射的URL為服務名稱 
  server:
    port: 8080 #服務端口
  spring:
    application:
      name: api-gateway #指定服務名
    cloud:
      gateway:
        routes:
        - id: product-service
          uri: lb://shop-service-product
          predicates:
          - Path=/product/**
  eureka:
    client:
      serviceUrl:
        defaultZone: http://127.0.0.1:8761/eureka/
        registry-fetch-interval-seconds: 5 # 獲取服務列表的周期:5s
    instance:
      preferIpAddress: true
      ip-address: 127.0.0.1
uri : uri以 lb: //開頭(lb代表從注冊中心獲取服務),后面接的就是你需要轉發到的服務名稱 

重寫轉發路徑

在SpringCloud Gateway中,路由轉發是直接將匹配的路由path直接拼接到映射路徑(URI)之后,那么在微服務開發中往往沒有那么便利。這里就可以通過RewritePath機制來進行路徑重寫。

(1) 案例改造

修改 application.yml ,將匹配路徑改為 /product-service/** 
重新啟動網關,我們在瀏覽器訪問http://127.0.0.1:8080/product-service/product/1,會拋出404。這是由於路由轉發規則默認轉發到商品微服務( http://127.0.0.1:9002/product-service/product/1 )路徑上,而商品微服務又沒有 product-service 對應的映射配置。 

(2) 添加RewritePath重寫轉發路徑

修改 application.yml ,添加重寫規則
  spring:
    application:
      name: api-gateway #指定服務名
    cloud:
      gateway:
        routes:
        - id: product-service
          uri: lb://shop-service-product
          predicates:
          - Path=/product-service/**
          filters:
          - RewritePath=/product-service/(?<segment>.*), /$\{segment}
通過RewritePath配置重寫轉發的url,將/product-service/(?.*),重寫為{segment},然后轉發到訂單微服務。比如在網頁上請求http://localhost:8080/product-service/product,此時會將請求轉發到http://127.0.0.1:9002/product/1( 值得注意的是在yml文檔中 $ 要寫成 $\ )
 


免責聲明!

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



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