Retry フィルター

Retry フィルターは次のパラメーターをサポートします。

  • retries: 試行する必要のある再試行の回数。

  • methodsorg.springframework.http.HttpMethod を使用して表される、再試行する必要のある HTTP メソッド。

  • seriesorg.springframework.http.HttpStatus.Series を使用して表される、再試行される一連のステータスコード。

  • exceptions: 再試行する必要があるスローされた例外のリスト。

  • cacheBody: リクエスト本文をキャッシュするかどうかを通知するフラグ。true に設定されている場合、キャッシュされた本文を下流に送信するために adaptCacheBody フィルターを使用する必要があります。

有効になっている場合、Retry フィルターには次のデフォルトが構成されています。

  • retries: 3 回

  • series: 5XX シリーズ

  • methods: GET メソッド

  • exceptionsIOExceptionTimeoutExceptionRetryException

  • cacheBodyfalse

cacheBody を true に設定すると、ゲートウェイは本体全体をメモリに読み込みます。これは注意して使用する必要があります。

次のリストは、再試行フィルターを構成します。

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: retry_route
          uri: http://localhost:8080/flakey
          predicates:
          - Host=*.retry.com
          filters:
          - name: Retry
            args:
              retries: 3
              series: SERVER_ERROR
              methods: GET,POST
              cacheBody: true
          - name: AdaptCachedBody
GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.filter.FilterFunctions.adaptCachedBody;
import static org.springframework.cloud.gateway.server.mvc.filter.RetryFilterFunctions.retry;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsRetry() {
        return route("retry_route")
            .route(host("*.retry.com"), http())
            .before(uri("http://localhost:8080/flakey"))
            .filter(retry(config -> config.setRetries(3)
                    .setSeries(Set.of(HttpStatus.Series.SERVER_ERROR))
                    .setMethods(Set.of(HttpMethod.GET, HttpMethod.POST))
                    .setCacheBody(true)))
            .filter(adaptCachedBody())
            .build();
    }
}
forward: プレフィックス付き URL で再試行フィルターを使用する場合は、エラーが発生した場合にクライアントにレスポンスが送信されてコミットされる可能性のある処理を行わないように、ターゲットエンドポイントを慎重に記述する必要があります。例: ターゲットエンドポイントがアノテーション付きコントローラーである場合、ターゲットコントローラーメソッドはエラーステータスコードで ResponseEntity を返さないようにする必要があります。代わりに、Exception をスローするか、エラーを通知する必要があります(たとえば、Mono.error(ex) の戻り値を介して)。これは、再試行によって処理するように再試行フィルターを構成できます。
再試行フィルターを使用すると、その後に続くすべてのフィルターが再試行されます。再試行フィルターの後のフィルターを複数回実行した場合、その結果が期待どおりであることを確認してください。
ボディと cacheBody=true を持つ HTTP メソッドでリトライフィルターを使用すると、ボディがキャッシュされ、ゲートウェイのメモリが制限されます。ボディは、MvcUtils.CACHED_REQUEST_BODY_ATT で定義されたリクエスト属性にキャッシュされます。オブジェクトの型は ByteArrayInputStream です。

簡略化された「ショートカット」表記は、単一の status および method で追加できます。

次の 2 つのルートの例は同等です。

application.yml
spring:
  cloud:
    gateway:
      routes:
      - id: retry_route
        uri: https://example.org
        filters:
        - name: Retry
          args:
            retries: 3
            statuses: INTERNAL_SERVER_ERROR
            methods: GET
      - id: retryshortcut_route
        uri: https://example.org
        filters:
        - Retry=3,INTERNAL_SERVER_ERROR,GET