Retry 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計算。 -
jitter:為重試配置的隨機抖動。在[backoff - backoff*randomFactor, backoff + backoff*randomFactor]之間生成退避 -
timeout:為重試配置的超時。
如果啟用,Retry 過濾器配置了以下預設值
-
retries:三次 -
series:5XX 系列 -
methods:GET 方法 -
exceptions:IOException和TimeoutException -
backoff:已停用 -
jitter:已停用 -
timeout:無限制
以下列表配置了一個 Retry GatewayFilter
spring:
cloud:
gateway:
routes:
- id: retry_test
uri: https://: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
jitter:
randomFactor: 0.5
timeout: 100ms
當使用重試過濾器和以 forward: 為字首的 URL 時,應仔細編寫目標端點,以便在發生錯誤時,它不會執行任何可能導致響應傳送到客戶端並提交的操作。例如,如果目標端點是帶註解的控制器,則目標控制器方法不應返回帶有錯誤狀態碼的 ResponseEntity。相反,它應該丟擲 Exception 或發出錯誤訊號(例如,透過 Mono.error(ex) 返回值),重試過濾器可以配置為透過重試來處理這些錯誤。 |
| 使用重試過濾器時,它將重試其之後的所有過濾器。請確保重試過濾器之後執行的過濾器的結果在多次執行時符合預期。 |
當使用重試過濾器和任何帶請求體的 HTTP 方法時,請求體將被快取,並且閘道器將受到記憶體限制。請求體快取在由 ServerWebExchangeUtils.CACHED_REQUEST_BODY_ATTR 定義的請求屬性中。物件的型別是 org.springframework.core.io.buffer.DataBuffer。 |
可以使用單個 status 和 method 新增簡化的“快捷方式”表示法。
以下兩個示例是等效的
spring:
cloud:
gateway:
routes:
- id: retry_route
uri: https://example.org
filters:
- name: Retry
args:
retries: 3
statuses: INTERNAL_SERVER_ERROR
methods: GET
backoff:
firstBackoff: 10ms
maxBackoff: 50ms
factor: 2
basedOnPreviousValue: false
jitter:
randomFactor: 0.5
timeout: 100ms
- id: retryshortcut_route
uri: https://example.org
filters:
- Retry=3,INTERNAL_SERVER_ERROR,GET,10ms,50ms,2,false,0.5,100ms