背景
在Centos7服务器上,有服务A调用服务B的上传文件接口,约几十个请求过去后,出现Caused by: java.io.IOException: Connection reset by peer异常
Caused by: java.io.IOException: Connection reset by peer
at sun.nio.ch.FileDispatcherImpl.write0(Native Method) ~[?:1.8.0_361]
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47) ~[?:1.8.0_361]
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93) ~[?:1.8.0_361]
排查
根据网络上查询的结果,有以下几种常见情况
- 服务并发处理处理不过来,会关闭其中的一些连接
- 防火墙
- 客户端关闭了连接(未通知服务端四次挥手,例如直接关闭客户端)
经排查不属于以上情况,最后定位到open files设置的值调小了导致的
我使用的Centos7,默认打开文件数为1024个,通过ulimit -a 可以查看open files
[root@localhost docker]# ulimit -a | grep open
open files (-n) 1024
解决方案
1.修改ulimit
vim /etc/security/limits.conf
# 在最后追加以下内容
* soft nofile 65536
* hard nofile 65536
或者使用如下命令直接追加至 /etc/security/limits.conf 文件
echo "* soft nofile 65536
* hard nofile 65536 " >> /etc/security/limits.conf
检查是否生效
[root@localhost docker]# ulimit -a | grep open
open files (-n) 65536
2.重启后台服务
至此问题解决!