FastDFS 是一个开源的高性能分布式文件系统(Distributed File System),由淘宝开发平台部资深架构师余庆开发。作为一个分布式文件系统,它对文件进行管理,主要功能包括功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等.

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第1张

一 FastDFS介绍

开源中国:https://www.oschina.net/question/tag/FastDFSgitHub

开源地址:https://github.com/happyfish100

参考:http://blog.chinaunix.net/uid-20196318-id-4058561.html

1.1 简介

FastDFS 是一个开源的高性能分布式文件系统(Distributed File System),由淘宝开发平台部资深架构师余庆开发。作为一个分布式文件系统,它对文件进行管理,主要功能包括功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,主要解决了海量数据存储问题,特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务。

FastDFS 系统有三个角色:跟踪服务器(Tracker Server)、存储服务器(Storage Server)和客户端(Client)。

  • Tracker Server:FastDFS的协调者,起到均衡的作用;负责管理所有的 storage server和 group,每个 storage 在启动后会连接 Tracker,告知自己所属 group 等信息,并保持周期性心跳,tracker根据storage的心跳信息,建立group==>[storage serverlist]的映射表。
  • Storage Server:存储服务器,主要提供容量和备份服务;以组(卷,group或volume)为单位组织,一个group内包含多台storage机器,数据互为备份,存储空间以group内容量最小的storage为准,所以建议group内的多个storage尽量配置相同,以免造成存储空间的浪费。
  • Client:客户端,作为业务请求的发起方,通过专有接口,使用TCP/IP协议与跟踪器服务器或存储节点进行数据交互,就是我们项目所部署在的服务器。

1.2 FastDFS架构图

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第2张

1.3 FastDFS的存储策略

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

在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。

1.4 FastDFS的上传过程

FastDFS向使用者提供基本文件访问接口,比如upload、download、append、delete等,以客户端库的方式提供给用户使用。

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第3张

Storage Server会定期的向Tracker Server发送自己的存储信息。当Tracker Server Cluster中的Tracker Server不止一个时,各个Tracker之间的关系是对等的,所以客户端上传时可以选择任意一个Tracker。

当Tracker收到客户端上传文件的请求时,会为该文件分配一个可以存储文件的group,当选定了group后就要决定给客户端分配group中的哪一个storage server。当分配好storage server后,客户端向storage发送写文件请求,storage将会为文件分配一个数据存储目录。然后为文件分配一个fileid,最后根据以上的信息生成文件名存储文件。如下图所示:

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第4张

1.5 FastDFS的文件同步

写文件时,客户端将文件写至group内一个storage server即认为写文件成功,storage server写完文件后,会由后台线程将文件同步至同group内其他的storage server。

每个storage写文件后,同时会写一份binlog,binlog里不包含文件数据,只包含文件名等元信息,这份binlog用于后台同步,storage会记录向group内其他storage同步的进度,以便重启后能接上次的进度继续同步;进度以时间戳的方式进行记录,所以最好能保证集群内所有server的时钟保持同步。

storage的同步进度会作为元数据的一部分汇报到tracker上,tracke在选择读storage的时候会以同步进度作为参考。

比如一个group内有A、B、C三个storage server,A向C同步到进度为T1 (T1以前写的文件都已经同步到B上了),B向C同步到时间戳为T2(T2 > T1),tracker接收到这些同步进度信息时,就会进行整理,将最小的那个做为C的同步时间戳,本例中T1即为C的同步时间戳为T1(即所有T1以前写的数据都已经同步到C上了);同理,根据上述规则,tracker会为A、B生成一个同步时间戳。

1.6 FastDFS的文件下载

客户端uploadfile成功后,会拿到一个storage生成的文件名,接下来客户端根据这个文件名即可访问到该文件。

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第5张

跟upload file一样,在downloadfile时客户端可以选择任意tracker server。tracker发送download请求给某个tracker,必须带上文件名信息,tracke从文件名中解析出文件的group、大小、创建时间等信息,然后为该请求选择一个storage用来服务读请求

二 FastDFS 环境安装

2.1 准备

操作环境CentOS 6.4

将所有的下载文件到放到package包下,统一管理

2.2 下载安装 libfastcommon

1.下载libfastcommon

  1. #wgethttps://github.com/happyfish100/libfastcommon/archive/V1.0.38.tar.gz

2.解压文件

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38

3.编译安装

  1. #./make.sh
  2. #./make.shinstall

