FastDFS介绍

FastDFS是一款开源的高性能分布式文件系统(DFS)

主要功能:文件存储,文件同步和文件访问,以及高容量和负载平衡

主要解决了海量数据存储问题,特别适合以中小文件为载体的在线服务

FastDFS系统有三个角色:

跟踪服务器(Tracker Server)

存储服务器(Storage Server)

客户端(Clinet)

Tracker Server:跟踪服务器,主要做调节工作,起到均衡的作用;负责管理所有的storage server和group,每个storage在启动后会连接Tracker,告知自己所属group等信息,并保持周期性心跳。
Storage Server:存储服务器。主要提供容量和备份服务器;以group为单位,每个group内可以有多台storage server,数据互为备份。
Client:客户端,上传下载数据的服务器,也就是我们自己的项目部署在的服务器。


单机部署fastdfs

安装centos7.2系统,最小化安装

设置好网络和防火墙  网络需要能访问外网,

ip 192.168.0.201

下面关闭防火墙

setenforce 0

systemctl stop firewalld

systemctl disable firewalld

sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinux


设置yum源

cd /etc/yum.repos.d/

yum install vim unzip lrzsz wget net-tools -y

wget http://mirrors.aliyun.com/repo/Centos-7.repo

wget http://mirrors.aliyun.com/repo/epel-7.repo

yum -y install epel-release


安装依赖包

yum install zip zlib zlib-devel make cmake gcc gcc-c++ pcre pcre-devel openssl openssl-devel perl perl-devel -y


创建目录

mkdir -p /opt/fastdfs     #创建包目录

mkdir -p /data/fastdfs    #创建数据目录


安装libfastcommon

cd /opt/fastdfs/
wget
https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz
tar zxf V1.0.39.tar.gz 

cd libfastcommon-1.0.39/
./make.sh
 ./make.sh install


安装fastdfs

cd /opt/fastdfs/
 wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
 tar zxf V5.11.tar.gz
 cd fastdfs-5.11/
 ./make.sh
  ./make.sh install

创建配置文件

下面文件分别是tracker storage client nginx nginx 服务使用

cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
cp /opt/fastdfs/fastdfs-5.11/conf/http.conf /etc/fdfs/
cp /opt/fastdfs/fastdfs-5.11/conf/mime.types /etc/fdfs/


-------------------------------------------------------------------------------

-------------------------------------------------------------------------------

配置tracker

mkdir /data/fastdfs/tracker                ##创建tracker的日志数据目录

vim /etc/fdfs/tracker.conf

port=22122                                           ##tracker服务端口
base_path=/data/fastdfs/tracker        ##tracker数据日志目录
http.server_port=80                            ##http端口,必须与nginx的相同


编辑tracker启动文件

vim /usr/lib/systemd/system/fastdfs-tracker.service

[Unit]
Description=The FastDFS File server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
ExecStop=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
ExecRestart=/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

[Install]
WantedBy=multi-user.target

添加到开机启动

systemctl daemon-reload
systemctl enable fastdfs-tracker.service
systemctl start fastdfs-tracker.service

查看进程

netstat -lnupt |grep tracker

[root@localhost tmp]# netstat -lnupt |grep tracker
tcp    0   0 0.0.0.0:22122     0.0.0.0:*    LISTEN    24487/fdfs_trackerd

-------------------------------------------------------------------------

-------------------------------------------------------------------------


配置storage

mkdir /data/fastdfs/storage
vim /etc/fdfs/storage.conf

port=23000                                     # storage服务端口(默认23000,一般不修改)
base_path=/data/fastdfs/storage             # 数据和日志文件存储根目录

group_name=group1                      #默认组名,根据实际情况修改

store_path_count=1                          #存储路径个数,需要和store_path个数匹配

store_path0=/data/fastdfs/storage             # 第一个存储目录
tracker_server=192.168.0.201:22122             # tracker服务器IP和端口
http.server_port=80                         # http端口(默认8888,需要和nginx中80保持一致)


编辑storage启动文件

vim /usr/lib/systemd/system/fastdfs-storage.service

[Unit]
Description=The FastDFS File server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
ExecStop=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
ExecRestart=/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

[Install]
WantedBy=multi-user.target


添加到开机启动

systemctl daemon-reload
systemctl enable fastdfs-storage.service
systemctl start fastdfs-storage.service

查看进程

netstat -lnupt |grep storage

[root@localhost tmp]# netstat -lnupt |grep storage
tcp     0    0 0.0.0.0:23000    0.0.0.0:*     LISTEN    24544/fdfs_storaged


查看tracker和storage绑定状态

/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

