HTTP长连接与短连接使用方法及测试详解
http短连接(非持久连接)是指,客户端和服务端进行一次http请求/响应之后,就关闭连接。所以,下一次的http请求/响应操作就需要重新建立连接。
http长连接(持久连接)是指,客户端和服务端建立一次连接之后,可以在这条连接上进行多次请求/响应操作。持久连接可以设置过期时间,也可以不设置。
我为什么没有说http/1.0 默认短连接,http/1.1起,默认长连接呢?因为我第一次看这个说法的时候,以为自己懂了,其实并没有懂。长短连接操作上有什么区别,有的地方出现的持久连接又是怎么回事?
使用设置
这里的设置,我们都以http1.1协议为例子。
设置http短连接
在首部字段中设置connection:close,则在一次请求/响应之后,就会关闭连接。
设置http长连接,有过期时间
在首部字段中设置connection:keep-alive 和keep-alive: timeout=60,表明连接建立之后,空闲时间超过60秒之后,就会失效。如果在空闲第58秒时,再次使用此连接,则连接仍然有效,使用完之后,重新计数,空闲60秒之后过期。
设置http长连接,无过期时间
在首部字段中只设置connection:keep-alive,表明连接永久有效。
实现原理
了解怎么设置之后,就开始用起来。然而,问题来了。在请求头中设置connection:keep-alive,为什么连接空闲一段时间之后,还是断开了呢?这是因为connection字段只有服务端设置才有效。
http操作是请求/响应成对出现的,即先有客户端发出请求,后有服务端处理请求。所以,一次http操作的终点操作在服务端上,关闭也是由服务端发起的。
接下来我们做做测试,以及show code。下面的测试都是使用spring resttemplate,封装apache http client进行的。为方便讲解代码,先说明长连接的情况,最后再对其他形式做测试总结。
客户端连接失效时间大于服务端失效时间
如下,为请求日志。客户端设置connection: keep-alive和keep-alive: timeout=60, 服务端设置connection: keep-alive和keep-alive: timeout=5。
## 客户端设置有效期为60s [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "post /adx-api/api/creative/upload http/1.1[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "accept: application/json, application/*+json, text/html, application/json, text/javascript[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "content-type: application/json;charset=utf-8[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "user-agent: mozilla/5.0 (windows nt 6.1) applewebkit/537.36 (khtml, like gecko) chrome/31.0.1650.16 safari/537.36[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "accept-encoding: gzip,deflate[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "accept-language: zh-cn[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "connection: keep-alive[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "keep-alive: timeout=60[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "content-length: 396[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "host: bizdomain[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "request data" ##服务端设置有效期为5s [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "http/1.1 200 ok[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "date: wed, 26 apr 2017 06:07:58 gmt[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "server: apache-coyote/1.1[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "content-type: text/html;charset=utf-8[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "keep-alive: timeout=5, max=100[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "connection: keep-alive[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "transfer-encoding: chunked[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "63[\r][\n]" [2017-04-26 14:08:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "response data"
客户端设置的有效期大于服务端的,那么实际连接的有效期呢?三分钟之后再次请求,从连接池中lease连接的时候,提示connection expired @ wed apr 26 14:08:05,即在上一次请求之后的5s失效,说明是服务端的设置生效了。
[2017-04-26 14:11:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection request: [route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200] [2017-04-26 14:11:00 debug] (org.apache.http.impl.conn.cpool:?) - connection [id:2][route:{}->http://bizdomain:80][state:null] expired @ wed apr 26 14:08:05 gmt+08:00 2017
源码分析
通过源代码了解一下连接失效时间的设置过程。
//org.apache.http.impl.execchain.mainclientexec#execute ...... //从连接池中lease connection final httpclientconnectionmanagedconn = connrequest.get(timeout > 0 ? timeout : 0, timeunit.milliseconds); ...... //将conenction封装在connectionholder中 final connectionholder connholder = new connectionholder(this.log, this.connmanager, managedconn); ...... // the connection is in or can be brought to a re-usable state. //如果返回值消息头中connection设置为close,则返回false if (reusestrategy.keepalive(response, context)) { // set the idle duration of this connection //取出response消息头中,keep-alive的timeout值 final long duration = keepalivestrategy.getkeepaliveduration(response, context); if (this.log.isdebugenabled()) { final string s; if (duration > 0) { s = "for " + duration + " " + timeunit.milliseconds; } else { s = "indefinitely"; } this.log.debug("connection can be kept alive " + s); } //设置失效时间 connholder.setvalidfor(duration, timeunit.milliseconds); connholder.markreusable(); } else { connholder.marknonreusable(); }
待读取响应之后,释放连接,即:connholder.releaseconnection()。调用org.apache.http.impl.conn.poolinghttpclientconnectionmanager#releaseconnection方法。
@override public void releaseconnection(final httpclientconnection managedconn, final object state,final long keepalive, final timeunit tunit) { args.notnull(managedconn, "managed connection"); synchronized (managedconn) { final cpoolentry entry = cpoolproxy.detach(managedconn); if (entry == null) { return; } final managedhttpclientconnection conn = entry.getconnection(); try { if (conn.isopen()) { final timeunit effectiveunit = tunit != null ? tunit : timeunit.milliseconds; entry.setstate(state); //设置失效时间 entry.updateexpiry(keepalive, effectiveunit); } } finally { 。。。。。。 } } } }
然后再下一次http操作,从连接池中获取连接时
//org.apache.http.impl.conn.poolinghttpclientconnectionmanager#requestconnection调用org.apache.http.pool.abstractconnpool#lease, //调用getpoolentryblocking,调用org.apache.http.impl.conn.cpoolentry#isexpired @override public boolean isexpired(final long now) { final boolean expired = super.isexpired(now); if (expired && this.log.isdebugenabled()) { //日志中看到的内容 this.log.debug("connection " + this + " expired @ " + new date(getexpiry())); } return expired; }
综上,连接的实际有效时间,是根据response的设置来决定的。
其他情况测试
客户端设置connection: close
##connection:close请求,kept alive的连接为0 [2017-04-26 13:57:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection request: [route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200] [2017-04-26 13:57:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection leased: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 200] [2017-04-26 13:57:00 debug] (org.apache.http.impl.execchain.mainclientexec:?) - opening connection {}->http://bizdomain:80 [2017-04-26 13:57:00 debug] (org.apache.http.impl.conn.defaulthttpclientconnectionoperator:?) - connecting to bizdomain/127.0.0.195:80 ## 建立新连接 [2017-04-26 13:57:00 debug] (org.apache.http.impl.conn.defaulthttpclientconnectionoperator:?) - connection established 127.0.0.191:49239<->127.0.0.195:80 ## 客户端设置短连接 [2017-04-26 13:57:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "connection: close[\r][\n]" ## 服务端返回的也是短连接 [2017-04-26 13:57:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "connection: close[\r][\n]" ##请求完之后,关闭连接 [2017-04-26 13:57:00 debug] (org.apache.http.impl.conn.defaultmanagedhttpclientconnection:?) - http-outgoing-0: close connection [2017-04-26 13:57:00 debug] (org.apache.http.impl.execchain.mainclientexec:?) - connection discarded [2017-04-26 13:57:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection released: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200]
如上,当服务端返回connection: close时,客户端接收完响应,便会关闭连接。
客户端设置60s超时,服务端设置5s超时
##keep-alive: timeout=60 第一次请求,与connection:close无差别 [2017-04-26 10:57:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection request: [route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 0 of 32; total allocated: 0 of 200] [2017-04-26 10:57:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection leased: [id: 0][route: {}->http://bizdomain:80][total kept alive: 0; route allocated: 1 of 32; total allocated: 1 of 200] [2017-04-26 10:57:00 debug] (org.apache.http.impl.execchain.mainclientexec:?) - opening connection {}->http://bizdomain:80 ## 客户端设置超时时间60s [2017-04-26 10:57:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "connection: keep-alive[\r][\n]" [2017-04-26 10:57:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "keep-alive: timeout=60[\r][\n]" ## 服务端设置超时时间5s [2017-04-26 10:57:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "keep-alive: timeout=5, max=100[\r][\n]" [2017-04-26 10:57:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "connection: keep-alive[\r][\n]" ## 服务端设置生效,连接可以保持5s [2017-04-26 10:57:00 debug] (org.apache.http.impl.execchain.mainclientexec:?) - connection can be kept alive for 5000 milliseconds [2017-04-26 10:57:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection [id: 0][route: {}->http://bizdomain:80] can be kept alive for 5.0 seconds [2017-04-26 10:57:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection released: [id: 0][route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200] ##keep-alive: timeout=60 非第一次请求 [2017-04-26 14:11:00 debug] (org.apache.http.impl.conn.poolinghttpclientconnectionmanager:?) - connection request: [route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200] ## 连接在上一次请求结束后5s失效 [2017-04-26 14:11:00 debug] (org.apache.http.impl.conn.cpool:?) - connection [id:2][route:{}->http://bizdomain:80][state:null] expired @ wed apr 26 14:10:05 gmt+08:00 2017
客户端设置失效时间,服务端设置不失效
## 客户端设置30s超时 [2017-04-26 17:45:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "connection: keep-alive[\r][\n]" [2017-04-26 17:45:00 debug] (org.apache.http.wire:?) - http-outgoing-0 >> "keep-alive: timeout=30[\r][\n]" ## 服务端设置永久连接 [2017-04-26 17:45:00 debug] (org.apache.http.wire:?) - http-outgoing-0 << "connection: keep-alive[\r][\n]" ## 连接将一直保持 [2017-04-26 17:45:00 debug] (org.apache.http.impl.execchain.mainclientexec:?) - connection can be kept alive indefinitely
综上,http连接保持时间是由服务端的消息头connection字段和keep-alive字段定的。
在上面前两种情况,请求的是同一个服务端,那么为什么一个返回的是短连接,一个返回的是长连接呢?这里转一下 这篇文章的解释:
不论request还是response的header中包含了值为close的connection,都表明当前正在使用的tcp链接在请求处理完毕后会被断掉。以后client再进行新的请求时就必须创建新的tcp链接了。 http connection的 close设置允许客户端或服务器中任何一方关闭底层的连接,双方都会要求在处理请求后关闭它们的tcp连接。
补充
tcp长短连接
在网上搜资料的时候,看到很多“http协议的长连接和短连接,实质上是tcp协议的长连接和短连接”。 http和tcp是不同两层的东西,它们怎么会是一样的呢?http是请求/响应模式的,就是说我们发一个请求一定要有一个回应。最直观的就是,浏览器上发请求,得不到响应就会一直转圈圈。 而tcp并不是一定要有响应。大家以前使用socket模拟一个im聊天,a跟b打完招呼,完全可以不用等待b的回应,就自己关掉连接的。
tcp keep-alive
另外还有http协议的keep-alive和tcp的keep-alive含义是有差别的。http的keep-alive是为了维持连接,以便复用连接。通过使用keep-alive机制,可以减少tcp连接建立次数,也意味着可以减少time_wait状态连接,以此提高性能和提高httpd服务器的吞吐率(更少的tcp连接意味着更少的系统内核调用,socket的accept()和close()调用)。但是,长时间的tcp连接容易导致系统资源无效占用。配置不当的keep-alive,有时比重复利用连接带来的损失还更大。
而tcp keep-alive是tcp的一种检测tcp连接状况的机制,涉及到三个参数tcp_keepalive_time, tcp_keepalive_intvl, tcp_keepalive_probes。
当网络两端建立了tcp连接之后,闲置(双方没有任何数据流往来)了tcp_keepalive_time后,服务器内核就会尝试向客户端发送侦测包,来判断tcp连接状况(有可能客户端崩溃、强制关闭了应用、主机不可达等等)。如果没有收到对方的回答(ack包),则会在 tcp_keepalive_intvl后再次尝试发送侦测包,直到收到对方的ack。如果一直没有收到对方的ack,一共会尝试 tcp_keepalive_probes次。如果尝试tcp_keepalive_probes,依然没有收到对方的ack包,则会丢弃该tcp连接。tcp连接默认闲置时间是2小时,一般设置为30分钟足够了。
上一篇: Python操作串口的方法
下一篇: PHP模版引擎原理、定义与用法实例