4.编译通过后, libfastcommon.so安装到了 /usr/lib64下,在32位中会安装在 /usr/lib/中,在64位会在 /usr/lib64/

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第6张

5.创建软链接,因为FastDFS主程序设置的lib目录是/usr/local/lib,所以需要我们创建软链接

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so

2.3 下载安装FastDFS

1 下载FastDFS

  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz

2 解压

  1. #tar-zxvfV5.11.tar.gz
  2. #cdfastdfs-5.11

3 编译安装

  1. #./make.sh
  2. #./make.shinstall

4 默认安装目录

服务脚本: cd/etc/init.d目录

  1. /etc/
  2. init.d/fdfs_storaged
  3. /etc/init.d/fdfs_tracker

配置文件: cd/etc/fdfs,有四个.sample后缀的文件(自动生成的fdfs模板配置文件)

  1. /etc/
  2. fdfs/client.conf.sample
  3. /etc/fdfs/storage.conf.sample
  4. /etc/fdfs/storage_ids.conf.sample
  5. /etc/fdfs/tracker.conf.sample

命令脚本:

  1. fdfs_appender_test
  2. fdfs_appender_test1
  3. fdfs_append_file
  4. fdfs_crc32
  5. fdfs_delete_file
  6. fdfs_download_file
  7. fdfs_file_info
  8. fdfs_monitor
  9. fdfs_storaged
  10. fdfs_test
  11. fdfs_test1
  12. fdfs_trackerd
  13. fdfs_upload_appender
  14. fdfs_upload_file
  15. stop.sh
  16. restart.sh

也可以查看可执行命令:ls-la/usr/bin/fdfs*

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
0

5 设置脚本软链接 FastDFS 服务脚本设置的 bin 目录是 /usr/local/bin, 但实际命令安装在 /usr/bin/ 下

可以通过建立 /usr/bin 到 /usr/local/bin 的软链接

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
1

2.4 配置Tracker服务

1、进入/etc/fdfs目录,通过cp命令复制tracker.conf.sample,重命名为:tracker.con(删除.sample后缀),作为正式文件

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
2

2、编辑tracker.conf:vi tracker.conf,修改相关参数(只需要修改:==basepath== 和 ==http.serverport== 即可)

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
3

3、创建tracker基础数据目录,即base_path对应的目录

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
4

4、开放 22122 端口

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
5

5、启动tracker(支持start|stop|restart):

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
6

成功启动后,会在 /home/lyy/fastdfs(配置的basepath)下创建 data、logs 两个目录。

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第7张

6 、查看启动日志进入刚刚指定的basepath(/home/lyy/fastdfs)中有个logs目录,查看tracker.log文件

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第8张

7、查看端口情况

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
7

如果22122端口正在被监听,则Tracker服务安装成功,如下图所示:

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第9张

Tracker 关闭命令:service fdfs_trackerd stop

8、设置Tracker开机启动

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
8

2.5 配置Storage服务

1、进入/etc/fdfs目录,有cp命令拷贝storage.conf.sample,重命名为 storage.conf(删除.sample)后缀作为正式文件

  1. #tar-zxvfV1.0.38.tar.gz
  2. #cdlibfastcommon-1.0.38
9

2、编辑storage.conf,修改相关参数(==basepath==、==storepath0==、==trackerserver==、==http.serverport==)

  1. #./make.sh
  2. #./make.shinstall
0

3、创建Storage基础数据目录,对应base_path目录

  1. #./make.sh
  2. #./make.shinstall
1

4、启动 Storage

  1. #./make.sh
  2. #./make.shinstall
2

5、查看Storage启动日志查看storage启动日志:进入刚刚指定的base_path(/home/lyy/storage/fastdfs)中有个logs目录,查看storage.log文件

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第10张

6、查看端口情况:netstat-apn|grep fdfs

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第11张

关闭Storage命令:service fdfs_storaged stop重新启动Storage命令:service fdfs_storaged restart

7、通过monitor来查看storage是否成功绑定,查看storage和Tracker是否通信成功

  1. #./make.sh
  2. #./make.shinstall
3

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第12张

8、设置 Storage 开机启动

  1. #./make.sh
  2. #./make.shinstall
4

9、Storage 目录在 Tracker和Storage 启动成功后,在basepath(/home/lyy/storage/file) 下创建了data、logs目录,记录 Storage Server 的信息。在 storepath0 目录下,创建了N*N个子目录:

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第13张

