使用Backoff策略提高HttpClient連接管理的效率
序
本文主要研究一下HttpClient的ConnectionBackoffStrategy
ConnectionBackoffStrategy
org/apache/http/client/ConnectionBackoffStrategy.java
/**
* When managing a dynamic number of connections for a given route, this
* strategy assesses whether a given request execution outcome should
* result in a backoff signal or not, based on either examining the
* {@code Throwable} that resulted or by examining the resulting
* response (e.g. for its status code).
*
* @since 4.2
*
*/
public interface ConnectionBackoffStrategy {
/**
* Determines whether seeing the given {@code Throwable} as
* a result of request execution should result in a backoff
* signal.
* @param t the {@code Throwable} that happened
* @return {@code true} if a backoff signal should be
* given
*/
boolean shouldBackoff(Throwable t);
/**
* Determines whether receiving the given {@link HttpResponse} as
* a result of request execution should result in a backoff
* signal. Implementations MUST restrict themselves to examining
* the response header and MUST NOT consume any of the response
* body, if any.
* @param resp the {@code HttpResponse} that was received
* @return {@code true} if a backoff signal should be
* given
*/
boolean shouldBackoff(HttpResponse resp);
}ConnectionBackoffStrategy定義了shouldBackoff方法,它根據(jù)異?;蛘遰esponse來(lái)進(jìn)行判斷
NullBackoffStrategy
org/apache/http/impl/client/NullBackoffStrategy.java
public class NullBackoffStrategy implements ConnectionBackoffStrategy {
@Override
public boolean shouldBackoff(final Throwable t) {
return false;
}
@Override
public boolean shouldBackoff(final HttpResponse resp) {
return false;
}
}NullBackoffStrategy實(shí)現(xiàn)了ConnectionBackoffStrategy,shouldBackoff方法返回false
DefaultBackoffStrategy
org/apache/http/impl/client/DefaultBackoffStrategy.java
public class DefaultBackoffStrategy implements ConnectionBackoffStrategy {
@Override
public boolean shouldBackoff(final Throwable t) {
return t instanceof SocketTimeoutException || t instanceof ConnectException;
}
@Override
public boolean shouldBackoff(final HttpResponse resp) {
return resp.getStatusLine().getStatusCode() == 429 ||
resp.getStatusLine().getStatusCode() == HttpStatus.SC_SERVICE_UNAVAILABLE;
}
}DefaultBackoffStrategy在SocketTimeoutException或者ConnectException的時(shí)候返回true,或者在response code為429或者503的時(shí)候返回true
BackoffStrategyExec
org/apache/http/impl/execchain/BackoffStrategyExec.java
@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
public class BackoffStrategyExec implements ClientExecChain {
private final ClientExecChain requestExecutor;
private final ConnectionBackoffStrategy connectionBackoffStrategy;
private final BackoffManager backoffManager;
public BackoffStrategyExec(
final ClientExecChain requestExecutor,
final ConnectionBackoffStrategy connectionBackoffStrategy,
final BackoffManager backoffManager) {
super();
Args.notNull(requestExecutor, "HTTP client request executor");
Args.notNull(connectionBackoffStrategy, "Connection backoff strategy");
Args.notNull(backoffManager, "Backoff manager");
this.requestExecutor = requestExecutor;
this.connectionBackoffStrategy = connectionBackoffStrategy;
this.backoffManager = backoffManager;
}
@Override
public CloseableHttpResponse execute(
final HttpRoute route,
final HttpRequestWrapper request,
final HttpClientContext context,
final HttpExecutionAware execAware) throws IOException, HttpException {
Args.notNull(route, "HTTP route");
Args.notNull(request, "HTTP request");
Args.notNull(context, "HTTP context");
CloseableHttpResponse out = null;
try {
out = this.requestExecutor.execute(route, request, context, execAware);
} catch (final Exception ex) {
if (out != null) {
out.close();
}
if (this.connectionBackoffStrategy.shouldBackoff(ex)) {
this.backoffManager.backOff(route);
}
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
if (ex instanceof HttpException) {
throw (HttpException) ex;
}
if (ex instanceof IOException) {
throw (IOException) ex;
}
throw new UndeclaredThrowableException(ex);
}
if (this.connectionBackoffStrategy.shouldBackoff(out)) {
this.backoffManager.backOff(route);
} else {
this.backoffManager.probe(route);
}
return out;
}
}BackoffStrategyExec實(shí)現(xiàn)了ClientExecChain接口,其execute執(zhí)行requestExecutor.execute,捕獲到異常的時(shí)候通過(guò)connectionBackoffStrategy.shouldBackoff(ex)來(lái)決定是否需要backOff,是的話執(zhí)行backoffManager.backOff(route);
若沒(méi)有異常則通過(guò)connectionBackoffStrategy.shouldBackoff(out)根據(jù)response來(lái)判斷是否需要backOff,是的化執(zhí)行backoffManager.backOff(route)
小結(jié)
HttpClient的DefaultBackoffStrategy在SocketTimeoutException或者ConnectException的時(shí)候返回true,或者在response code為429或者503的時(shí)候返回true;BackoffStrategyExec則通過(guò)connectionBackoffStrategy與backoffManager來(lái)配合執(zhí)行backOff。這個(gè)backOff的目的就是動(dòng)態(tài)調(diào)整每個(gè)route的connection大小(MaxPerRoute)。
以上就是使用Backoff策略提高HttpClient連接管理的效率的詳細(xì)內(nèi)容,更多關(guān)于HttpClient Backoff連接管理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 解讀httpclient的validateAfterInactivity連接池狀態(tài)檢測(cè)
- httpclient的disableConnectionState方法工作流程
- 探索HttpClient中的close方法及其對(duì)連接的影響
- HttpClient的RedirectStrategy重定向處理核心機(jī)制
- HttpClient的DnsResolver自定義DNS解析另一種選擇深入研究
- HttpClient HttpRoutePlanner接口確定請(qǐng)求目標(biāo)路由
- 提升網(wǎng)絡(luò)請(qǐng)求穩(wěn)定性HttpClient的重試機(jī)制深入理解
- httpclient getPoolEntryBlocking連接池方法源碼解讀
相關(guān)文章
springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫(kù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
使用Java讀取本地文件并轉(zhuǎn)換為MultipartFile對(duì)象的方法
在許多Java Web應(yīng)用中,我們經(jīng)常會(huì)遇到將本地文件上傳至服務(wù)器或其他系統(tǒng)的需求,在這種場(chǎng)景下,MultipartFile對(duì)象非常常用,用來(lái)表示HTTP請(qǐng)求中的文件,在本文中,我將演示如何編寫代碼來(lái)讀取本地文件并將其轉(zhuǎn)換為自定義的MultipartFile對(duì)象,需要的朋友可以參考下2025-08-08
spring如何實(shí)現(xiàn)依賴注入DI(spring-test方式)
本文主要介紹如何實(shí)現(xiàn)spring 的依賴注入,并且淺顯的講述一下注入需要注意的事項(xiàng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
java中response對(duì)象用法實(shí)例分析
這篇文章主要介紹了java中response對(duì)象用法,結(jié)合實(shí)例形式分析了Java中response對(duì)象的功能及具體使用技巧,需要的朋友可以參考下2015-12-12
Java實(shí)現(xiàn)高校教務(wù)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)高校教務(wù)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
spring整合atomikos實(shí)現(xiàn)分布式事務(wù)的方法示例
本文整合了一個(gè)spring和atomikos的demo,并且通過(guò)案例演示說(shuō)明atomikos的作用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
SpringBoot統(tǒng)一功能處理示例詳解(攔截器)
這篇文章主要介紹了SpringBoot統(tǒng)一功能處理(攔截器),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
JAVA JDK8 List分組的實(shí)現(xiàn)和用法
今天小編就為大家分享一篇關(guān)于JAVA JDK8 List分組的實(shí)現(xiàn)和用法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12

