《HTTP的KeepAlive是开启还是关闭》,今天这篇文章教你如何配置tomcat服务器和java客户端httpclient来高效的使用keepalive机制"/>

编程小贴士

给你的编程提供小点子


配置TOMCAT及httpClient的keepalive以高效利用长连接

总所 周知http1.1(http1.0不是标准,依服务器而定)是支持长连接的,长连接能够保证服务器和客户端的socket能够高效利用,减少握手等额外 的开销。httpClient在正常情况下会带上Connection: keep-alive表示我是支持长连接的,当完成一个请求后,视情况决定是否关闭连接。

正常情况下,服务器在返回内容的头中会带上如下信息:
tips1000_2014-09-11_23-39-08
但是如果服务器端连接次数计数达到指定值时,则会在返回内容中添加Connection: close信息,表示该连接将被关闭。
tips1000_2014-09-11_23-39-07
这 个时候该connection就失效了,客户端如果下次请求服务器的话,需要重新创建一个新的连接。但是有个问题,是否连接保持是由服务器端决定的,一旦 连接超时导致connection close,服务器是不会专门通知客户端的,所以我们不能依赖Connection: close信息来决定是否关闭socket。我们先来看看tomcat对长连接的配置。下面是一个典型的配置长连接的方式:
tips1000_2014-09-11_23-39-071
其中:
keepAliveTimeout:表示在下次请求过来之前,tomcat保持该连接多久。这就是说假如客户端不断有请求过来,且为超过过期时间,则该连接将一直保持。
maxKeepAliveRequests:表示该连接最大支持的请求数。超过该请求数的连接也将被关闭(此时就会返回一个Connection: close头给客户端)。
由 于keeplive的不确定性,这对客户端高效的实现http请求带来了一定挑战。在httpClient中对连接的复用采用了多种机制同时保证,但是这 需要和服务器端一起配合。如果服务器端和客户端在keepAlive相关的配置不匹配的话,轻则效率低下,重则伤筋动骨,具体有哪些需要注意的地方?下面 一一道来。
http连接池
 
httpClient提供了连接池实现连接的复用,这是实现高效请求的基础,下面是给httpClient设置连接池的方法:
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
//连接池最大生成连接数200
cm.setMaxTotal(200);
// 默认设置route最大连接数为20
cm.setDefaultMaxPerRoute(20);
// 指定专门的route,设置最大连接数为80
HttpHost localhost = new HttpHost(“locahost”, 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 50);
// 创建httpClient
CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(cm)
        .build();
有几个需要注意的地方:
1.如果你的客户端连接的目标服务器只有一个,那么大可设置最大route连接数和最大连接池连接数相同,以便高效利用连接池中创建的连接。
2.创建的httpClient对象是线程安全的,如果连接的目标服务器只有一个的话,创建一个全局对象即可。一个对象好比开了一个浏览器,多个线程无需每次请求时专门开一个浏览器,统一一个即可。
3.如果httpClient对象不再使用,记得关闭,释放与服务器保持连接的socket,以便服务器更高效的释放资源。
连接清理线程
 
虽 然连接池有了,但是由于http连接的特殊性(只有在通讯正在进行(block)时才能够对IO事件做出反应)。一旦连接被放回连接池后,我们无从知道该 连接是否还是keepalive的,且此时也无法监控当前socket的状态(即服务器主动关闭了连接,但客户端没有通讯时是不知道当前连接的状态是怎样 的)。怎么办呢?httpClient采用了一个折中的方案来检查连接的“状态”,就是由客户端自己通过配置去主动关闭其认为是失效的连接。具体方法如 下:
public static class IdleConnectionMonitorThread extends Thread {
    private final HttpClientConnectionManager connMgr;
    private volatile boolean shutdown;
    public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
        super();
        this.connMgr = connMgr;
    }
    @Override
    public void run() {
        try {
            while (!shutdown) {
                synchronized (this) {
                    wait(5000);
                    // Close expired connections
                    connMgr.closeExpiredConnections();
                    // Optionally, close connections
                    // that have been idle longer than 30 sec
                    connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
                }
            }
        } catch (InterruptedException ex) {
            // terminate
        }
    }
    public void shutdown() {
        shutdown = true;
        synchronized (this) {
            notifyAll();
        }
    }
}
我 们新建了一个Thread,该Thread每隔5s进行一次清理,把认为是expired或空闲超过30s的连接给关闭掉。这里关闭空闲的连接好理解。问 题是关闭expired的连接,什么样的连接才算是expired呢?刚才说过客户端是无法检测到对应的连接是否是alive的,故通过一个配置告诉 httpClient每个连接大概什么时候会断掉,httpClient会认为过了该时间的连接就是expired的连接。默认情况 下,httpClient认为只要连接上服务器的socket将永远不会断掉。显然这种假设过于乐观。
keepalive策略
 
