正确的方法来实现HTTP连接池 [英] Correct way to implement HTTP Connection Pooling

查看:461
本文介绍了正确的方法来实现HTTP连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我对某些Web服务的REST API调用期间,我正在使用Apache HTTP Client进行连接池.

I am using Apache HTTP Client for connection pooling during my REST API calls into certain web services.

奇怪的是,尽管我使用HTTP连接池,但是性能却没有提高.

Strange thing is that in spite of me using HTTP Connection Pooling there are no gain in my performance.

我正在使用 Apache HTTP客户端连接到我的网站服务,并且代码如下,文档:

I am using Apache HTTP Client to connect to my web services, and the code is as follows from there documentation :

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

cm.setMaxTotal(200);

cm.setDefaultMaxPerRoute(20);

HttpHost host = new HttpHost("abc.com", 80);
cm.setMaxPerRoute(new HttpRoute(host), 50);

CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(cm)
        .build();

我正在使用Spring的RestTemplate来包装使用Spring的HttpComponentsClientHttpRequestFactory的Apache的HttpClient实现.

I am using Spring's RestTemplate to wrap around the HttpClient implemenation of Apache using Spring's HttpComponentsClientHttpRequestFactory.

但是,即使我不使用连接池也是如此.使用Spring的SimpleClientHttpRequestFactory,我没有任何性能优势.

But even if I use no connection pooling ie. use the SimpleClientHttpRequestFactory of Spring, I get no performance advantage.

我的连接仍然需要相同的时间才能完成.

My connections still take the same amount of time to complete.

我是否已完成实现HTTP连接池的正确方法?我在做错什么吗?

请告知我是否需要我提供进一步的信息.

Please let me know if any further info is required from my side.

推荐答案

请注意HTTP客户端池的工作方式,它可能会在短时间内提高性能.检查以下分析:

Beware of how HTTP Client pools work, it may be improving performance during a short period of time. Check the analysis below:

从PoolingHttpClientConnectionManager javadocs

From PoolingHttpClientConnectionManager javadocs

过时的连接的处理在版本4.4中进行了更改.以前,该代码默认情况下会在重新使用之前检查每个连接.现在,该代码仅在自上次使用连接以来经过的时间超过已设置的超时的情况下才检查连接.默认超时设置为2000ms

The handling of stale connections was changed in version 4.4. Previously, the code would check every connection by default before re-using it. The code now only checks the connection if the elapsed time since the last use of the connection exceeds the timeout that has been set. The default timeout is set to 2000ms

从池性能的角度看,这意味着只要管理器认为默认情况下2秒钟内该路由为活动",就可以重用与特定路由的连接.
在闲置2秒钟后,与该路由的连接将被视为陈旧并被丢弃,因此在下次请求该路由时会产生连接初始化损失.

From the pool performance perspective it means that a connection to a particular route will be reused as long as the manager considers that route as "active" during a period of 2 seconds by default.
After 2 seconds of inactivity connections to that route will be considered stale and discarded thus incurring in a connection init penalty next time that route is requested.

换句话说,开箱即用,在第一次连接后的2秒钟内,该池可提高连接的性能.重型路线是最受益的.

In other words, out of the box, the pool improves performance for connections after the first during 2 seconds. Heavy duty routes are the most benefited.

作为一个简单的测试,请将池大小设置为一个较小的值,例如5 max.在Linux上发送5个请求并检查与该路由建立的连接数

As a simple test, set your pool size to an small value, like 5 max. Send 5 requests and check the number of established connections to that route, on linux

watch "netstat -ant | grep <your route IP>"

您应该看到5个连接.等待10或20秒,然后将2个请求发送到同一路由,您应该会看到这5个连接已关闭并且2个新创建. 使用调试日志记录也可以观察到这一点. 此处是很好的参考文章.

You should see 5 connections. Wait 10 or 20 seconds and send 2 requests to the same route, you should see those 5 connections closed and 2 new created. It's also possible to observe that with debug logging. Here is a good article as reference.

这篇关于正确的方法来实现HTTP连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