`
gary0416
  • 浏览: 330405 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

FastDFS的安装,配置与使用(java)

阅读更多

先引用一下FastDFS的介绍:

FastDFS is an open source high performance distributed file system. It's major functions include: file storing, file syncing and file accessing (file uploading and file downloading), and it can resolve the high capacity and load balancing problem. FastDFS should meet the requirement of the website whose service based on files such as photo sharing site and vidio sharing site.

FastDFS has two roles: tracker and storage. The tracker takes charge of scheduling and load balancing for file access. The storage store files and it's function is file management including: file storing, file syncing, providing file access interface. It also manage the meta data which are attributes representing as key value pair of the file. For example: width=1024, the key is "width" and the value is "1024".

 

 

开始安装:

1. 在http://code.google.com/p/fastdfs/downloads/list下载所需文件,此外还需先安装好libevent


2. tar xzf FastDFS_v2.11.tar.gz


3. cd FastDFS
如果支持HTTP, vi make.sh, 使用/WITH_HTTPD查找到这一行,输入i进入编辑模式,删除掉前面的注释#,:wq保存退出,如果需要安装成服务,则把下面一行也解开
./make.sh
./make.sh install


4. 使用netstat -an | grep 端口号, 找几个空闲的端口


5. 根据实际情况修改/etc/fdfs下的配置文件,每个上面都有注释说明,如果需要HTTP,别忘了解开最下面的#include http.conf,要带一个#


6. 启动tracker: /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf


7. 启动storage: /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf如果出现错误,可以到步骤5修改配置文件时设置的目录的log目录下查看具体错误原因,我就是因为多删了一#导致变量无法导入,总是空.


8. 到此安装配置完毕


上传文件:/usr/local/bin/fdfs_upload_file  <config_file> <local_filename>

下载文件:/usr/local/bin/fdfs_download_file <config_file> <file_id> [local_filename]

删除文件:/usr/local/bin/fdfs_delete_file <config_file> <file_id>

monitor: /usr/local/bin/fdfs_monitor /etc/fdfs/client.conf

关闭:

killall fdfs_trackerd

killall fdfs_storaged

/usr/local/bin/stop.sh /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf

/usr/local/bin/stop.sh /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf


重启:

/usr/local/bin/restart.sh /usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf

/usr/local/bin/restart.sh /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf


9.java client

上传

package com.gary.test;

import java.io.File;
import java.io.FileInputStream;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

/**
 * 测试上传
 * @author gary
 *
 */
public class TestUpload {
	public static void main(String[] args) throws Exception{
		String classPath = new File(TestUpload.class.getResource("/").getFile()).getCanonicalPath();
		String configFilePath = classPath + File.separator + "fdfs_client.conf";
		System.out.println("配置文件:" + configFilePath);
		
		ClientGlobal.init(configFilePath);
		
		TrackerClient trackerClient = new TrackerClient();
	    TrackerServer trackerServer = trackerClient.getConnection();

	    StorageServer storageServer = null;
	    StorageClient storageClient = new StorageClient(trackerServer, storageServer);
	    
	    NameValuePair[] meta_list = new NameValuePair[3];
	    meta_list[0] = new NameValuePair("width", "120");
	    meta_list[1] = new NameValuePair("heigth", "120");
	    meta_list[2] = new NameValuePair("author", "gary");
	    
//	    byte[] file_buff = "F:\\pic.jpg".getBytes(ClientGlobal.g_charset);
	    File file = new File("F:\\pic.jpg");
	    FileInputStream fis = new FileInputStream(file);
	    byte[] file_buff = null;
	    if(fis != null){
	    	int len = fis.available();
	    	file_buff = new byte[len];
	    	fis.read(file_buff);
	    }
	    System.out.println("file length: " + file_buff.length);
	    
	    String group_name = null;
	    StorageServer[] storageServers = trackerClient.getStoreStorages(trackerServer, group_name);
	    if (storageServers == null) {
	    	System.err.println("get store storage servers fail, error code: " + storageClient.getErrorCode());
	    }else{
	        System.err.println("store storage servers count: " + storageServers.length);
	        for (int k = 0; k < storageServers.length; k++){
	        	System.err.println(k + 1 + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());
	        }
	        System.err.println("");
	    }
	    
	    long startTime = System.currentTimeMillis();
	    String[] results = storageClient.upload_file(file_buff, "jpg", meta_list);
	    System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");

	    if (results == null){
	        System.err.println("upload file fail, error code: " + storageClient.getErrorCode());
	        return;
	    }
	    
	    group_name = results[0];
	    String remote_filename = results[1];
	    System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);
	    System.err.println(storageClient.get_file_info(group_name, remote_filename));

	    ServerInfo[] servers = trackerClient.getFetchStorages(trackerServer, group_name, remote_filename);
	    if (servers == null){
	    	System.err.println("get storage servers fail, error code: " + trackerClient.getErrorCode());
	    } else {
	    	System.err.println("storage servers count: " + servers.length);
	    	for (int k = 0; k < servers.length; k++){
	    		System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());
	    	}
	    	System.err.println("");
	    }
	}
}

 console输出:

配置文件:D:\AEclipse2\workspace\myeclipse\testFastDFS\bin\fdfs_client.conf
file length: 10
store storage servers count: 1
1. 192.168.49.130:23000

upload_file time used: 29 ms
group_name: group1, remote_filename: M00/00/00/wKgxgk5HbLvfP86RAAAAChd9X1Y736.jpg
source_ip_addr = 192.168.49.130, file_size = 10, create_timestamp = 2011-08-14 14:35:39, crc32 = 394092374
storage servers count: 1
1. 192.168.49.130:23000

上传成功后可以打开浏览器使用http方式访问此文件,ip+port+group+filename,

例如 http://192.168.49.130:8001/group1/M00/00/00/wKgxgk5HcFCwvS9QAAANWIusP08057.jpg


 获取文件信息

package com.gary.test;

import java.io.File;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

/**
 * 获取文件信息
 * @author gary
 *
 */
public class TestGet {
	public static void main(String[] args) throws Exception {
		String classPath = new File(TestGet.class.getResource("/").getFile()).getCanonicalPath();
		String configFilePath = classPath + File.separator + "fdfs_client.conf";
		ClientGlobal.init(configFilePath);
		TrackerClient trackerClient = new TrackerClient();
	    TrackerServer trackerServer = trackerClient.getConnection();
	    StorageServer storageServer = null;
	    StorageClient storageClient = new StorageClient(trackerServer, storageServer);
	    
	    String group_name = "group1";
	    String remote_filename = "M00/00/00/wKgxgk5HcFCwvS9QAAANWIusP08057.jpg";
	    FileInfo fi = storageClient.get_file_info(group_name, remote_filename);
	    String sourceIpAddr = fi.getSourceIpAddr();
	    long size = fi.getFileSize();
	    System.out.println("ip:" + sourceIpAddr + ",size:" + size);
	}
}

 console输出

ip:192.168.49.130,size:3416

 删除

package com.gary.test;

import java.io.File;

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

/**
 * 删除文件
 * @author gary
 *
 */
public class TestDelete {
	public static void main(String[] args) throws Exception {
		String classPath = new File(TestDelete.class.getResource("/").getFile()).getCanonicalPath();
		String configFilePath = classPath + File.separator + "fdfs_client.conf";
		ClientGlobal.init(configFilePath);
		TrackerClient trackerClient = new TrackerClient();
	    TrackerServer trackerServer = trackerClient.getConnection();
	    StorageServer storageServer = null;
	    StorageClient storageClient = new StorageClient(trackerServer, storageServer);
	    
	    String group_name = "group1";
	    String remote_filename = "M00/00/00/wKgxgk5HbLvfP86RAAAAChd9X1Y736.jpg";
	    storageClient.delete_file(group_name, remote_filename);
	    System.out.println("删除成功");
	}
}
 

附件为配置文件和myeclipse项目源代码

  • 大小: 23.6 KB
3
5
分享到:
评论
8 楼 the0728 2015-12-08  
你好,遇到了个问题是,在浏览器中,按照ip+port+group+filename显示不出图片
7 楼 yoyona 2015-06-30  
zj_203 写道
这个上传显示connect timed out 是什么情况?

我也是上传报这个错误。。。。求解
6 楼 javachen002 2015-06-18  
l582113448 写道
可以上传,查找,但是,为什么删除成功后文件依然在目录里?所以删除功能并没有实现 ,为什么删除不了呢?求解!!!!!

5 楼 javachen002 2015-06-18  
l582113448 写道
可以上传,查找,但是,为什么删除成功后文件依然在目录里?所以删除功能并没有实现



为什么删除不了呢?求解
4 楼 zj_203 2015-04-28  
这个上传显示connect timed out 是什么情况?
3 楼 sunlightcs 2014-03-26  
http://doc.baba.io/subject/318   这个文档不错了,是FastDFS5.01的安装
2 楼 xiaoll880214 2014-03-17  
全部验证通过!可用,但是FileInfo 怎么整成File 还不懂
1 楼 l582113448 2013-12-27  
可以上传,查找,但是,为什么删除成功后文件依然在目录里?所以删除功能并没有实现

相关推荐

Global site tag (gtag.js) - Google Analytics