[英]Feign client retry on exception
到目前为止,我们有一个 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); }
);
最近我尝试从这个自定义重试器移动到一个内置的假装重试器,如下所示:
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);
理解是假装客户端本身会处理异常,但显然,情况并非如此,分钟客户端抛出5xx
,我得到一个异常,没有重试。 实现重试是否还需要其他东西?
这项服务在 dropwizard 中,git 和 SO 线程主要围绕 spring/ribbon 而我不是这种情况。
深度
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>${feign.version}</version>
</dependency>
如果没有额外的配置,Feign 只会重试IOException
。 如果您希望根据状态代码重试,则需要创建一个ErrorDecoder
来抛出RetryableException
或派生类,以触发重试。
这是一个简单的例子:
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");
}
}
}
有关更多示例,请查看错误处理文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.