连接时出现异常,具体如下:
connect to server xxx(服务器对应公网IP):22122 fail
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.TrackerGroup.getConnection(TrackerGroup.java:46)
at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:68)
at org.csource.fastdfs.TrackerClient.getConnection(TrackerClient.java:57)
at top.javahai.subtlechat.api.Tests.testUpload(Tests.java:67)
at top.javahai.subtlechat.api.Tests.main(Tests.java:23)
这里有几个注意事项需要大致说下,由于我这边都是本地使用进行远程连接的,所以在部署安装fastdsf相关应用时,对应的IP需更改成对应的公网IP或域名。其次是相关端口需添加到阿里服务器的安全组规则内,如下所示:
接下来是关于解决问题的大致思路有以下几个:
关闭防火墙重新连接即可,连接成功后上传完成会返回上传文件路径。由于我是本地测试类调用,具体代码见下:
public static void testUpload() {
try {
//初始化配置文件
ClientGlobal.initByProperties("fastdfs-client.properties");
//实例化TrackerClient对象
TrackerClient tracker = new TrackerClient();
//获取连接对象
TrackerServer trackerServer = tracker.getConnection();
//重置StorageServer对象
StorageServer storageServer = null;
//获取客户端连接
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
NameValuePair nvp[] = null;
//上传到文件系统并返回对应路径
String fileId = client.upload_file1("M:\\image\\002.png", "png",
nvp);
System.out.println("上传返回消息:"+fileId);
} catch (Exception e) {
e.printStackTrace();
}
}
对应配置文件如下:
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
#服务器密钥,默认为“FastDFS1234567890”。可更改但必须保证服务端与客户端一致
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
#服务器公网IP与端口
fastdfs.tracker_servers = xxxx:22122
fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000
连接上传成功效果如下:
Disconnected from the target VM, address: '127.0.0.1:51744', transport: 'socket'
上传返回消息:group1/M00/00/00/L2PKeWDztcuAPdf8AAIohYtxWLY743.png
这边有个小插曲,就是关于防火墙的问题。因为不小心看错命令执行了暂时关闭防火墙导致重启出问题了。解决暂时关闭后无法启动的问题,因为暂时关闭会将任务进入锁定状态:
Failed to start firewalld.service: Unit is masked
解决方法:
systemctl unmask firewalld
防火墙相关命令:
1.启动防火墙
systemctl start firewalld
2.禁用防火墙
systemctl stop firewalld
3.设置开机启动
systemctl enable firewalld
4.停止并禁用开机启动
sytemctl disable firewalld
5.重启防火墙
firewall-cmd --reload
6.查看状态
systemctl status firewalld或者 firewall-cmd --state
7.查看版本
firewall-cmd --version
8.查看帮助
firewall-cmd --help
9.查看区域信息
firewall-cmd --get-active-zones
10.查看指定接口所属区域信息
firewall-cmd --get-zone-of-interface=eth0
11.拒绝所有包
firewall-cmd --panic-on
12.取消拒绝状态
firewall-cmd --panic-off
13.查看是否拒绝
firewall-cmd --query-panic
14.将接口添加到区域(默认接口都在public)
firewall-cmd --zone=public --add-interface=eth0(永久生效再加上 --permanent 然后reload防火墙)
15.设置默认接口区域
firewall-cmd --set-default-zone=public(立即生效,无需重启)
16.更新防火墙规则
firewall-cmd --reload或firewall-cmd --complete-reload(两者的区别就是第一个无需断开连接,就是firewalld特性之一动态 添加规则,第二个需要断开连接,类似重启服务)
17.查看指定区域所有打开的端口
firewall-cmd --zone=public --list-ports
18.在指定区域打开端口(记得重启防火墙)
firewall-cmd --zone=public --add-port=80/tcp(永久生效再加上 --permanent)