Retry Filter

Retry 过滤器支持以下参数:

The Retry filter supports the following parameters:

  • retries: The number of retries that should be attempted.

  • methods: The HTTP methods that should be retried, represented by using org.springframework.http.HttpMethod.

  • series: The series of status codes to be retried, represented by using org.springframework.http.HttpStatus.Series.

  • exceptions: A list of thrown exceptions that should be retried.

为启用的 Retry 过滤器配置以下默认值:

The following defaults are configured for Retry filter, if enabled:

  • retries: Three times

  • series: 5XX series

  • methods: GET method

  • exceptions: IOException, TimeoutException and RetryException

以下列表配置一个 Retry 过滤器:

The following listing configures a Retry filter:

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) 返回值),重试过滤器可以配置为通过重试来处理该信号。

When using the retry filter with a forward: prefixed URL, the target endpoint should be written carefully so that, in case of an error, it does not do anything that could result in a response being sent to the client and committed. For example, if the target endpoint is an annotated controller, the target controller method should not return ResponseEntity with an error status code. Instead, it should throw an Exception or signal an error (for example, through a Mono.error(ex) return value), which the retry filter can be configured to handle by retrying.

可以为单个 statusmethod 添加简化的“快捷方式”表示法。

A simplified "shortcut" notation can be added with a single status and method.

以下两个示例是等效的:

The following two examples are equivalent:

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