三 安装Nginx

1、下载Nginx安装包:

  1. #./make.sh
  2. #./make.shinstall
5

2、解压nginx:

  1. #./make.sh
  2. #./make.shinstall
6

3、进入nginx目录

  1. #./make.sh
  2. #./make.shinstall
7

4、安装依赖库

  1. #./make.sh
  2. #./make.shinstall
8

5、配置nginx,加载fastdfs-nginx-module模块

  1. #./make.sh
  2. #./make.shinstall
9

6、编译安装nginx

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
0

7、查看nginx安装路径:whereis nginx

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第14张

8、启动停止:

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
1

9、设置开机启动

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
2

10、查询nginx版本

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
3

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第15张

11、防火墙中打开Nginx

80 端口

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
4

12、通过浏览器访问nginx

查看nginx是否启动成功,输入我们的ip地址(我的是:192.168.50.198)默认是80端口,看到下图所示说明nginx启动成功:

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第16张

四 文件上传测试

4.1 修改 nginx.conf文件

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
5

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第17张

4.2 Tracker 服务器中的客户端配置文件

1、修改配置clinet配置

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
6

修改如下配置

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
7

创建文件夹

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
8

4.3 上传测试

在linux内部执行如下命令上传 1.jpg图片

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
9

成功后返回文件ID号:group1/M00/00/00/wKgyxl2dfNWAG9cOAAOUD-69J7A524.jpg

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第18张
  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
0

4.4 通过wget和浏览器方式访问成功

浏览器

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第19张

wget 方式

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第20张

五 安装配置fastdfs-nginx-module模块

5.1 fastdfs-nginx-module 模块说明

FastDFS 通过 Tracker 服务器,将文件放在 Storage 服务器存储, 但是同组存储服务器之间需要进行文件复制, 有同步延迟的问题。

假设 Tracker 服务器将文件上传到了 192.168.50.196,上传成功后文件 ID已经返回给客户端。

此时 FastDFS 存储集群机制会将这个文件同步到同组存储 192.168.50.197,在文件还没有复制完成的情况下,客户端如果用这个文件 ID 在 192.168.50.197 上取文件,就会出现文件无法访问的错误。

而 fastdfs-nginx-module 可以重定向文件链接到源服务器取文件,避免客户端由于复制延迟导致的文件无法访问错误。

5.2 安装fastdfs-nginx-module安装包

1、下载fastdfs-nginx-module安装包

  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
1

2、配置Nginx

  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
2
  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
3

3、查看nginx版本

  1. #ln-s/usr/lib64/libfastcommon.so/usr/local/lib/libfastcommon.so
  2. #ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so
  3. #ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so
  4. #ln-s/usr/lib64/libfdfsclient.so/usr/lib/libfdfsclient.so
3

如下图所示,说明配置成功

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第21张

5.3 配置Nginx和fastdfs-nginx-module模块

1、配置mod-fastdfs.conf,并拷贝到/etc/fdfs文件目录下

  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
5

2、修改mod-fastdfs.conf

  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
6

修改以下配置:

  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
7

3、拷贝 FastDFS 解压目录中(anti-steal.jpg、http.conf、mime.types)到/etc/fdfs 目录下

  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
8

4、配置nginx,修改nginx.conf

  1. #wgethttps://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
9

80端口下添加fastdfs-nginx模块,将之前的的配置注释掉

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第22张

5、启动nginx

  1. #tar-zxvfV5.11.tar.gz
  2. #cdfastdfs-5.11
0

6、 通过wget和浏览器方式访问成功

浏览器

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第23张

wget 方式

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第24张

结构图(网上找的图,不是原创):

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第25张

六 FastDFS 常用测试命令

6.1 - 上传文件

  1. #tar-zxvfV5.11.tar.gz
  2. #cdfastdfs-5.11
1

6.2 - 下载文件

  1. #tar-zxvfV5.11.tar.gz
  2. #cdfastdfs-5.11
2

查看结果:

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第26张

6.3 - 删除文件

  1. #tar-zxvfV5.11.tar.gz
  2. #cdfastdfs-5.11
3

返回结果:

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第27张

我是牧小农,怕什么真理无穷,进一步有进一步的欢喜,大家加油~!

 FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件) FastDFS 开源 数据存储 第28张

转载请说明出处
知优网 » FastDFS搭建文件管理系统(详细教程)(fastdfs配置文件)

发表评论

您需要后才能发表评论