CentOS7 搭建FastDFS分布式文件系统1

  1. FastDFS 搭建
  2. FastDFS 整合 Nginx
  3. FastDFS 整合 Spring Boot 上传

1. FastDFS 介绍

1.1 简介

FastDFS的作者淘宝资深架构余庆,这个优秀的轻量及的分布式文件系统的开源没多久,立马就火了。FastDFS是为互联网应用量身定做的一套分布式文件存储系统,非常适合用来存储用户图片、视频、文档等文件。对于互联网应用,和其他分布式文件系统相比,优势非常明显。

1.2 详细介绍

FastDFS是一个开源的分布式文件系统,她对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。跟踪器主要做调度工作,在访问上起负载均衡的作用。

存储节点存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,FastDFS同时对文件的meta data进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value pair)方式表示,如:width=1024,其中的key为width,value为1024。文件meta data是文件属性列表,可以包含多个键值对。

跟踪器和存储节点都可以由一台或多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。

为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷 的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起 到了冗余备份和负载均衡的作用。

在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。

当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。
FastDFS中的文件标识分为两个部分:卷名和文件名,者缺一不可。

1.3 上传文件交互过程

  1. client询问tracker上传到的storage,不需要附加参数;
  2. tracker返回一台可用的storage;
  3. client直接和storage通讯完成文件上传。

1.4 下载文件交互过程

  1. client询问tracker下载文件的storage,参数为文件标识(卷名和文件名);
  2. tracker返回一台可用的storage;
  3. client直接和storage通讯完成文件下载。

2.FastDFS安装准备

作者的GitHub地址:https://github.com/happyfish100

https://github.com/happyfish100/fastdfs

wget https://github.com/happyfish100/fastdfs/tree/V5.11

Version 5.11 对应的 fastdfs-nginx-module 的Version 1.20

Version 5.10 对应的 fastdfs-nginx-module 的Version 1.19

下载其它工具:

  1. libfastcommon
  2. fastdfs-nginx-module
  3. fastdfs-client-java
  4. nginx
wget https://github.com/happyfish100/libfastcommon/archive/master.zip
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip
wget https://github.com/happyfish100/fastdfs-client-java/archive/master.zip
wget http://nginx.org/download/nginx-1.13.11.tar.gz

3. 安装libfastcommon

先安装解压工具:

yum -y install unzip zip

安装成功后解压 libfastcommon-master.zip

unzip libfastcommon-master.zip

进入刚解压的目录:

[root@localhost FastDFS]# cd libfastcommon-master/
[root@localhost libfastcommon-master]# ls
doc  HISTORY  INSTALL  libfastcommon.spec  make.sh  php-fastcommon  README  src

编译前,先安装 gcc

yum -y install gcc-c++

然后执行:

[root@localhost libfastcommon-master]# ./make.sh
[root@localhost libfastcommon-master]# ./make.sh install

libfastcommon默认会被安装到/usr/lib64/libfastcommon.so但是FastDFS的主程序却在/usr/local/lib目录下,这个时候我们就要建立一个软链接了。

ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

2.安装FastDFS

2.1 解压 FastDFS 安装包

unzip fastdfs-master.zip

进入刚解压的目录:

2.2 编译安装

[root@localhost FastDFS]# cd fastdfs-master/
[root@localhost fastdfs-master]# ./make.sh
[root@localhost fastdfs-master]# ./make.sh install

如果没有报错那么就成功了。

安装log中会提示FastDFS安装到了/etc/fdfs目录下。

成功后查看安装目录:

[root@localhost fastdfs-master]# cd /etc/fdfs/                                                           

[root@localhost fdfs]# ll
-rw-r--r--. 1 root root  1461 Apr  7 23:02 client.conf.sample
-rw-r--r--. 1 root root  7978 Apr  7 23:02 storage.conf.sample
-rw-r--r--. 1 root root   105 Apr  7 23:02 storage_ids.conf.sample
-rw-r--r--. 1 root root  7441 Apr  7 23:02 tracker.conf.sample

我们需要把这三个示例文件复制一份,去掉.sample。

cp client.conf.sample client.conf
cp storage.conf.sample storage.conf
cp tracker.conf.sample tracker.conf

FastDFS 安装完成。

3.安装tracker

3.1 创建tracker工作目录

这个目录可以自定义创建,用来保存tracker的data和log

下面创建的目录:

