有人可以分享如何配置现代 HttpClient 4.5.3 以重试失败的请求并在每次重试之前等待一段时间吗?
到目前为止看来我的理解是正确的.setRetryHandler(new DefaultHttpRequestRetryHandler(X, false))
将允许重试请求 X 次。
但我不明白如何配置退避:.setConnectionBackoffStrategy()
/ .setBackoffManager()
根据 JavaDocs 规范其他内容,而不是重试之间的超时。
关于动态延迟,我想建议的是:
CloseableHttpClient client = HttpClientBuilder.create()
.setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
return executionCount <= maxRetries ;
}
})
.setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {
int waitPeriod = 100;
@Override
public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
waitPeriod *= 2;
return executionCount <= maxRetries &&
response.getStatusLine().getStatusCode() >= 500; //important!
}
@Override
public long getRetryInterval() {
return waitPeriod;
}
})
.build();
附录:
请注意,如果出现超时、端口未打开或连接关闭等 IO 错误,则不会调用 ServiceUnavailableRetryStrategy.retryRequest。在这种情况下,只会调用 HttpRequestRetryHandler.retryRequest,并且重试将立即发生或在固定延迟之后发生(我最终无法澄清这一点)。所以奥列格的答案实际上是正确的。没有办法在 HttpClient 4.5 的支持下做到这一点。
(我实际上想将此称为设计错误,因为 IO 错误后的延迟重试在现代微服务环境中至关重要。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)