fastdfs 高并发场景下的异常处理
2019-01-17 本文已影响0人
咦咦咦萨
我们用fastdfs做为商户文件资料的存储,近期随着业务变动,也开始对外提供文件访问服务,发现请求偶尔会报错,异常大概如下:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209) ~[na:1.8.0_74]
at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_74]
at java.net.SocketInputStream.read(SocketInputStream.java:127) ~[na:1.8.0_74]
at org.csource.fastdfs.ProtoCommon.recvHeader(ProtoCommon.java:168) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.ProtoCommon.recvPackage(ProtoCommon.java:201) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.TrackerClient.getStorages(TrackerClient.java:392) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.TrackerClient.getFetchStorage(TrackerClient.java:296) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.StorageClient.newReadableStorageConnection(StorageClient.java:1647) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1118) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1105) ~[fastdfs-client-java-1.27.jar!/:na]
java.io.IOException: recv package size -1 != 10
at org.csource.fastdfs.ProtoCommon.recvHeader(ProtoCommon.java:169) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.ProtoCommon.recvPackage(ProtoCommon.java:201) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.TrackerClient.getStorages(TrackerClient.java:392) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.TrackerClient.getFetchStorage(TrackerClient.java:296) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.StorageClient.newReadableStorageConnection(StorageClient.java:1647) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1118) ~[fastdfs-client-java-1.27.jar!/:na]
at org.csource.fastdfs.StorageClient.download_file(StorageClient.java:1105) ~[fastdfs-client-java-1.27.jar!/:na]
at com.ielpm.images.viewer.FastDFSClient.downloadFile(FastDFSClient.java:173) ~[classes!/:0.0.1-SNAPSHOT]
at com.ielpm.images.viewer.ViewController.imgViewer(ViewController.java:53) ~[classes!/:0.0.1-SNAPSHOT]
at sun.reflect.GeneratedMethodAccessor30.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]
软件环境如下:
- fastdfs:v5.05
- fastdfs_client_java:v1.27
解决方法:
-
由于fastdfs客户端的实现中,storageClient、trackerServer和storageClient都不是线程安全的,而且,每次调用都会new一个socket连接,调用完成后会关闭,所以不能复用。
-
在5.05版本中,storage和tracker的默认最大连接数(max_connections)为256,访问量一大就会超出报错,所以要根据场景,适当调整该值的大小。