如何在apache异步http客户端中设置重定向策略 [英] How to set redirect strategy in apache async http client
问题描述
如何在 apache 异步 http 客户端中设置重定向策略?我有这样的东西(scala 代码).注释代码按预期工作,但我每秒无法对一台主机执行超过 4 个并发请求,第二个版本可以处理更多并发连接,但根本不处理重定向.
How i can set redirect strategy in apache async http client? I have something like this (scala code). Commented code works as expected, but i am not able to perform more than 4 concurrent request to one host per second, second version can handle much more concurrent connections but doesn't handle redirects at all.
object HttpClientManager {
def createHttpClient(): CloseableHttpAsyncClient = { //cm: NHttpClientConnectionManager
/*
val httpClient = HttpAsyncClients
.custom()
.setDefaultRequestConfig(config)
//.setConnectionManager(cm)
.build()
*/
// val config = RequestConfig.custom()
// .setSocketTimeout(3000)
// .setConnectTimeout(3000).build();
val socketConfig = SocketConfig.custom()
.setSoTimeout(15000)
.build();
val connectionConfig = ConnectionConfig.custom()
.setBufferSize(8 * 1024)
.setFragmentSizeHint(8 * 1024)
.build();
val ioreactor = new DefaultConnectingIOReactor();
val mgr = new PoolingNHttpClientConnectionManager(ioreactor);
mgr.setDefaultSocketConfig(socketConfig);
mgr.setDefaultConnectionConfig(connectionConfig);
mgr.setDefaultMaxPerRoute(100)
mgr.setMaxTotal(200)
val httpclient = HttpAsyncClients.createMinimal(mgr);
httpclient.start()
httpclient
}
}
推荐答案
CloseableHttpAsyncClient client = HttpAsyncClients.custom()
.setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
.build();
由 HttpAsyncClients#createMinimal
创建的最小客户端使用与其成熟"对应物完全相同的连接管理代码.它与它的不同之处在于仅提供最小的协议管道,以便在人们准备牺牲非必要协议方面的情况下提供更好的性能:代理支持、重定向、身份验证和状态管理.因此,最小实现根本不处理重定向.
Minimal client created by HttpAsyncClients#createMinimal
uses absolutely the same connection management code as its 'full-blown' counterpart. It differs from it though in providing only a minimal protocol pipeline in order to provide better performance in those scenarios when people are prepared to sacrifice non-essential protocol aspects: proxy support, redirect, authentication and state management. So, minimal implementation simply does not handle redirects.
这篇关于如何在apache异步http客户端中设置重定向策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!