FastDFS原理

                                                                      FastDFS原理

  1. 配置tracker
  2. 配置storage
  3. 文件测试及web访问

1 配置tracker

1.1 问题

FastDFS是一个分布式文件系统,主要的服务器角色有TrackerStorage。本例安装一台Tracker,实现以下功能:

  • 接受客户端的访问
  • 检索存储节点,为客户端提供一台可用的storage

1.2 方案

准备一台虚拟机,安装FastDFS Tracker

主机的主机名及对应的IP地址如表-1所示。

表-1 主机名称及对应IP地址表

1.3 步骤

实现此案例需要按照如下步骤进行。

1)配置yum源,并安装所需的软件包。

  1. [root@fdfs_tracker1 ~]# yum install -y gcc libevent make
  2. [root@fdfs_tracker1 ~]# yum install -y libevent-devel-*.rpm

2)解压缩FastDFS安装包,查看安装说明文档进行安装。

  1. [root@fdfs_tracker1 fdfs]# tar xzf FastDFS_v4.06.tar.gz
  2. [root@fdfs_tracker1 fdfs]# cd FastDFS
  3. [root@fdfs_tracker1 FastDFS]# vim INSTALL
  4. [root@fdfs_tracker1 fdfs]# ./make.sh
  5. [root@fdfs_tracker1 fdfs]# ./make.sh install
  6. [root@fdfs_tracker1 FastDFS]# vim INSTALL
  7. [root@fdfs_tracker1 FastDFS]# cp init.d/fdfs_trackerd /etc/init.d/

3)创建存储目录(存储日志等)。

  1. [root@fdfs_tracker1 FastDFS]# mkdir -pv /data/fastdfs

4)修改配置文件。

  1. [root@fdfs_tracker1 FastDFS]# vim /etc/fdfs/tracker.conf
  2. base_path=/data/fastdfs/      设置基础目录
  3. max_connections=4096          设置最大连接数
  4. store_group=group1           # 设置存储组名
  5. use_storage_id = true         使用stroage_id代替ip地址

5)创建storage_id文件。

  1. [root@fdfs_tracker1 FastDFS]# cp conf/storage_ids.conf /etc/fdfs/

6)编辑storage id文件,加入存储组声明。

  1. [root@fdfs_tracker1 FastDFS]# vim /etc/fdfs/storage_ids.conf
  2. 100001   group1  192.168.113.60

7)修改客户端配置文件。

  1. [root@fdfs_tracker1 FastDFS]# vim /etc/fdfs/client.conf
  2. base_path=/data/fastdfs
  3. tracker_server=192.168.113.50:22122

8)控制tracker启动。

  1. [root@fdfs_tracker1 FastDFS]# /etc/init.d/fdfs_trackerd
  2. 用法:/etc/init.d/fdfs_trackerd {start|stop|status|restart|condrestart}

9)查看tracker运行状态。

  1. [root@fdfs_tracker1 FastDFS]# /etc/init.d/fdfs_trackerd status
  2. fdfs_trackerd (pid 28928) 正在运行...
  3. [root@fdfs_tracker1 FastDFS]# netstat -tlnp | grep tracker
  4. tcp  0    0   0.0.0.0:22122      0.0.0.0:*    LISTEN      28928/fdfs_trackerd

配置storage

2.1 问题

安装FastDFS Storage存储节点,存储节点被划分到相应的组里。同一个组中的服务器保存完全相同的内容, 不同分组存不同的内容。存储节点具有以下功能:

  • 接受Tracker的调度
  • 响应客户端请求
  • 将客户端发来的文件存储至相应存储位置
  • 读取请求的文件,发送给客户端

2.2 方案

准备一台虚拟机,安装FastDFS Tracker

主机的主机名及对应的IP地址如表-2所示。

表-2 主机名称及对应IP地址表

 

2.3 步骤

实现此案例需要按照如下步骤进行。

1)配置yum源,并安装所需的软件包。

  1. [root@fdfs_storage1 ~]# yum install -y gcc libevent make
  2. [root@fdfs_storage1 ~]# yum install -y libevent-devel-*.rpm

2)解压缩、查看安装指南进行安装

  1. [root@fdfs_storage1 fdfs]# tar xzf FastDFS_v4.06.tar.gz
  2. [root@fdfs_storage1 fdfs]# cd FastDFS
  3. [root@fdfs_storage1 FastDFS]# vim INSTALL
  4. [root@fdfs_storage1 fdfs]# ./make.sh
  5. [root@fdfs_storage1 fdfs]# ./make.sh install
  6. [root@fdfs_storage1 FastDFS]# vim INSTALL
  7. [root@fdfs_storage1 FastDFS]# cp init.d/fdfs_storaged /etc/init.d/

3)创建数据存储目录(存储日志、数据文件等)

  1. [root@fdfs_storage1 FastDFS]# mkdir -pv /data/fastdfs

4)修改配置文件。

  1. [root@fdfs_storage1 FastDFS]# vim /etc/fdfs/storage.conf
  2. base_path=/data/fastdfs/       设置基础工作目录
  3. max_connections=4096            设置最大连接数
  4. store_path0=/data/fastdfs/     设置上传文件目录
  5. tracker_server=192.168.113.50:22122     设置连接的Tracker
  6. upload_priority=10               设置组内服务器优先级

