Retry フィルター

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

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

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

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

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

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

  • retries: 3 回

  • series: 5XX シリーズ

  • methods: GET メソッド

  • exceptionsIOExceptionTimeoutExceptionRetryException

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

application.yml
spring:
  cloud:
    gateway:
      mvc:
        routes:
        - id: retry_test
          uri: http://localhost:8080/flakey
          predicates:
          - Host=*.retry.com
          filters:
          - name: Retry
            args:
              retries: 3
              series: SERVER_ERROR
              methods: GET,POST
GatewaySampleApplication.java
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> gatewayRouterFunctionsAddReqHeader() {
		return route("add_request_parameter_route")
			.route(host("*.retry.com"), http("https://example.org"))
				.filter(retry(config -> config.setRetries(3).setSeries(Set.of(HttpStatus.Series.SERVER_ERROR)).setMethods(Set.of(HttpMethod.GET, HttpMethod.POST))))
			.build();
    }
}
forward: プレフィックス付き URL で再試行フィルターを使用する場合は、エラーが発生した場合にクライアントにレスポンスが送信されてコミットされる可能性のある処理を行わないように、ターゲットエンドポイントを慎重に記述する必要があります。例: ターゲットエンドポイントがアノテーション付きコントローラーである場合、ターゲットコントローラーメソッドはエラーステータスコードで ResponseEntity を返さないようにする必要があります。代わりに、Exception をスローするか、エラーを通知する必要があります(たとえば、Mono.error(ex) の戻り値を介して)。これは、再試行によって処理するように再試行フィルターを構成できます。

簡略化された「ショートカット」表記は、単一の 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