问题概述

在项目中使用FastDFSClient上传图片/文件时,一直上传失败的,报错:java.net.SocketTimeoutException: connect timed out 表示连接失败的,错误信息部分截图如下:

具体信息如下:

2020-03-08 16:41:12,423 [localhost-startStop-1-SendThread(192.168.159.128:2181)] [org.apache.zookeeper.ClientCnxn]-[DEBUG] Got ping response for sessionid: 0x10000063e360009 after 2ms
java.net.SocketTimeoutException: connect timed out
	at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at org.csource.fastdfs.ClientGlobal.getSocket(ClientGlobal.java:208)
	at org.csource.fastdfs.StorageServer.<init>(StorageServer.java:43)
	at org.csource.fastdfs.TrackerClient.getStoreStorage(TrackerClient.java:144)
	at org.csource.fastdfs.StorageClient.newWritableStorageConnection(StorageClient.java:1627)
	at org.csource.fastdfs.StorageClient.do_upload_file(StorageClient.java:639)
	at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:162)
	at org.csource.fastdfs.StorageClient.upload_file(StorageClient.java:180)
	at org.csource.fastdfs.StorageClient1.upload_file1(StorageClient1.java:103)
	at com.huazai.aiyou.manager.web.util.FastDFSClient.uploadFile(FastDFSClient.java:99)
	at com.huazai.aiyou.manager.web.util.FastDFSClient.uploadFile(FastDFSClient.java:110)
	at com.huazai.aiyou.manager.controller.PictureController.fileUpload(PictureController.java:70)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

 

 

解决办法

经过排查,发现少开了一个端口号23000,整个FastDFS中需要开两个端口,一个是Tracker的22122,一个是Storage的23000,这两个是必须的,如下图:

关于FastDFS交互原理,请参看《FastDFS分布式文件系统内部架构及其原理解析》一目了然了就,

1、暴力点,直接关闭防火前(如果在生产环境下,不安全,在开发或测试环境嘛,随便!)

[root@192 ~]# systemctl stop firewalld
[root@192 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Sun 2020-03-08 01:14:35 EST; 8s ago
     Docs: man:firewalld(1)
  Process: 718 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 718 (code=exited, status=0/SUCCESS)

Mar 07 20:32:59 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Mar 07 20:33:00 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Mar 08 01:14:34 192.168.159.136 systemd[1]: Stopping firewalld - dynamic firewall daemon...
Mar 08 01:14:35 192.168.159.136 systemd[1]: Stopped firewalld - dynamic firewall daemon.

2、在生产环境中,出于安全考虑,开启防火前,并在防火墙添加指定的通行端口

[root@192 ~]# firewall-cmd --zone=public --add-port=23000/tcp --permanent
success
[root@192 ~]# firewall-cmd --zone=public --add-port=22122/tcp --permanent
success
[root@192 ~]# firewall-cmd --reload
success

开启指定端口或者直接关闭防火墙后,再次使用上传时就可以了!

 

 

 


 好了,关于 java.net.SocketTimeoutException: connect timed out 的解决办法 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者:华    仔
联系作者:who.seek.me@java98k.vip
来        源:CSDN (Chinese Software Developer Network)
原        文:https://blog.csdn.net/Hello_World_QWP/article/details/104735517
版权声明:本文为博主原创文章,请在转载时务必注明博文出处!
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