FastDFS文件存储系统搭建_第1张图片

==============================================================


配置client

vim /etc/fdfs/client.conf

http.tracker_server_port=80                ##保持与tracker服务器的http端口相同

base_path=/data/fastdfs/tracker        ##tracker服务器文件目录

tracker_server=192.168.0.201:22122    ##tracker服务器的ip和端口


安装nginx和fastdfs-nginx-module

 下载安装

wget http://nginx.org/download/nginx-1.15.2.tar.gz
 wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz
 tar zxf nginx-1.15.2.tar.gz
 tar zxf V1.20.tar.gz

编译nginx

 cd nginx-1.15.2

vim /opt/fastdfs/fastdfs-nginx-module-1.20/src/config   ##修改module参数

ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

执行编译

 ./configure --prefix=/usr/local/nginx --add-module=/opt/fastdfs/fastdfs-nginx-module-1.20/src/

make && make install


配置mod-fastdfs.conf文件

 cp /opt/fastdfs/fastdfs-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs

 vim /etc/fdfs/mod_fastdfs.conf

base_path=/data/fastdfs
tracker_server=192.168.0.201:22122        ##tracker地址
storage_server_port=23000             ##storage server端口
group_name=group1
url_have_group_name = true                        ##设置为true,url包含group名
store_path_count=1                ##存储路径个数,与store_path的个数匹配
store_path0=/data/fastdfs/storage        ##存储文件位置

配置nginx

 vim /usr/local/nginx/conf/nginx.conf

        location ~/group[0-9]/M00 {
            root /data/fastdfs/storage/data;
            ngx_fastdfs_module;
        }

启动nginx
/usr/local/nginx/sbin/nginx

------------------------------------------

测试fastdfs

新建文档并用命令上传文件

touch test.xtx
echo "jhdkjfoiajgi" > test.xtx

/usr/bin/fdfs_upload_file  /etc/fdfs/client.conf test.xtx
/usr/bin/fdfs_upload_file  /etc/fdfs/client.conf /tmp/1.jpg

上传文件
[root@localhost home]# /usr/bin/fdfs_upload_file  /etc/fdfs/client.conf test.xtx 
group1/M00/00/00/wKgAyVzADQKAKftBAAAADUotZKc946.xtx
[root@localhost home]# /usr/bin/fdfs_upload_file  /etc/fdfs/client.conf /tmp/1.jpg 
group1/M00/00/00/wKgAyVzADYOAYzniAADtzKPg5UU160.jpg
查看文件 
[root@localhost home]# ll /data/fastdfs/storage/data/00/00/wKgAyVzADQKAKftBAAAADUotZKc946.xtx 
-rw-r--r-- 1 root root 13 4月  24 15:15 /data/fastdfs/storage/data/00/00/wKgAyVzADQKAKftBAAAADUotZKc946.xtx
[root@localhost home]# ll /data/fastdfs/storage/data/00/00/wKgAyVzADYOAYzniAADtzKPg5UU160.jpg 
-rw-r--r-- 1 root root 60876 4月  24 15:17 /data/fastdfs/storage/data/00/00/wKgAyVzADYOAYzniAADtzKPg5UU160.jpg

通过wget下载

[root@node1 home]# wget http://192.168.0.201/group1/M00/00/00/wKgAyVzADYOAYzniAADtzKPg5UU160.jpg
--2019-04-24 15:18:17--  http://192.168.0.201/group1/M00/00/00/wKgAyVzADYOAYzniAADtzKPg5UU160.jpg
正在连接 192.168.0.201:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:60876 (59K) [image/jpeg]
正在保存至: “wKgAyVzADYOAYzniAADtzKPg5UU160.jpg”
100%[====================================================>] 60,876      --.-K/s 用时 0.005s  
2019-04-24 15:18:17 (11.9 MB/s) - 已保存 “wKgAyVzADYOAYzniAADtzKPg5UU160.jpg” [60876/60876])

通过浏览器访问

 http://192.168.0.201/group1/M00/00/00/wKgAyVzADYOAYzniAADtzKPg5UU160.jpg

FastDFS文件存储系统搭建_第2张图片


服务端下载文件操作

/usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKgAyVzADQKAKftBAAAADUotZKc946.xtx

服务端删除文件操作

/usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKgAyVzADQKAKftBAAAADUotZKc946.xtx

到这里整个fastdfs搭建完成了。


fastdfs可参考文章:

https://www.cnblogs.com/handsomeye/p/9451568.html

https://www.cnblogs.com/sunnydou/p/49b92d511047f4f9da6cd727cfd415d5.html

http://www.bubuko.com/infodetail-2296971.html