5)控制storage启动

  1. [root@fdfs_storage1 FastDFS]# /etc/init.d/fdfs_storaged
  2. 用法:/etc/init.d/fdfs_storaged {start|stop|status|restart|condrestart}

6)查看storage工作状态

  1. [root@fdfs_storage1 FastDFS]# /etc/init.d/fdfs_storaged status
  2. fdfs_storaged (pid 28916) 正在运行...
  3. [root@fdfs_storage1 FastDFS]# netstat -tlnp | grep storage
  4. tcp        0      0 0.0.0.0:23000               0.0.0.0:*                   LISTEN      28916/fdfs_storaged

 

 文件测试及web访问

3.1 问题

为实现文件的上传、下载和删除等功能,FastDFS提供相应的命令行工具。测试这些工具的使用。

为了方便用户访问,可以在storage上配置web访问功能,使得文件通够通过web页面进行访问。

3.2 步骤

步骤一:通过fdfs_test

1)上传文件

测试程序会对一个文件上传两次。返回的文件id也是两个并上传文件附加属性。storage上生成4个文件

  1. [root@fdfs_tracker ~]# /usr/local/bin/fdfs_test /etc/fdfs/client.conf upload install.log

查看上传到storage的文件

  1. [root@fdfs_storage1 FastDFS]# ls /data/fastdfs/data/00/00/
  2. wKhxPFc3oD6ARewxAAC6OPZ7hp8271_big.log    wKhxPFc3oD6ARewxAAC6OPZ7hp8271.log
  3. wKhxPFc3oD6ARewxAAC6OPZ7hp8271_big.log-m  wKhxPFc3oD6ARewxAAC6OPZ7hp8271.log-m

2)删除文件

  1. [root@fdfs_tracker1 fastdfs]# /usr/local/bin/fdfs_test /etc/fdfs/client.conf
  2. delete group1 M00/00/00/wKhxPFc3oD6ARewxAAC6OPZ7hp8271_big.log

步骤二:通过其他相关命令

1)通过fdfs_upload_file上传文件

  1. [root@fdfs_tracker1 fastdfs]# /usr/local/bin/fdfs_upload_file
  2. /etc/fdfs/client.conf /etc/passwd
  3. group1/M00/00/00/wKhxPFc3pEyAFrHBAAAGKI1fx1k5058918

2)通过fdfs_download_file下载文件

  1. [root@fdfs_tracker1 ~]# /usr/local/bin/fdfs_download_file /etc/fdfs/client.conf
  2. group1/M00/00/00/wKhxPFc3pEyAFrHBAAAGKI1fx1k5058918

3)通过fdfs_delete_file删除文件

  1. [root@fdfs_tracker1 ~]# /usr/local/bin/fdfs_delete_file /etc/fdfs/client.conf
  2. group1/M00/00/00/wKhxPFc3pEyAFrHBAAAGKI1fx1k5058918

步骤三:配置web访问

1)在storage上安装nginx

  1. [root@fdfs_storage1 ~]# yum install -y pcre pcre-devel zlib zlib-devel
  2. [root@fdfs_storage1 fdfs]# tar xzf fastdfs-nginx-module_v1.16.tar.gz
  3. [root@fdfs_storage1 fdfs]# tar xzf nginx-1.7.10.tar.gz
  4. [root@fdfs_storage1 fdfs]# cd nginx-1.7.10
  5. [root@fdfs_storage1 nginx-1.7.10]# useradd -s /sbin/nologin nginx
  6. [root@fdfs_storage1 nginx-1.7.10]# ./configure --prefix=/usr/local/nginx
  7. --user=nginx --group=nginx --add-module=../fastdfs-nginx-module/src/
  8. [root@fdfs_storage1 nginx-1.7.10]# make && make install

2)为nginx修改fastdfs模块配置

  1. [root@fdfs_storage1 fdfs]# cp fastdfs-nginx-module/src/mod_fastdfs.conf
  2. /etc/fdfs
  3. [root@fdfs_storage1 fdfs]# vim /etc/fdfs/mod_fastdfs.conf
  4. tracker_server=192.168.113.50:22122
  5. group_name=group1
  6. url_have_group_name = true
  7. store_path0=/data/fastdfs/

3)修改ngin配置文件并启动

  1. [root@fdfs_storage1 fdfs]# vim /usr/local/nginx/conf/nginx.conf
  2. # location / {
  3. #    root   html;
  4. #    index  index.html index.htm;
  5. # }                                      将这段说明注释掉,增加以下说明
  6. location / {
  7. ngx_fastdfs_module;
  8. }
  9. [root@fdfs_storage1 fdfs]# /usr/local/nginx/sbin/nginx

4)测试web访问

  1. [root@fdfs_tracker1 ~]# /usr/local/bin/fdfs_upload_file
  2. /etc/fdfs/client.conf tedu.jpg
  3. group1/M00/00/00/wKhxPFc4f2yAVLH7AACwEV-ILDc194.jpg
  4. [root@fdfs_tracker1 ~]# firefox
  5. http://192.168.113.60/group1/M00/00/00/wKhxPFc4f2yAVLH7AACwEV-ILDc194.jpg &

结果如图1所示

 

原文地址:https://www.cnblogs.com/qingbai/p/11951332.html