【问题标题】:Is it possible to set RetryPolicy in spring-retry based on HttpStatus status code?是否可以根据 HttpStatus 状态码在 spring-retry 中设置 RetryPolicy?
【发布时间】:2015-01-29 22:05:09
【问题描述】:

是否可以根据错误状态码在spring retry(https://github.com/spring-projects/spring-retry)中设置RetryPolicy?例如我想用HttpStatus.INTERNAL_SERVER_ERROR 状态码重试HttpServerErrorException,即503。因此它应该忽略所有其他错误代码——[500 - 502] 和[504 - 511]。

【问题讨论】:

  • 不是直接的,但是如果您可以提供更多有关如何调用服务器的上下文(例如,如果您使用 Spring Integration 出站网关,或者您是否直接从您的代码),我们也许可以提出解决方案。
  • 我扩展了 RestTemplate 并用 RetryTemplate 覆盖了一些围绕它们的方法。我正在关注上面 github 链接上给出的示例,类似于... SimpleRetryPolicy policy = new SimpleRetryPolicy(); policy.setMaxAttempts(5); policy.setRetryableExceptions(new Class[] {HttpServerErrorException.class}); Spring restTemplate 报告 http 状态错误代码 500 - 511 的“HttpServerErrorException.class”,但我想在 503 和 504 上重试。
  • 现在,我在 doWithRetry(RetryContext context) 中从 RetryContext 中提取 throwable 并读取错误消息 - context.getLastThrowable().getMessage(),然后查找 503 或 504。必须我认为这是一种更好的方法。
  • @MaratKurbanov,你能帮我们做同样的事情吗,根据特定的http状态码重试
  • @GaryRussell 我正在使用 Spring Integration 出站网关来调用服务器。你能提出一个解决方案吗?

标签: spring spring-retry


【解决方案1】:

您也可以在 SinmpleRetryPolicy 的 retryableExceptions 列表中添加具体的错误代码。

Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>();
retryableExceptions.put(HttpClientErrorException.Unauthorized.class, true);
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(5, retryableExceptions));

【讨论】:

    【解决方案2】:

    对于面临同样问题的其他人,我发布了这个答案。 实现自定义重试策略如下:

    class InternalServerExceptionClassifierRetryPolicy extends ExceptionClassifierRetryPolicy {
    public InternalServerExceptionClassifierRetryPolicy() {
        final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy();
        simpleRetryPolicy.setMaxAttempts(3);
    
        this.setExceptionClassifier(new Classifier<Throwable, RetryPolicy>() {
            @Override
            public RetryPolicy classify(Throwable classifiable) {
                if (classifiable instanceof HttpServerErrorException) {
                    // For specifically 500 and 504
                    if (((HttpServerErrorException) classifiable).getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR
                            || ((HttpServerErrorException) classifiable)
                                    .getStatusCode() == HttpStatus.GATEWAY_TIMEOUT) {
                        return simpleRetryPolicy;
                    }
                    return new NeverRetryPolicy();
                }
                return new NeverRetryPolicy();
            }
        });
    }}
    

    Ans 简单地调用它如下:

    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(new InternalServerExceptionClassifierRetryPolicy())
    

    【讨论】:

    • 只是一个重要的说明,我花了一些时间来弄清楚 + 调试。策略必须在 lambda 之外创建,就像在示例中一样(即不要尝试在内部 if 中返回 New SimpleRetryPolicy(3)),因为如果不是,它会进入无限循环(即不遵守最大尝试次数) .希望对您有所帮助。
    【解决方案3】:

    RestTemplatesetErrorHandler 选项,DefaultResponseErrorHandler 是默认选项。

    它的代码如下:

    public void handleError(ClientHttpResponse response) throws IOException {
        HttpStatus statusCode = getHttpStatusCode(response);
        switch (statusCode.series()) {
            case CLIENT_ERROR:
                throw new HttpClientErrorException(statusCode, response.getStatusText(),
                        response.getHeaders(), getResponseBody(response), getCharset(response));
            case SERVER_ERROR:
                throw new HttpServerErrorException(statusCode, response.getStatusText(),
                        response.getHeaders(), getResponseBody(response), getCharset(response));
            default:
                throw new RestClientException("Unknown status code [" + statusCode + "]");
        }
    }
    

    因此,您可以为该方法提供自己的实现,以简化您的 RetryPolicy 周围所需的状态代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 2017-07-18
      相关资源
      最近更新 更多