最近在接中间件Memcached的时候,总是会出现以下错误:
[18-11-9 12:10:59:156 CST] 0000014b SystemOut O 2018-11-09 12:10:59 156 [ERROR][Heal-Session-Thread][com.google.code.yanf4j.core.impl.AbstractController] SessionMonitor connect error
java.net.SocketException: 打开的文件过多
at sun.nio.ch.Net.socket0(Native Method)
at sun.nio.ch.Net.socket(Net.java:529)
at sun.nio.ch.Net.socket(Net.java:512)
at sun.nio.ch.SocketChannelImpl.
at sun.nio.ch.SelectorProviderImpl.openSocketChannel(SelectorProviderImpl.java:73)
at java.nio.channels.SocketChannel.open(SocketChannel.java:154)
at net.rubyeye.xmemcached.impl.MemcachedConnector.connect(MemcachedConnector.java:508)
at net.rubyeye.xmemcached.impl.MemcachedConnector$SessionMonitor.run(MemcachedConnector.java:127)
,后来是发现服务器端设置的socket可接受数量不够引起的,linux默认只允许1024个连接请求上来。参考了网上相关文档,需要将这个值设置大一点即可。
,linux系统默认ulimit为1024个访问 用户最多可开启的程序数目。一般一个端口(即一个进程)的最高连接为2的16次方65536
通过这个命令 ulimit -n 可以看到默认值为1024
查看全局文件句柄数限制(系统支持的最大值)
cat /proc/sys/fs/file-max
查看每个进程文件句柄数限制
ulimit -n
* soft nofile 65536
* hard nofile 65536
session required /lib/security/pam_limits.so
如果是64bit系统的话,应该为 :
session required /lib64/security/pam_limits.so
net.ipv4.ip_local_port_range = 1024 65535
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_window_scaling = 0
net.ipv4.tcp_sack = 0
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_no_metrics_save=1
net.core.somaxconn = 262144
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
fs.file-max=1000000
/sbin/sysctl -p /etc/sysctl.conf
/sbin/sysctl -w net.ipv4.route.flush=1
echo ulimit -HSn 65536 >> /etc/rc.local
echo ulimit -HSn 65536 >>/root/.bash_profile
ulimit -HSn 65536
通过修改,tcp可以达到65536个连接完全没有问题
通过这个命令 ulimit -n 可以看到值改为65536了,也就是说现在最多支持65536个tcp socket连接了
查看当前有多少个TCP连接到当前服务器命令:
netstat -antp |grep -i est |wc -l
关于netstat请参考:
Linux netstat命令详解