运维发现服务器有大量连接不释放,而且每次app访问都会建立新连接。
netstat -antlp |grep ESTAB|grep 8080|wc -l (访问服务器8080端口的已建立的连接数)
后来用浏览器测试没问题。接着发现终端连接时,设置了http header: Connection: keep-alive 。但是同样用postman模拟,也没问题。最终发现应该是app写了keepalive,连接调用时确没有用以前打开的连接,而是新建一个连接。
而服务器端wildfly默认打开了keepalive,而且永远不会超时。wildfy的undertow添加keepalive超时设置如下:ms
<http-listener name="default" max-post-size="204857600" socket-binding="http" redirect-socket="https" tcp-keep-alive="true" read-timeout="30000"/>
用telnet模拟http 客户端调用,可以看到当服务器返回内容后,连接并没有断开,直到超时。
如下示例在一个打开的连接里面,做了两次post。
>telnet 211.100.75.241 8080 Trying 211.100.75.241... Connected to 211.100.75.241. Escape character is '^]'. POST /Message/update HTTP/1.1 Host: 211.100.75.241:8080 Connection: keep-alive Content-Length: 31 Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 Origin: null Content-Type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5702.400 QQBrowser/10.2.1893.400 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 DNT: 1 Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.9 mac=00%3A07%3A63%3Ae2%3Aa7%3A62 HTTP/1.1 200 OK Connection: keep-alive X-Powered-By: Undertow/1 Server: WildFly/10 Content-Type: application/json;charset=UTF-8 Content-Length: 1 Date: Mon, 06 Aug 2018 08:48:58 GMT POST /Message/update HTTP/1.1 Host: 211.100.75.241:8080 Connection: keep-alive Content-Length: 31 Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 Origin: null Content-Type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5702.400 QQBrowser/10.2.1893.400 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 DNT: 1 Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.9 mac=00%3A07%3A63%3Ae2%3Aa7%3A62 HTTP/1.1 200 OK Connection: keep-alive X-Powered-By: Undertow/1 Server: WildFly/10 Content-Type: application/json;charset=UTF-8 Content-Length: 1 Date: Mon, 06 Aug 2018 08:49:05 GMT Connection closed by foreign host.