Fastdfs客户端连接超时解决方案

欢迎扫码加入Java高知群交流


Linux服务器上安装好的FastDFS之后,在服务器上测试上传是没问题的:

/usr/local/bin/fdfs_test /etc/fdfs/client.conf upload a.html

返回的信息如下:

This is FastDFS client test program v3.06

Copyright (C) 2008, Happy Fish / YuQing

FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/ 
for more detail.

[2016-03-05 14:31:53] INFO - base_path=/home/yuqing/fastdfs, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0

tracker_query_storage_store_list_without_group: 
	server 1. group_name=group1, ip_addr=192.168.0.168, port=23000

group_name=group1, ip_addr=192.168.0.168, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/ezlCY1bafVn06_1qAAAAGUEWZqQ75.html
source ip address: 192.168.0.168
file timestamp=2016-03-05 14:31:53
file size=25
file crc32=1091987108
file url: http://192.168.0.168/group1/M00/00/00/ezlCY1bafVn06_1qAAAAGUEWZqQ75.html
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/ezlCY1bafVn06_1qAAAAGUEWZqQ75_big.html
source ip address: 192.168.0.168
file timestamp=2016-03-05 14:31:53
file size=25
file crc32=1091987108
file url: http://192.168.0.168/group1/M00/00/00/ezlCY1bafVn06_1qAAAAGUEWZqQ75_big.html
然而在本地调试的时候却提示连接不上,java代码如下:

package com.eric.core.resource.fastdfs;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerServer;

import com.eric.utils.FileUtil;

/**
 * FastDFS功能调用类
 * 
 */
public class FastDFS {

	private static Logger log = Logger.getLogger(FastDFS.class);

	private static final String FAST_DFS_CONF_FILE = FastDFS.class.getResource("/").getPath() + "/fdfs_client.conf";

