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

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

转载地址:http://gary0416.iteye.com/blog/1148790

先引用一下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

上传

Java代码   收藏代码
  1. package com.gary.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5.   
  6. import org.csource.common.NameValuePair;  
  7. import org.csource.fastdfs.ClientGlobal;  
  8. import org.csource.fastdfs.ServerInfo;  
  9. import org.csource.fastdfs.StorageClient;  
  10. import org.csource.fastdfs.StorageServer;  
  11. import org.csource.fastdfs.TrackerClient;  
  12. import org.csource.fastdfs.TrackerServer;  
  13.   
  14. /** 
  15.  * 测试上传 
  16.  * @author gary 
  17.  * 
  18.  */  
  19. public class TestUpload {  
  20.     public static void main(String[] args) throws Exception{  
  21.         String classPath = new File(TestUpload.class.getResource("/").getFile()).getCanonicalPath();  
  22.         String configFilePath = classPath + File.separator + "fdfs_client.conf";  
  23.         System.out.println("配置文件:" + configFilePath);  //工程配置文件的获取
  24.           
  25.         ClientGlobal.init(configFilePath);  
  26.           
  27.         TrackerClient trackerClient = new TrackerClient();  
  28.         TrackerServer trackerServer = trackerClient.getConnection();  
  29.   
  30.         StorageServer storageServer = null;  
  31.         StorageClient storageClient = new StorageClient(trackerServer, storageServer);  
  32.           
  33.         NameValuePair[] meta_list = new NameValuePair[3];  
  34.         meta_list[0] = new NameValuePair("width""120");  
  35.         meta_list[1] = new NameValuePair("heigth""120");  
  36.         meta_list[2] = new NameValuePair("author""gary");  
  37.           
  38. //      byte[] file_buff = "F:\\pic.jpg".getBytes(ClientGlobal.g_charset);  
  39.         File file = new File("F:\\pic.jpg");  
  40.         FileInputStream fis = new FileInputStream(file);  
  41.         byte[] file_buff = null;  
  42.         if(fis != null){  
  43.             int len = fis.available();  
  44.             file_buff = new byte[len];  
  45.             fis.read(file_buff);  
  46.         }  
  47.         System.out.println("file length: " + file_buff.length);  
  48.           
  49.         String group_name = null;  
  50.         StorageServer[] storageServers = trackerClient.getStoreStorages(trackerServer, group_name);  
  51.         if (storageServers == null) {  
  52.             System.err.println("get store storage servers fail, error code: " + storageClient.getErrorCode());  
  53.         }else{  
  54.             System.err.println("store storage servers count: " + storageServers.length);  
  55.             for (int k = 0; k < storageServers.length; k++){  
  56.                 System.err.println(k + 1 + ". " + storageServers[k].getInetSocketAddress().getAddress().getHostAddress() + ":" + storageServers[k].getInetSocketAddress().getPort());  
  57.             }  
  58.             System.err.println("");  
  59.         }  
  60.           
  61.         long startTime = System.currentTimeMillis();  
  62.         String[] results = storageClient.upload_file(file_buff, "jpg", meta_list);  
  63.         System.out.println("upload_file time used: " + (System.currentTimeMillis() - startTime) + " ms");  
  64.   
  65.         if (results == null){  
  66.             System.err.println("upload file fail, error code: " + storageClient.getErrorCode());  
  67.             return;  
  68.         }  
  69.           
  70.         group_name = results[0];  
  71.         String remote_filename = results[1];  
  72.         System.err.println("group_name: " + group_name + ", remote_filename: " + remote_filename);  
  73.         System.err.println(storageClient.get_file_info(group_name, remote_filename));  
  74.   
  75.         ServerInfo[] servers = trackerClient.getFetchStorages(trackerServer, group_name, remote_filename);  
  76.         if (servers == null){  
  77.             System.err.println("get storage servers fail, error code: " + trackerClient.getErrorCode());  
  78.         } else {  
  79.             System.err.println("storage servers count: " + servers.length);  
  80.             for (int k = 0; k < servers.length; k++){  
  81.                 System.err.println(k + 1 + ". " + servers[k].getIpAddr() + ":" + servers[k].getPort());  
  82.             }  
  83.             System.err.println("");  
  84.         }  
  85.     }  
  86. }  

 console输出:

