为什么Apache HttpAsyncClient每秒仅发送10个请求。我配置有问题吗?这是我启动asyncClient的方式:
PoolingNHttpClientConnectionManager connManager = null;
try {
if (client != null && client.isRunning()) {
client.close();
}
TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> socketFactoryRegistry =
RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslsf).build();
Registry<SchemeIOSessionStrategy> socketRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create()
.register("http", NoopIOSessionStrategy.INSTANCE)
.register("https", new SSLIOSessionStrategy(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER))
.build();
connManager = new PoolingNHttpClientConnectionManager(
new DefaultConnectingIOReactor( IOReactorConfig.custom()
.setConnectTimeout( connectionTimeout * 1000 ) //connectTimeout
.setSoTimeout( readTimeout * 1000 ) //readTimeout
.setIoThreadCount(10000)
.build() ), socketRegistry );
} catch (Exception e) {}
client = HttpAsyncClients.custom()
.setMaxConnPerRoute( maxConnsPerRoute )
.setConnectionManager( connManager )
.setMaxConnTotal( maxConnections )
.setKeepAliveStrategy( DefaultConnectionKeepAliveStrategy.INSTANCE )
.build();
client.start();
这是我的使用方式:
for(int i = 0; i < 100; i++) {
client.execute(request.getHttpPost(), null);
}
如何每秒获得更多请求?
答案 0 :(得分:1)
所有版本的Apache HttpClient可以轻松地每秒生成数万个请求。
https://github.com/ok2c/httpclient-benchmark/wiki
性能问题可能与服务器代码或您的应用程序代码有关。
请勿执行此操作。太厉害了一个人使用的I / O调度线程不应超过系统上的CPU内核。除非有充分的理由,否则不要覆盖默认值。
.setIoThreadCount(10000) // do not do this
如果手动设置一个连接管理器,则所有连接管理参数均无效。代码中的maxConnsPerRoute
和maxConnections
值均无效。您需要将它们直接应用于连接管理器。
.setConnectionManager( connManager ) // if you do this
.setMaxConnPerRoute( maxConnsPerRoute ) // this has no effect
.setMaxConnTotal( maxConnections )
答案 1 :(得分:0)
非常感谢Okta,根据他的指导,我将此代码添加到了我的项目中,问题得以解决:
connManager.setMaxTotal(10);
connManager.setDefaultMaxPerRoute(10);
HttpHost host = new HttpHost("75.10.91.11", 8443, "https");
connManager.setMaxPerRoute(new HttpRoute(host), 10);
您可以在baeldung.com
中找到完整的说明