`
gary0416
  • 浏览: 333496 次
  • 性别: 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  
可以上传,查找,但是,为什么删除成功后文件依然在目录里?所以删除功能并没有实现

相关推荐

    fastdfs 安装及使用

    **FastDFS简介** FastDFS是一个开源的高性能、轻量级的分布式文件系统,它对文件进行管理,包括文件存储、...了解并熟练掌握FastDFS的安装、配置、使用以及与Java的集成,对于提升系统的稳定性和扩展性具有重要意义。

    fastDFS linux安装及java代码

    通过以上步骤,你已经在Linux环境中成功安装并配置了FastDFS,同时了解了如何使用Java客户端进行文件上传。在实际生产环境中,你可能还需要考虑负载均衡、容灾备份、监控告警等高级功能,以确保系统的稳定性和高可用...

    fastdfs安装配置

    在本文中,我们将详细介绍如何在Linux环境中安装配置FastDFS,并确保Java客户端能够成功调用。 首先,我们需要安装libevent库,它是FastDFS运行所依赖的关键组件。通过以下步骤来安装libevent-1.4.14b-stable: 1....

    FastDFS集群配置文件

    `mod_fastdfs.conf`是FastDFS的模块配置文件,它定义了FastDFS与Nginx之间的交互方式。其中,`group1--group2`表示至少有两个存储组,每个组由若干个Storage服务器构成,提供冗余和负载均衡。在配置文件中,你需要...

    fastdfs-client-javajar1.27-SNAPSHOT.zip

    总的来说,FastDFS客户端Java版是Java开发者与FastDFS服务交互的重要工具,它的使用涉及到项目构建配置、网络通信以及文件操作等多个方面。在开发过程中,理解并正确配置这个客户端是确保项目能够正常运行的关键。

    FastDFS安装文件及java代码示例

    在Java应用中使用FastDFS,需要引入FastDFS的Java客户端库。这个Demo中可能包含了FastDFS的Java SDK,用于文件上传和下载操作。 1. **引入依赖**:将FastDFS的Java客户端库加入到项目类路径,如果是Maven项目,可以...

    fastdfs-client-java-1.27.zip

    2. **fastdfs-client-java-1.27-SNAPSHOT.pom**:这是Maven项目的依赖配置文件,包含了该库的依赖关系和版本信息。如果项目使用Maven构建,可以通过引入这个POM文件来管理FastDFS客户端的依赖,确保与其他库的兼容性...

    fastdfs-client-java-1.29-SNAPSHOT.jar

    使用`fastdfs-client-java`时,首先需要在项目中引入`fastdfs-client-java-1.29-SNAPSHOT.jar`和`commons-io-2.4.jar`这两个依赖库,然后根据`fdfs_client.conf`配置客户端,并通过`FastDFSClient`类提供的API进行...

    Java操作FastDFS示例代码

    本示例代码将帮助开发者理解如何使用Java与FastDFS进行交互。 在Java中,我们通常会使用FastDFS的Java客户端来实现文件的上传和下载。首先,你需要在项目中引入FastDFS的Java客户端库。这可以通过Maven或Gradle的...

    fastdfs-client-java-1.27-SNAPSHOT.zip

    1. **fastdfs-client-java-1.27-SNAPSHOT.jar**:这是FastDFS Java客户端的核心库文件,包含了所有与FastDFS交互的API和实现。开发人员可以通过引入这个JAR包,调用其提供的接口来完成文件的上传、下载、查询等操作...

    fastdfs-client-java-1.27

    《FastDFS客户端Java库1.27版...总之,FastDFS客户端Java库1.27版为Java开发者提供了一个便捷、高效的与FastDFS服务器交互的工具,结合源码阅读,开发者可以更深入地理解和定制化使用,以满足多样化的文件存储需求。

    FastDFS 搭建,及其JAVA客户端使用

    - FastDFS可以在CentOS 7环境下安装配置。在新版的FastDFS(如5.0.5版本)中,若安装后访问没有反应(例如8099端口),需要查看日志文件(error.log)来诊断问题。根据日志提示,如果发现缺少支持多组的配置,应在/...

    Java Centos7上fastdfs安装详细手册

    至此,您已经完成了FastDFS在Centos7上的安装及配置,包括libfastcommon、FastDFS本身以及fastdfs-nginx-module和Nginx等必要组件的安装与配置。通过以上步骤,您可以构建起一个稳定且高效的数据存储与分发系统。

    Java操作FastDFS文件上传

    在IT行业中,FastDFS是一个轻量级的分布式...总之,`fastdfs-client-java`库为Java开发者提供了方便的接口,以与FastDFS分布式文件系统交互。理解其工作原理和API使用方式,可以帮助我们高效地处理文件存储和管理任务。

    fastdfs-client-java-master.zip

    使用FastDFS客户端Java库时,开发者需要配置FastDFS服务器的地址和相关参数,然后可以通过API调用来执行以下操作: 1. 文件上传:将本地文件上传到FastDFS服务器,返回文件ID。 2. 文件下载:根据文件ID从FastDFS...

    FastDFS分布式文件系统安装、配置与使用,并搭配nginx

    **FastDFS配置** 1. **tracker服务配置**:`tracker.conf`主要配置监听端口、日志路径等。 2. **storage服务配置**:`storage.conf`包含存储路径、组名、数据和元数据的存储策略等。 3. **连接池配置**:如果使用...

    fastdfs-client-javajar1.29-SNAPSHOT.zip

    1. **安装与配置**:包括FastDFS服务器端的安装、配置以及启动,客户端Java库的引入和配置。 2. **连接管理**:理解和使用FastDFS的连接池,有效地管理和复用连接,以提高系统性能。 3. **文件上传**:通过Java ...

Global site tag (gtag.js) - Google Analytics