SpringCloudGateway配置介紹

做過微服務開發,或者瞭解、學習過微服務的朋友對Spring Cloud Netflix Zuul肯定不陌生!隨着微服務的不斷髮展Spring Cloud 推出了Spring Cloud Gateway,他相比zuul的功能更強大,入門也相對簡單!接下來就開始對Gateway的學習吧

一.Gateway工作方式

在這裏插入圖片描述
客戶端向Spring Cloud Gateway發出請求。如果網關處理程序映射確定請求與路由匹配,則將其發送到網關Web處理程序。該處理程序通過特定於請求的過濾器鏈來運行請求。篩選器由虛線分隔的原因是,篩選器可以在發送代理請求之前和之後運行邏輯。所有“前置”過濾器邏輯均被執行。然後發出代理請求。發出代理請求後,將運行“後”過濾器邏輯。這裏的處理相比Zuul強大很多,可以通過Filter來自定義一些處理方式!

二.配置方式

2.1 根據時間進行請求過濾

After 匹配在指定日期時間之後發生的請求
Before匹配在指定日期時間之前發生的請求
Between 匹配在指定日期時間之內發生的請求

spring:
  cloud:
    gateway:
      routes:
      - id: after_route # 唯一表示,有意義即可
        uri: https://example.org # 服務的訪問路徑
        predicates:
        - After=2020-02-20T17:42:47.789-07:00[America/Denver]

2.2 Cookie / Header 線路匹配

Cookie / Header工廠採用兩個參數,該cookie name和regexp(其是Java正則表達式)

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p        

此路由匹配具有名稱爲chocolate與ch.p正則表達式匹配的cookie的請求。

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+

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

2.3 Method 路線匹配

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST

是匹配GET ,POST請求

2.4 路徑路線匹配

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

如果請求路徑是,例如匹配/red/1或/red/blue或/blue/green

2.5 重量路線匹配

該Weight路線謂詞工廠有兩個參數:group和weight(一個int)。權重是按組計算的

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

這條路線會將大約80%的流量轉發到weighthigh.org,將大約20%的流量轉發到weightlow.org。

2.6 filters 線路匹配

PrefixPath

spring:
  cloud:
    gateway:
      routes:
      - id: prefixpath_route
        uri: https://example.org
        filters:
        - PrefixPath=/mypath

這將/mypath作爲所有匹配請求的路徑的前綴。因此,/hello將向發送請求/mypath/hello。
StripPrefix

spring:
  cloud:
    gateway:
      routes:
      - id: nameRoot
        uri: https://nameservice
        predicates:
        - Path=/name/**
        filters:
        - StripPrefix=2

通過網關/name/blue/red發出請求時,發出的請求nameservice看起來像nameservice/red
GatewayFilter工廠
該Retry GatewayFilter工廠支持以下參數:

  • retries:應嘗試的重試次數。

  • statuses:應重試的HTTP狀態代碼,使用表示org.springframework.http.HttpStatus。

  • methods:應該重試的HTTP方法,以表示org.springframework.http.HttpMethod。

  • series:要重試的一系列狀態代碼,使用表示org.springframework.http.HttpStatus.Series。

  • exceptions:應重試的引發異常的列表。

  • backoff:爲重試配置的指數補償。重試在的退避間隔後執行firstBackoff * (factor ^ n),其中n爲迭代。如果maxBackoff已配置,則應用的最大退避限制爲- maxBackoff。如果basedOnPreviousValue爲true,則使用計算退避prevBackoff * factor。

Retry如果啓用了以下默認過濾器配置:

  • retries:3次

  • series:5XX系列

  • methods:GET方法

  • exceptions:IOException和TimeoutException

  • backoff:禁用

以下清單配置了Retry GatewayFilter

spring:
  cloud:
    gateway:
      routes:
      - id: retry_test
        uri: http://localhost:8080/flakey
        predicates:
        - Host=*.retry.com
        filters:
        - name: Retry
          args:
            retries: 3
            statuses: BAD_GATEWAY
            methods: GET,POST
            backoff:
              firstBackoff: 10ms
              maxBackoff: 50ms
              factor: 2
              basedOnPreviousValue: false

2.6 根據註冊中心內服務名轉發

上面的案例都是根據url進行服務轉發,我們也可根據服務名進行服務的轉發

spring:
  cloud:
    gateway:
      routes:
      - id: myRoute
        uri: lb://service 
        predicates:
        - Path=/service/**

lb:// 這是固定的寫法,後面寫服務的名稱

2.6 超時配置

要配置全局http超時:
connect-timeout必須以毫秒爲單位指定。
response-timeout必須指定爲java.time.Duration

spring:
  cloud:
    gateway:
      httpclient:
        connect-timeout: 1000
        response-timeout: 5s

每個路由超時

   - id: per_route_timeouts
        uri: https://example.org
        predicates:
          - name: Path
            args:
              pattern: /delay/{timeout}
        metadata:
          response-timeout: 200
          connect-timeout: 200

2.7 CORS配置

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOrigins: "https://docs.spring.io"
            allowedMethods:
            - GET

在前面的示例中,允許從docs.spring.io所有GET請求路徑的源請求發出CORS請求

Gateway的配置方式遠遠不止這些,更多的配置可以看官方介紹,或者API介紹

本文的分享暫時就到這裏,希望對您有所幫助
關注 Java有貨領取更多資料

聯繫小編。微信:372787553,帶您進羣互相學習
左側小編微信,右側獲取免費資料
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章