Feign 客户端重试异常

Noo*_*tor 6 java dropwizard feign openfeign

到目前为止,我们有一个 feign 客户端,如果出现异常,我们曾经重试如下

Retryer<ClientResponse> retryer = RetryerBuilder.<ClientResponse>newBuilder()
  .retryIfExceptionOfType(FeignException.class)
  .withStopStrategy(StopStrategies.stopAfterAttempt(retryCount))
  .withWaitStrategy(WaitStrategies.exponentialWait(maxWaitSeconds, TimeUnit.SECONDS))
  .build();
    
retryer.call(() -> { 
  return client.doStuffs(someInput); }
);
Run Code Online (Sandbox Code Playgroud)

最近我尝试从这个自定义重试器移动到一个内置的假装重试器,如下所示:

Feign client = Feign.builder()
    .decoder(jacksonDecoder)
    .encoder(jacksonEncoder)
    .logger(slf4jLogger)
    .client(okHttpClient)
    .retryer(new Retryer.Default(
                            SECONDS.toMillis(minWaitSeconds), 
                            SECONDS.toMillis(maxWaitSeconds), 
                            retryCount
            ))
    .requestInterceptor(new BasicAuthRequestInterceptor(clientConfig.getUser(), clientConfig.getPassword()))
    .target(target);
    
client.doStuffs(someInput);
Run Code Online (Sandbox Code Playgroud)

理解是假装客户端本身会处理异常,但显然,情况并非如此,当客户端抛出 a 时5xx,我得到一个异常而没有重试。实现重试是否还需要其他东西?

这项服务在 dropwizard 中,git 和 SO 线程主要围绕 spring/ribbon 而我不是这种情况。

深度

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-core</artifactId>
    <version>${feign.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

Kev*_*vis 14

无需额外配置,Feign 只会重试IOExceptions。如果您希望根据状态代码重试,则需要创建一个ErrorDecoder抛出 aRetryableException或衍生的 ,以触发重试。

这是一个简单的例子:

class MyErrorDecoder implements ErrorDecoder {
    public Exception decode(String methodKey, Response response) {
        if (response.status() == 503) {
            throw new RetryableException(
                response.status(), 
                "Service Unavailable", 
                response.request().httpMethod(), 
                null);
        } else {
            return new RuntimeException("error");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有关更多示例,请查看错误处理文档。