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

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

问题描述

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

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 Client 连接到我的网站服务,代码如下文档 :

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 的 HttpComponentsClientHttpRequestFactoryHttpClient Apache 实现.

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.在 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 <你的路由IP>"

您应该看到 5 个连接.等待 10 或 20 秒,然后向同一路由发送 2 个请求,您应该看到这 5 个连接已关闭并创建了 2 个新连接.也可以通过调试日志来观察.这里是一篇不错的文章,可以参考.
Apache 官方文档关于 http 日志记录.

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.
Apache official docs on http logging.

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

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