このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring Cloud Gateway 4.3.2 を使用してください! |
Retry フィルター
The Retry filter automatically selects the appropriate retry implementation based on what is available on the classpath:
If Spring Retry [GitHub] (英語) is on the classpath, the filter uses
GatewayRetryFilterFunctions(based on Spring Retry) by default.If Spring Retry is not on the classpath, the filter automatically uses
FrameworkRetryFilterFunctions(based on the retry functionality in Spring Framework 7 ).
Spring Retry has been placed in maintenance-only mode. Once Spring Retry is no longer being maintained, the Retry filter will exclusively use the Framework retry implementation (FrameworkRetryFilterFunctions), and the Spring Retry-based implementation will be removed. |
You can force the use of the Framework retry filter even when Spring Retry is on the classpath by setting spring.cloud.gateway.server.webmvc.use-framework-retry-filter=true in your configuration. |
Retry フィルターは次のパラメーターをサポートします。
retries: 試行する必要のある再試行の回数。methods:org.springframework.http.HttpMethodを使用して表される、再試行する必要のある HTTP メソッド。series:org.springframework.http.HttpStatus.Seriesを使用して表される、再試行される一連のステータスコード。exceptions: 再試行する必要があるスローされた例外のリスト。cacheBody: リクエスト本文をキャッシュするかどうかを通知するフラグ。trueに設定されている場合、キャッシュされた本文を下流に送信するためにadaptCacheBodyフィルターを使用する必要があります。
有効になっている場合、Retry フィルターには次のデフォルトが構成されています。
retries: 3 回series: 5XX シリーズmethods: GET メソッドexceptions:IOException、TimeoutException、RetryExceptioncacheBody:false
cacheBody を true に設定すると、ゲートウェイは本体全体をメモリに読み込みます。これは注意して使用する必要があります。 |
次のリストは、再試行フィルターを構成します。
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: AdaptCachedBodyimport 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 つのルートの例は同等です。
spring:
cloud:
gateway:
mvc:
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,GETForcing Framework Retry Filter
When Spring Retry is on the classpath, the Retry filter uses the Spring Retry-based implementation by default. To force the use of the Framework retry filter instead, set the following property:
spring:
cloud:
gateway:
server:
webmvc:
use-framework-retry-filter: truespring.cloud.gateway.server.webmvc.use-framework-retry-filter=true