[root@localhost ~]# cd /usr/
[root@localhost usr]# mkdir xiao
[root@localhost usr]# cd xiao/
[root@localhost xiao]# mkdir fastdfs
[root@localhost xiao]# cd fastdfs/
[root@localhost fastdfs]# mkdir fastdfs_tracker
[root@localhost fastdfs]# cd fastdfs_tracker/
[root@localhost fastdfs_tracker]# pwd
/usr/xiao/fastdfs/fastdfs_tracker        #这个是我最终创建的目录

3.2 配置tracker

cd /etc/fdfs
vim tracker.conf

打开后注意以下4个配置:

disabled=false  #默认开启 
port=22122      #默认端口号 
base_path=/usr/xiao/fastdfs/fastdfs_tracker #我刚刚创建的目录 
http.server_port=6666   #默认端口是8080

3.3 启动tracker

保存配置后启动tracker:

service fdfs_trackerd start

如果不能启动,或提示用systemctl,则可用命令:

systemctl start fdfs_trackerd

成功后可以看到:

[root@localhost fdfs]# service fdfs_trackerd start
Starting fdfs_trackerd (via systemctl):                    [  OK  ]

进入刚刚创建的tracker目录,发现目录中多了data和log两个目录:

[root@localhost fdfs]# cd /usr/xiao/fastdfs/fastdfs_tracker/
[root@localhost fastdfs_tracker]# ll
total 0
drwxr-xr-x 2 root root 178 Apr  7 21:19 data
drwxr-xr-x 2 root root  26 Apr  7 22:01 logs

3.4 将tracker加入开机启动

[root@localhost fastdfs_tracker]# ll /etc/rc.d/rc.local
-rw-r--r-- 1 root root 501 Jun 16 21:34 /etc/rc.d/rc.local

发现并没有执行权限,需要权限:

chmod +x /etc/rc.d/rc.local

加完成应该是这样的:

-rwxr-xr-x 1 root root 501 Jun 16 21:34 /etc/rc.d/rc.local

修改rc.local

vim /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
service fdfs_trackerd start

查看一下tracker的端口监听情况

[root@localhost fastdfs_tracker]# netstat -unltp|grep fdfs
tcp        0      0 0.0.0.0:22122           0.0.0.0:*               LISTEN      2231/fdfs_trackerd

端口22122成功监听

4.安装storage

storage的安装与tracker很类似.

4.1 为storage配置工作目录

与tracker不现的是,由于storage还需要一个目录用来存储数据,所以我另外多建了一个fasdfs_storage_data
下面是我的目录结构:

[root@localhost fastdfs]# ls
fastdfs_storage  fastdfs_storage_data  fastdfs_tracker

4.2修改storage配置文件

修改 storage.conf

vim /etc/fdfs/storage.conf
disabled=false 
group_name=group1 #组名,根据实际情况修改 
port=23000 #设置storage的端口号,默认是23000,同一个组的storage端口号必须一致 
base_path=/usr/xiao/fastdfs/fastdfs_storage #设置storage数据文件和日志目录
store_path_count=1 #存储路径个数,需要和store_path个数匹配 
base_path0=/usr/xiao/fastdfs/fastdfs_storage_data #实际文件存储路径 
tracker_server=192.168.235.32:22122 #我CentOS7的ip地址 
http.server_port=8888 #设置 http 端口号

修改保存后创建软链接:

ln -s /usr/bin/fdfs_storaged /usr/local/bin

4.3 启动storage

service fdfs_storaged start

如果不能启动,或提示用systemctl,则可改用命令:

systemctl start fdfs_storaged

成功后应该可以看到:

[root@localhost fdfs]# service fdfs_stroaged start
Starting fdfs_storaged (via systemctl):                    [  OK  ]

4.4 设置开机启动

修改rc.local

vim /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
service fdfs_trackerd start
service fdfs_storaged start

查看一下服务是否启动:

[root@localhost fastdfs]# netstat -unltp | grep fdfs
tcp   0   0 0.0.0.0:22122   0.0.0.0:*   LISTEN   2231/fdfs_trackerd
tcp   0   0 0.0.0.0:23000   0.0.0.0:*   LISTEN   2323/fdfs_storaged

服务已正常启动。

5 校验整合

到这里,fastdfs的东西都已安装完成,最后我们还要确定一下,storage是否注册到了tracker中去。

查看命令:

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

成功后可以看到:

ip_addr = 192.168.235.32 (localhost.localdomain) ACTIVE
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值