fastDFS 是一个轻量级的开源分布式文件系统,主要解决了大容量的文件存储和高并发访问的问题,文件存取时实现了负载均衡,同时支持存储服务器在线扩容。
fastDFS 是基于源代码本地编译、安装、部署的,所以在使用前首先要部署。网上也有不少 fastDFS 的部署教程,不过大部分都不能正确操作完毕或者最后不能正确使用,本文经过在 CentOS 7 上实测,可正常使用。

1. 安装 libfastcommon

下载:

1
wget -O libfastcommon-1.0.7.tar.gz https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz

解压:

1
tar -zxvf libfastcommon-1.0.7.tar.gz

编译:

1
2
cd libfastcommon-1.0.7
sudo ./make.sh

安装:

1
sudo ./make.sh install

2. 安装 FastDFS

下载:

1
wget -O fastdfs-5.05.tar.gz https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz

解压:

1
tar -zxvf fastdfs-5.05.tar.gz

编译:

1
2
cd fastdfs-5.05
sudo ./make.sh

安装:

1
sudo ./make.sh install

3. 配置 Tracker 服务

复制 tracker.conf :

1
cp /etc/fdfs/tracker.conf.sample /home/deployer/fastdfs/tracker.conf

配置 tracker.conf :

1
2
# the base path to store data and log files
base_path=/home/deployer/fastdfs

启动 tracker 服务:

1
2
3
4
5
6
# start fdfs_trackerd
fdfs_trackerd /home/deployer/fastdfs/tracker.conf start
# restart fdfs_trackerd
fdfs_trackerd /home/deployer/fastdfs/tracker.conf restart
# stop fdfs_trackerd
fdfs_trackerd /home/deployer/fastdfs/tracker.conf stop

检查服务:

1
netstat -unltp|grep fdfs

4. 配置 Storage 服务

复制 storage.conf :

1
cp /etc/fdfs/storage.conf.sample /home/deployer/fastdfs/storage.conf

配置 storage.conf :

1
2
3
4
5
6
7
8
9
10
# the base path to store data and log files
base_path=/home/deployer/fastdfs/storage

# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
store_path0=/home/deployer/fastdfs/storage

# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.10.204:22122

启动 storage 服务:

1
2
3
4
5
6
# start fdfs_storaged
fdfs_storaged /home/deployer/fastdfs/storage.conf start
# restart fdfs_storaged
fdfs_storaged /home/deployer/fastdfs/storage.conf restart
# stop fdfs_storaged
fdfs_storaged /home/deployer/fastdfs/storage.conf stop

检查服务:

1
netstat -unltp|grep fdfs

启动成功后可以在 /home/deployer/fastdfs/storage 目录下看到众多文件夹。

检查连通性:

1
fdfs_monitor /home/deployer/fastdfs/storage.conf

5.测试

复制 client.conf :

1
cp /etc/fdfs/client.conf.sample /home/deployer/fastdfs/client.conf

配置 client.conf :

1
2
3
4
5
6
# the base path to store log files
base_path=/home/deployer/server/fastdfs/data

# tracker_server can ocur more than once, and tracker_server format is
# "host:port", host can be hostname or ip address
tracker_server=192.168.10.204:22122

上传文件:

1
fdfs_test /home/deployer/fastdfs/client.conf upload /home/deployer/hello.txt

上传文件会输出类似如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
This is FastDFS client test program v5.05

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.

[2017-08-01 17:12:09] DEBUG - base_path=/home/deployer/server/fastdfs/data, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.10.204, port=23000

group_name=group1, ip_addr=192.168.10.204, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKgKzFmARemAXLmOAAAAE2nrDUs542.txt
source ip address: 192.168.10.204
file timestamp=2017-08-01 17:12:09
file size=19
file crc32=1777012043
example file url: http://192.168.10.204/group1/M00/00/00/wKgKzFmARemAXLmOAAAAE2nrDUs542.txt
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKgKzFmARemAXLmOAAAAE2nrDUs542_big.txt
source ip address: 192.168.10.204
file timestamp=2017-08-01 17:12:09
file size=19
file crc32=1777012043
example file url: http://192.168.10.204/group1/M00/00/00/wKgKzFmARemAXLmOAAAAE2nrDUs542_big.txt

group_name=group1 表示上传文件到 group1 组下

remote_filename=M00/00/00/wKgKzFmARemAXLmOAAAAE2nrDUs542.txt 表示保存在 fdfs server 中的文件名

下载文件:

1
fdfs_test client.conf download group1 M00/00/00/wKgKzFmARemAXLmOAAAAE2nrDUs542_big.txt

下载文件会输出类似如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
This is FastDFS client test program v5.05

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.

[2017-08-01 17:59:27] DEBUG - base_path=/home/deployer/server/fastdfs/data, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0

storage=192.168.10.123:23000
download file success, file size=19, file save to wKgKzFmARemAXLmOAAAAE2nrDUs542_big.txt