linux-FastDFS-配置实用

来源:互联网 发布:视频日语同声翻译软件 编辑:程序博客网 时间:2024/05/01 02:22

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了文件对应用存储透明和负载均衡的问题。它只能通过专有API对文件进行存取访问,和mogileFS、HDFS、TF类似,它不支持POSIX接口方式,不能mount, 只能说是应用级的分布式文件存储服务。FastDFS对于中小型规模应用具有轻量、简单维护方便等优势。


    FastDFS服务端有三个角色:跟踪服务器(tracker server)、存储服务器(storage server)和客户端(client)。

tracker server:跟踪服务器,主要做调度工作,起负载均衡的作用。在内存中记录集群中所有存储组和存储服务器的状态信息,是客户端和数据服务器交互的枢纽。

storage server:存储服务器(又称:存储节点或数据服务器),文件和文件属性(meta data)都保存到存储服务器上。Storage server直接利用OS的文件系统调用管理文件。

client:客户端,作为业务请求的发起方,通过专有接口,使用TCP/IP协议与跟踪器服务器或存储节点进行数据交互。





一、环境安装

1、FastDFS的依赖libevent库实现epoll等网络操作,如果操作系统上没有libevent则必须首先安装libevent。

wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz 

tar -zxvf libevent-2.0.21-stable.tar.gz  

cd libevent-2.0.21-stable 

./configure --prefix=/usr  

如果你没有root权限的话,也可以选择自己的安装目录

make clean

make  

make install 


2、编译安装FastDFS

wget https://fastdfs.googlecode.com/files/FastDFS_v4.06.tar.gz

tar -zxvf FastDFS_v4.06.tar.gz

 cd FastDFS


 可以修改安装路径,vim  make.sh

 修改以下配置为你想要的位置

TARGET_PREFIX=/home/fastdfs

TARGET_CONF_PATH=/home/fastdfs/conf

./make.sh C_INCLUDE_PATH=/usr/local/libevent-2.0.21/include   

LIBRARY_PATH=/usr/local/libevent-2.0.21/lib  

./make.sh install 

ls /home/fastdfs/conf

client.conf  http.conf  mime.types  storage.conf  tracker.conf 

二:FastDFS的配置

1、配置及启动Tracker Server:

vim /home/fastdfs/conf/tracker.conf 找到base_path=,修改为/home/fastdfs
cd /home/dfs/bin ./fdfs_trackerd /home/fastdfs/conf/tracker.conf若出现找不到libevent库的提示,还需要export LD_LIBRARY_PATH=/yourpath/libevent-2.0.21/lib: $LD_LIBRARY_PATH:

检查tracker是否启动成功,可以查看如下文件/home/fastdfs/logs/trackerd.log:

[2011-10-21 14:29:44] INFO - FastDFS v3.03, base_path=/home/fastdfs, run_by_group=, run_by_user=, connect_timeout=30s, network_timeout=60s, port=22122, bind_addr=, max_connections=256, work_threads=4, store_lookup=2, store_group=, store_server=0, store

2、配置及启动Storage Server:

mkdir /home/fastdfs/fdfs_storage  cd /home/fastdfs/conf  vi storage.conf  找到base_path=/home/yuqing/fastdfs 修改为 /home/fastdfs/fdfs_storage  找到store_path0=/home/yuqing/fastdfs 修改为 store_path0=/home/fastdfs/fdfs_storage  group_name=group1 修改tracker_server={trackerIP}:22122cd /home/dfs/bin ./fdfs_storaged/home/fastdfs/conf/storage.conf
[2011-10-21 14:49:20] INFO - FastDFS v3.03, base_path=/home/fastdfs/fdfs_storage, store_path_count=1, subdir_count_per_path=256, group_name=group1, run_by_group=, run_by_user=, connect_timeout=30s, network_timeout=60s, port=23000, bind_addr=, client_bind=1, max_connections=256, work_threads=4, disk_rw_separated=1, disk_reader_threads=..

三:测试及使用FastDFS

1、FastDFS之配置client:

vi /etc/fdfs/client.conf  base_path==/home/fastdfs  tracker_server=yours:22122

2、测试上传文件:

cd /home/dfs/bin   ./fdfs_test /home/fastdfs/conf/client.conf upload FastDFS_v4.06.tar.gz

返回

group1/M00/00/00/rfzI5E6hG_O5wNKxAAVAc72y1Jc_big.tar.gz

3、测试下载文件的java客户端代码

https://fastdfs.googlecode.com/files/fastdfs_client_v1.24.jar 

新建工程,将fastdfs_client_v1.24.jar加入lib path

新建配置文件fastdfs_client.conf放入classpath,内容如下

connect_timeout = 2

network_timeout = 30

charset = UTF-8

http.tracker_http_port = 8080

http.anti_steal_token = no

http.secret_key = FastDFS1234567890

tracker_server = yours:22122 



新建测试类

public class FastDFSClientDemo{

 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 = "group1/M00/00/00/rfzI5E6hG_O5wNKxAAVAc72y1Jc_big.tar.gz"

 FileInfo fi = storageClient.get_file_info(group_name, remote_filename); 

 byte [] filedata = storageClient.download_file(group_name, remote_filename);

 System.out.println(filedata.length); 

    }  

0 0
原创粉丝点击