正确的方法来实现HTTP连接池

编程入门 行业动态 更新时间:2024-10-15 00:26:59
本文介绍了正确的方法来实现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", 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连接池

本文发布于:2023-11-25 23:38:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:来实现   正确   连接池   方法   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!