为了使connMgr.closeExpiredConnections();起到作用,我们需要指定连接keep alive策略,来告诉httpClient,哪些连接大概什么时候会过期,你可以关闭他们。下面是一个设置keep alive 策略的例子:
ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
    public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
        // Honor ‘keep-alive’ header
        HeaderElementIterator it = new BasicHeaderElementIterator(
                response.headerIterator(HTTP.CONN_KEEP_ALIVE));
        while (it.hasNext()) {
            HeaderElement he = it.nextElement();
            String param = he.getName();
            String value = he.getValue();
            if (value != null && param.equalsIgnoreCase(“timeout”)) {
                try {
                    return Long.parseLong(value) * 1000;
                } catch(NumberFormatException ignore) {
                }
            }
        }
        HttpHost target = (HttpHost) context.getAttribute(
                HttpClientContext.HTTP_TARGET_HOST);
        if (“www.naughty-server.com”.equalsIgnoreCase(target.getHostName())) {
            // Keep alive for 5 seconds only
            return 5 * 1000;
        } else {
            // otherwise keep alive for 30 seconds
            return 30 * 1000;
        }
    }
};
CloseableHttpClient client = HttpClients.custom()
        .setKeepAliveStrategy(myStrategy)
        .build();
这个例子指明在访问www.naughty-server.com和其他未知服务器时的keeplive为一个固定值,如果服务器返回keeplive过期时间,则通过服务器告诉客户端该连接大概什么时候过期(注意这不是一个标准的http协议,不是所有服务器都支持)。
总结
如 果httpClient是访问单一的地址(如rpc请求),通过与服务器配置配合能够更高效的利用客户端的连接池,例如以tomcat为例,设置 maxKeepAliveRequests为-1则可以使该连接可以请求无限次数,而设置keepAliveTimeout为一个较大的时间则可以让客户 端更有效的利用连接池中的连接。凡事都有前提,如果目标tomcat即用于rpc调用,又用于服务正常连接请求的话,则上面优化反而会大量消耗服务器资 源。这时可以讲这两类服务拆分成两个tomcat进行分别处理。另外,如果在服务器前面配有代理的话(apache,nginx),则还需视情况设置代理 的keepAlive策略以提高利用率。

