微服務網關GateWay


Gateway簡介
簡介
Spring Cloud Gateway 是 Spring 官方基於 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技術開
發的網關,旨在為微服務架構提供一種簡單而有效的統一的 API 路由管理方式,統一訪問接口。Spring
Cloud Gateway 作為 Spring Cloud 生態系中的網關,目標是替代 Netflix 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. 過濾器(filter) 一個標准的Spring webFilter,Spring Cloud Gateway中的Filter分為兩種類型,
分別是Gateway Filter和Global Filter。過濾器Filter可以對請求和響應進行處理。

入門案例
(1) 創建工程導入依賴
在項目中添加新的模塊 shop_gateway_server ,並導入依賴

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

注意 SpringCloud Gateway使用的web框架為webflux,和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: 9014
eureka:
  instance:
    instance-id: gateway-server
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:9010/eureka/
spring:
  application:
    name: gateway-server
  cloud:
    gateway:
      routes:
      - id: order-service
        uri: lb://service-order2
        predicates:
        - Path=/order/**

id :我們自定義的路由 ID,保持唯一
uri :目標服務地址
predicates :路由條件,Predicate 接受一個輸入參數,返回一個布爾值結果。該接口包含多種默認方法來將 Predicate 組合成其他復雜的邏輯(比如:與,或,非)。
filters :過濾規則,暫時沒用。

 uri以 lb: //開頭(lb代表從注冊中心獲取服務),后面接的就是你需要轉發到的服務名稱

路由規則
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

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

  cloud:
    gateway:
      routes:
      - id: order-service
        uri: lb://service-order2
        predicates:
        # - Path=/order/**
        - Path=/order-service/**
        filters:
        - RewritePath=/order-service/(?<segment>.*), /$\{segment

 

 根據微服務名進行轉發(這樣默認和Zuul一樣)

spring:
  application:
    name: gateway-server
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true # 啟動根據服務名稱進行轉發
          lower-case-service-id: true #忽略大小寫
#      routes:
#      - id: order-service
#        uri: lb://service-order2
#        predicates:
#        # - Path=/order/**
#        - Path=/order-service/**
#        filters:
#        - RewritePath=/order-service/(?<segment>.*), /$\{segment}

 


免責聲明!

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



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