CentOS solves the problem of a large number of time_wait servers. centostime_wait
Recently, a large number of time_wait TCP connections have occurred on the server, resulting in excessive server connections, which eventually leads to tomcat suspended. The following message is displayed when you view the number of connections on the server.
[root@test apache-tomcat-7.0.53]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT 14434FIN_WAIT2 6ESTABLISHED 133
Obviously, requests are too frequent, mainly because the backend servers call each other through rest requests, while the java Server recycles tcp threads slowly, although the connection has been closed explicitly
Connection recovery is slow. The way to solve this problem through java APIs is blocked.
Later, I went to the server to check a large amount of information and considered modifying the Linux kernel to solve the problem. The solution is as follows:
vi /etc/sysctl.conf
Add configuration information
# Enable SYN Cookies. When a SYN wait queue overflows, cookies are enabled to prevent a small number of SYN attacks. The default value is 0, indicating to disable net. ipv4.tcp _ syncookies = 1 #. This indicates to Enable reuse of tcp connections. Allow TIME-WAIT sockets to be re-used for a New TCP connection. The default value is 0, indicating to disable net. ipv4.tcp _ tw_reuse = 1 #. This indicates to enable the fast recovery of TIME-WAIT sockets in TCP connections. The default value is 0, indicating that net is disabled. ipv4.tcp _ tw_recycle = 1 # indicates that if the socket is disabled by the local end, this parameter determines the time for it to remain in the FIN-WAIT-2 state. net. ipv4.tcp _ fin_timeout = 30
Make the parameter configuration take effect
/sbin/sysctl -p
Returned results
Perfect solution:
[root@test apache-tomcat-7.0.53]# netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' TIME_WAIT 96FIN_WAIT2 3ESTABLISHED 141