	static {
		try {
			// 初始化配置文件
			ClientGlobal.init(FAST_DFS_CONF_FILE);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取存储服务器连接
	 * 
	 * @return
	 * @throws IOException
	 */
	private static StorageClient getStorageClient() throws IOException {

		// 建立tracker server 的连接
		/*
		 * TrackerGroup tg = new TrackerGroup( new InetSocketAddress[] { new
		 * InetSocketAddress( TRACKER_SERVER_IP, TRACKER_SERVER_PORT) });
		 */

		// TrackerClient tc = new TrackerClient();
		TrackerServer ts = ClientGlobal.getG_tracker_group().getConnection();
		if (ts == null) {
			System.out.println("getConnection return null");
			return null;
		}

		// 建立存储服务器的连接
		StorageServer ss = null;// tc.getStoreStorage(ts);
		/*
		 * if (ss == null) { System.out.println("getStoreStorage return null");
		 * return null; }
		 */

		// 建立存储客户端
		StorageClient sc = new StorageClient(ts, ss);

		/* for test only */
		// System.out.println("active test to storage server: " +
		// ProtoCommon.activeTest(ss.getSocket()));
		// ss.close();
		/* for test only */
		// System.out.println("active test to tracker server: " +
		// ProtoCommon.activeTest(ts.getSocket()));
		// ts.close();

		/* for test only */
		System.out.println("active test to storage server: " + ProtoCommon.activeTest(ss.getSocket()));
		ss.close();
		/* for test only */
		System.out.println("active test to tracker server: " + ProtoCommon.activeTest(ts.getSocket()));
		ts.close();

		return sc;
	}

	/**
	 * 上传文件
	 * 
	 * @return
	 */
	public static String[] uploadFile(String fileName, String fileExt, byte[] contents) {
		try {
			if (fileName == null || contents == null) {
				System.out.println("Upload file[" + fileName + "] is null");
				return null;
			}

			long startTime = System.currentTimeMillis();
			System.out.println("Upload file[" + fileName + "] startTime::" + startTime);

			// TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = ClientGlobal.getG_tracker_group().getConnection();
			StorageServer storageServer = null;
			StorageClient client = new StorageClient(trackerServer, storageServer);

			NameValuePair[] meta_list = new NameValuePair[1];
			meta_list[0] = new NameValuePair("filename", fileName);
			String[] fileInfo = client.upload_file(contents, fileExt, meta_list);

			System.out.println("Upload file[" + fileName + "] ok");
			System.out.println(fileInfo[0]);// 文件存储所在的组 如:group1 group2等
			System.out.println(fileInfo[1]);// 文件在服务器上的路径及文件名
			// 如:M00/00/00/eWXds1DJpzCASX0oAAAAA4i3nNI382.txt

			long endTime = System.currentTimeMillis();
			System.out.println("Upload file[" + fileName + "] endTime::" + endTime);

			/* for test only */
			// System.out.println("active test to storage server: " +
			// ProtoCommon.activeTest(storageServer.getSocket()));
			// storageServer.close();
			/* for test only */
			// System.out.println("active test to tracker server: " +
			// ProtoCommon.activeTest(trackerServer.getSocket()));
			// trackerServer.close();

			return fileInfo;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 删除文件
	 * 
	 * @param groupName
	 * @param fileName
	 * @return
	 */
	public static Integer deleteFile(String groupName, String fileName) {
		try {
			long startTime = System.currentTimeMillis();
			System.out.println("Delete file[" + fileName + "] startTime::" + startTime);

			// 建立存储客户端
			// StorageClient sc = getStorageClient();

			// 建立tracker server 的连接
			// TrackerClient tc = new TrackerClient();
			TrackerServer ts = ClientGlobal.getG_tracker_group().getConnection();
			if (ts == null) {
				System.out.println("getConnection return null");
				return null;
			}

			// 建立存储服务器的连接
			StorageServer ss = null;// tc.getStoreStorage(ts);

			// 建立存储客户端
			StorageClient sc = new StorageClient(ts, ss);

			Integer x = sc.delete_file(groupName, fileName);

			System.out.println("Delete file[" + fileName + "] ok");

			long endTime = System.currentTimeMillis();
			System.out.println("Delete file[" + fileName + "] endTime::" + endTime);

			/* for test only */
			System.out.println("active test to storage server: " + ProtoCommon.activeTest(ss.getSocket()));
			ss.close();
			/* for test only */
			System.out.println("active test to tracker server: " + ProtoCommon.activeTest(ts.getSocket()));
			ts.close();

			return x;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 下载文件
	 * 
	 * @param groupName
	 * @param fileName
	 * @return
	 */
	public static byte[] downloadFile(String groupName, String fileName) {
		try {
			long startTime = System.currentTimeMillis();
			System.out.println("Download file[" + fileName + "] startTime::" + startTime);

			// 建立存储客户端
			// StorageClient sc = getStorageClient();

			// 建立tracker server 的连接
			// TrackerClient tc = new TrackerClient();
			TrackerServer ts = ClientGlobal.getG_tracker_group().getConnection();
			if (ts == null) {
				System.out.println("getConnection return null");
				return null;
			}

			// 建立存储服务器的连接
			StorageServer ss = null;// tc.getStoreStorage(ts);

			// 建立存储客户端
			StorageClient sc = new StorageClient(ts, ss);

			byte[] localfileByteArr = sc.download_file(groupName, fileName);

			System.out.println("Download file[" + fileName + "] ok");

			long endTime = System.currentTimeMillis();
			System.out.println("Download file[" + fileName + "] endTime::" + endTime);

			/* for test only */
			System.out.println("active test to storage server: " + ProtoCommon.activeTest(ss.getSocket()));
			ss.close();
			/* for test only */
			System.out.println("active test to tracker server: " + ProtoCommon.activeTest(ts.getSocket()));
			ts.close();

			return localfileByteArr;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		try {

			String fileName = "test";
			String fileExt = "txt";
			File file = new File("D:\\temp\\test.txt");
			uploadFile(fileName, fileExt, FileUtil.getBytesFromFile(file));

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

执行main方法时报错:

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:47)
at org.csource.fastdfs.TrackerGroup.getConnection(TrackerGroup.java:72)
at com.eric.core.resource.fastdfs.FastDFS.uploadFile(FastDFS.java:107)
at com.eric.core.resource.fastdfs.FastDFS.main(FastDFS.java:257)


解决的办法:

storage的tracker_server地址必须是外网地址,重启FastDFS就好了。

欢迎扫码加入Java高知群交流


阅读更多

扫码向博主提问

Java高知

非学,无以致疑;非问,无以广识
去开通我的Chat快问

没有更多推荐了,返回首页