85 Responses to “ 配置TOMCAT及httpClient的keepalive以高效利用长连接 ”

  1. best pron说道:

    RaskDP It as difficult to find knowledgeable people on this subject, however, you sound like you know what you are talking about! Thanks

  2. Wow, great article post.Really thank you! Want more.

  3. My Face Gifts说道:

    Well I sincerely liked studying it. This post offered by you is very useful for accurate planning.

  4. Click to read说道:

    This is one awesome blog article. Want more.

  5. It as difficult to find experienced people for this topic, however, you sound like you know what you are talking about! Thanks

  6. w88world.com说道:

    wow, awesome post.Really thank you! Much obliged.

  7. Thanks for another wonderful post. Where else could anyone get that type of information in such an ideal way of writing? I have a presentation next week, and I am on the look for such information.

  8. These people work together with leap close to they will combined with the boots or shoes nevertheless search great. I truly do think they may be well worth the charge.

  9. Very interesting subject , thankyou for putting up.

  10. Airport taxi说道:

    Thanks a lot for the article post.Much thanks again. Much obliged.

  11. click for more说道:

    Thanks for sharing, this is a fantastic blog post.Really looking forward to read more. Great.

  12. klm voucher说道:

    indeed, analysis is paying off. sure, study is paying off. Take pleasure in the entry you given.. sure, research is paying off.

  13. ebay coupon说道:

    Some really choice blog posts on this internet site , bookmarked.

  14. What as up all, here every person is sharing these kinds of familiarity, thus it as pleasant to read this web site, and I used to pay a visit this website all the time.

  15. Thank you for your article.Much thanks again. Will read on

  16. junk removala说道:

    Normally I do not learn post on blogs, however I wish to say that this write-up very pressured me to take a look at and do so! Your writing style has been amazed me. Thank you, quite nice post.

  17. you ave gotten an awesome weblog right here! would you prefer to make some invite posts on my blog?

  18. tools for sales说道:

    I will immediately snatch your rss as I can not in finding your e-mail subscription link or e-newsletter service. Do you ave any? Please allow me realize so that I could subscribe. Thanks.

  19. giay da nam说道:

    Thanks again for the blog.Really looking forward to read more. Much obliged.

  20. Im obliged for the post.Really looking forward to read more. Much obliged.

  21. very good submit, i actually love this website, carry on it

  22. Supermodel Elena说道:

    this topic to be really something that I think I would never understand.

  23. share说道:

    Im thankful for the article.Really looking forward to read more. Much obliged.

  24. beauty说道:

    What as up everybody, here every person is sharing these kinds of experience, therefore it as pleasant to read this webpage, and I used to visit this web site daily.

  25. This is a good tip especially to those new to the blogosphere. Short but very accurate information Appreciate your sharing this one. A must read post!

  26. Tejido a crochet说道:

    I value the blog post.Thanks Again. Really Great.

  27. It as not that I want to duplicate your web site, but I really like the design. Could you let me know which design are you using? Or was it custom made?

  28. Voyance par mail tirage tarots gratuits en ligne

  29. There as certainly a great deal to learn about this issue. I like all the points you ave made.

  30. Interesting post reminds me of another gem. Adding manpower to a late software project makes it later. Attributed to Laws of Computer Programming

  31. I was recommended this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my trouble. You are amazing! Thanks!

  32. Im thankful for the article.Really looking forward to read more. Great.

  33. negocios说道:

    This is a good tip especially to those fresh to the blogosphere. Brief but very accurate information Many thanks for sharing this one. A must read article!

  34. free browsing说道:

    Thanks a lot for the article.Thanks Again. Awesome.

  35. I see that you simply are making use of WordPress on your blog, wordpress could be the best.~ a-~*

  36. I value the article.Much thanks again. Really Great.

  37. This is a very good tip especially to those fresh to the blogosphere. Simple but very precise info Thanks for sharing this one. A must read article!

  38. This real estate product is a total solution that helps you through every step in the real estate market place, with document management and the best real estate analysis on the market.

  39. Some really excellent posts on this site, regards for contribution.

  40. It as nearly impossible to find experienced people about this subject, but you sound like you know what you are talking about! Thanks

  41. click here说道:

    Very good blog.Much thanks again. Much obliged.

  42. mjagkie okna说道:

    Looking for me, I came here for important information. The information is so incredible that I have to check it out. Nevertheless, thanks.

  43. My brother recommended I might like this blog. He was entirely right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

  44. iHerb Melatonin说道:

    understands what they are discussing on the net.

  45. Your kindness will be tremendously appreciated.

  46. This blog is definitely entertaining and besides factual. I have chosen helluva helpful tips out of this source. I ad love to go back again soon. Thanks a bunch!

  47. see this website说道:

    I think this is a real great blog article.Really looking forward to read more. Keep writing.

  48. There is definately a great deal to know about this topic. I really like all of the points you have made.

  49. Share说道:

    Very nice post. I just stumbled upon your blog and wanted to say

  50. The longest way round is the shortest way home.

  51. see more说道:

    Thanks for sharing, this is a fantastic blog article.Really thank you! Want more. this link

  52. Thanks for the blog article.Thanks Again.

  53. I’m book-marking and will be tweeting this to my followers!

  54. Thank you ever so for you blog post.Much thanks again. Really Cool.

  55. I truly appreciate this blog post.Really looking forward to read more. Keep writing.

  56. I will immediately grasp your rss as I can at in finding your e-mail subscription link or e-newsletter service. Do you ave any? Please let me know so that I could subscribe. Thanks.

  57. This excellent website truly has all the information and facts I needed about this subject and didn at know who to ask.

  58. Sweet blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I ave been trying for a while but I never seem to get there! Thanks

  59. the time to study or pay a visit to the material or websites we ave linked to below the

  60. Game Online说道:

    You have brought up a very wonderful points , appreciate it for the post.

  61. This is one awesome article.Really thank you! Will read on

  62. It as nearly impossible to find experienced people about this topic, but you sound like you know what you are talking about! Thanks

  63. You have a good blog and I liked this post. I will be looking forward to your future posts. Keep up the good work.

  64. This particular blog is obviously interesting as well as amusing. I have chosen a lot of interesting things out of this amazing blog. I ad love to return over and over again. Thanks!

  65. Some really excellent information, Gladiola I observed this.

  66. There is apparently a lot to identify about this. I assume you made various good points in features also.

  67. clothing store说道:

    Incredible points. Sound arguments. Keep up the good spirit.

  68. sprinted down the street to one of the button stores

  69. Well I sincerely enjoyed reading it. This tip procured by you is very constructive for correct planning.

  70. Really excellent info can be found on website.

  71. Would you be eager about exchanging links?

  72. use this link说道:

    logiciel lire mkv logiciel amplificateur wifi

  73. kids clothes说道:

    Perfectly composed articles , regards for selective information.

  74. mealcoupons说道:

    Mikha Tambayong The Official Site Peluncuran Film Senandung Bumi

  75. see pron说道:

    qGvnGh Wow, superb blog layout! How long have you been blogging for?

  76. see pron说道:

    JFHHNt you ave gotten an ideal weblog right here! would you like to make some invite posts on my weblog?

  77. You have observed very interesting points! ps decent site.

  78. Really enjoyed this blog.Really thank you! Want more.

  79. Wow, awesome blog layout! How lengthy have you been blogging for? you make blogging glance easy. The full glance of your web site is magnificent, let alone the content!

  80. This piece of writing about Search engine marketing is genuinely fastidious one, and the back links are actually very helpful to market your site, its also referred to as SEO.

  81. I value the post.Really thank you! Fantastic.

  82. Wow, wonderful weblog format! How long have you been running a blog for? you make blogging glance easy. The overall glance of your site is fantastic, let alone the content material!

  83. Very good article.Really thank you! Cool.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>