Console代码   收藏代码
  1. 配置文件:D:\AEclipse2\workspace\myeclipse\testFastDFS\bin\fdfs_client.conf  
  2. file length: 10  
  3. store storage servers count: 1  
  4. 1192.168.49.130:23000  
  5.   
  6. upload_file time used: 29 ms  
  7. group_name: group1, remote_filename: M00/00/00/wKgxgk5HbLvfP86RAAAAChd9X1Y736.jpg  
  8. source_ip_addr = 192.168.49.130, file_size = 10, create_timestamp = 2011-08-14 14:35:39, crc32 = 394092374  
  9. storage servers count: 1  
  10. 1192.168.49.130:23000  

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

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


 获取文件信息

Java代码   收藏代码
  1. package com.gary.test;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.csource.fastdfs.ClientGlobal;  
  6. import org.csource.fastdfs.FileInfo;  
  7. import org.csource.fastdfs.StorageClient;  
  8. import org.csource.fastdfs.StorageServer;  
  9. import org.csource.fastdfs.TrackerClient;  
  10. import org.csource.fastdfs.TrackerServer;  
  11.   
  12. /** 
  13.  * 获取文件信息 
  14.  * @author gary 
  15.  * 
  16.  */  
  17. public class TestGet {  
  18.     public static void main(String[] args) throws Exception {  
  19.         String classPath = new File(TestGet.class.getResource("/").getFile()).getCanonicalPath();  
  20.         String configFilePath = classPath + File.separator + "fdfs_client.conf";  
  21.         ClientGlobal.init(configFilePath);  
  22.         TrackerClient trackerClient = new TrackerClient();  
  23.         TrackerServer trackerServer = trackerClient.getConnection();  
  24.         StorageServer storageServer = null;  
  25.         StorageClient storageClient = new StorageClient(trackerServer, storageServer);  
  26.           
  27.         String group_name = "group1";  
  28.         String remote_filename = "M00/00/00/wKgxgk5HcFCwvS9QAAANWIusP08057.jpg";  
  29.         FileInfo fi = storageClient.get_file_info(group_name, remote_filename);  
  30.         String sourceIpAddr = fi.getSourceIpAddr();  
  31.         long size = fi.getFileSize();  
  32.         System.out.println("ip:" + sourceIpAddr + ",size:" + size);  
  33.     }  
  34. }  

 console输出

Console代码   收藏代码
  1. ip:192.168.49.130,size:3416  

 删除

Java代码   收藏代码
  1. package com.gary.test;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.csource.fastdfs.ClientGlobal;  
  6. import org.csource.fastdfs.StorageClient;  
  7. import org.csource.fastdfs.StorageServer;  
  8. import org.csource.fastdfs.TrackerClient;  
  9. import org.csource.fastdfs.TrackerServer;  
  10.   
  11. /** 
  12.  * 删除文件 
  13.  * @author gary 
  14.  * 
  15.  */  
  16. public class TestDelete {  
  17.     public static void main(String[] args) throws Exception {  
  18.         String classPath = new File(TestDelete.class.getResource("/").getFile()).getCanonicalPath();  
  19.         String configFilePath = classPath + File.separator + "fdfs_client.conf";  
  20.         ClientGlobal.init(configFilePath);  
  21.         TrackerClient trackerClient = new TrackerClient();  
  22.         TrackerServer trackerServer = trackerClient.getConnection();  
  23.         StorageServer storageServer = null;  
  24.         StorageClient storageClient = new StorageClient(trackerServer, storageServer);  
  25.           
  26.         String group_name = "group1";  
  27.         String remote_filename = "M00/00/00/wKgxgk5HbLvfP86RAAAAChd9X1Y736.jpg";  
  28.         storageClient.delete_file(group_name, remote_filename);  
  29.         System.out.println("删除成功");  
  30.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值