FastDFS学习笔记
文章目录
- FastDFS学习笔记
-
- 一、FastDFS引言
- 二、通过javaAPI操作FastDFS
- 三、FastDfS集成Nginx
- 四、FastDFS集成FastDHT(文件去重)
- 五、FastDFS分布式文件系统集群搭建(虚拟机)
- 六、FastDFS分布式文件系统集群环境集成FastDHT(虚拟机)
- 七、FastDFS分布式文件系统集群环境集成Nginx(虚拟机)
- 八. 在tracker server中搭建Nginx网关服务器
一、FastDFS引言
本地存储与分布式文件系统
本地存储的缺点:
-
是否有备份? 没有
-
成本角度? 贵
服务器 :用于计算 ---- cpu/内存
用于存储 ---- 硬盘大
-
存储瓶颈? 容量有限
-
是否使用于应用服务器的集群环境? 不适用
-
。。。等等
解决本地存储的缺点 : 使用分布式文件系统 — 引入存储服务的概念
支持冗余备份;可以增加节点,所以理论上没有瓶颈;支持集群环境;支持负载均衡特性,可以在一个存储节点压力大时,将请求分给其他存储节点。
数据存储的方式:
- 关系型数据库(mysql,oracle…):用于强事务环境
- 非关系型数据库(redis…):用于弱化事务环境,性能高
- 分布式文件系统(FastDFS…):用于存储二进制数据(图片/视频/音频)
FastDFS概述
FastDFS(Distributed File System (DFS))是一个开源的分布式文件系统,她对文件进行管理,功能包括:文件存储、文件同步(冗余备份)、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等
FastDFS服务端有两个角色:跟踪器(tracker)和存储节点(storage)。
跟踪器主要做调度工作,在访问上起负载均衡的作用。
存储节点存储文件,完成文件管理的所有功能:存储、同步和提供存取接口,FastDFS同时对文件的meta data(元数据)进行管理。所谓文件的meta data就是文件的相关属性,以键值对(key value pair)方式表示,如:width=1024,其中的key为width,value为1024。文件meta data是文件属性列表,可以包含多个键值对。
FastDFS架构
FastDFS架构图:
FastDFS架构图详解 :
注:图中文件上传到存储节点后返回的文件在文件系统中的path,即为文件在FastDFS中的文件标识,分为两个部分:卷名和文件名,二者缺一不可。
跟踪器和存储节点都可以由一台至多台服务器构成。跟踪器和存储节点中的服务器均可以随时增加或下线而不会影响线上服务。其中跟踪器中的所有服务器都是对等的,可以根据服务器的压力情况随时增加或减少。
为了支持大容量,存储节点(服务器)采用了分卷(或分组)的组织方式。存储系统由一个或多个卷组成,卷与卷之间的文件是相互独立的,所有卷 的文件容量累加就是整个存储系统中的文件容量。一个卷可以由一台或多台存储服务器组成,一个卷下的存储服务器中的文件都是相同的,卷中的多台存储服务器起 到了冗余备份和负载均衡的作用。
在卷中增加服务器时,同步已有的文件由系统自动完成,同步完成后,系统自动将新增服务器切换到线上提供服务。
当存储空间不足或即将耗尽时,可以动态添加卷。只需要增加一台或多台服务器,并将它们配置为一个新的卷,这样就扩大了存储系统的容量。
以上内容来自:https://www.oschina.net/p/fastdfs
FastDFS环境搭建(虚拟机)
1. 创建虚拟机克隆机
2. 配置虚拟机
-
删除保存有原始虚拟机网卡初始化信息的文件:
# CentOS 6 中执行以下命令 [root@localhost ~]# rm -rf /etc/udev/rules.d/70-persistent-net.rules
-
修改主机名:
-
CentOS 6 中修改主机名
# CentOS 6 中执行以下命令 [root@localhost ~]# vi /etc/hosts
# CentOS 6 中执行以下命令 [root@localhost ~]# vi /etc/sysconfig/network
-
CentOS 7 中修改主机名(参考:https://www.itzgeek.com/how-tos/linux/centos-how-tos/change-hostname-in-centos-7-rhel-7.html)
-
查看当前主机名
[root@localhost ~]# hostname localhost.localdomain
-
检查当前主机名
[root@localhost ~] #hostnamectl status Static hostname: localhost.localdomain Icon name: computer-vm Chassis: vm Machine ID: 565ea8b749544aca9d5563308f9e4bc2 Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-123.el7.x86_64 Architecture: x86_64 //==================翻译==================== 静态主机名:localhost.localdomain 图标名称:computer-vm 底盘:vm 机器ID:565ea8b749544aca9d5563308f9e4bc2 引导ID:5c979d9b5f754df8b75a4e3aeabf2bad 虚拟化:vmware 操作系统:CentOS Linux 7(核心) CPE操作系统名称:cpe:/ o:centos:centos:7 内核:Linux 3.10.0-123.el7.x86_64 架构:x86_64 //==========================================
-
设置主机名
[root@localhost ~]# hostnamectl set-hostname tracker.fastdfs
-
再次检查主机名
[root @ client~] #hostnamectl status Static hostname: tracker.fastdfs Icon name: computer-vm Chassis: vm Machine ID: 565ea8b749544aca9d5563308f9e4bc2 Boot ID: 5c979d9b5f754df8b75a4e3aeabf2bad Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-123.el7.x86_64 Architecture: x86_64 //==================翻译==================== 静态主机名:tracker.fastdfs 图标名称:computer-vm 底盘:vm 机器ID:565ea8b749544aca9d5563308f9e4bc2 引导ID:5c979d9b5f754df8b75a4e3aeabf2bad 虚拟化:vmware 操作系统:CentOS Linux 7(核心) CPE操作系统名称:cpe:/ o:centos:centos:7 内核:Linux 3.10.0-123.el7.x86_64 架构:x86_64 //==========================================
-
-
-
修改完毕,需要重启虚拟机
# CentOS 6 与 7 中执行以下命令 [root@localhost ~]# reboot
-
网卡信息的配置
i. 进入到网卡信息编辑文件:
# CentOS 6 中执行以下命令 [root@fastdfs ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 # CentOS 7 中执行以下命令 [root@tracker ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
ii. 在当前窗口: 按 i 键,进入编辑 通过上下左右箭头按键,控制光标
编辑完成按 esc, 然后输入 冒号 :wq! 回车 ,进行保存
设置静态IP可参考博客:https://blog.csdn.net/u010452388/article/details/86481868
iii. 重新加载网卡配置:
# CentOS 6与7 中执行以下命令 [root@fastdfs ~]# service network restart # CentOS 7 中执行以下命令 [root@tracker ~]# systemctl restart network
-
检查防火墙状态
# CentOS 6 中执行以下命令 [root@fastdfs ~]# service iptables status # CentOS 7 中执行以下命令 [root@tracker ~]# systemctl status firewalld
-
关闭防火墙
# CentOS 6 中执行以下命令 [root@fastdfs ~]# service iptables stop # CentOS 7 中执行以下命令 [root@tracker ~]# systemctl stop firewalld
-
关闭防火墙开机自启功能
# CentOS 6 中执行以下命令 [root@fastdfs ~]# chkconfig iptables off # CentOS 7 中执行以下命令 [root@tracker ~]# systemctl disable firewalld
-
查看防火墙状态
# CentOS 6 中执行以下命令 [root@fastdfs ~]# service iptables status # CentOS 7 中执行以下命令 [root@tracker ~]# systemctl status firewalld
3. 下载安装gcc
安装方式为yum安装(需网络):
[root@tracker ~]# yum install gcc-c++ perl-devel pcre-devel openssl-devel zlib-devel wget
4. 下载安装FastDFS
[root@tracker ~]# wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz
5. 下载安装FastDFS依赖
[root@tracker ~]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.36.tar.gz
6. 解压缩依赖tar包
解压缩libfastcommon-1.0.36的依赖tar包到指定文件夹 /usr
[root@tracker ~]# tar -zxvf V1.0.36.tar.gz -C /usr
7. 解压缩FastDFS的tar包
解压缩fastdfs-5.11的依赖tar包到指定文件夹 /usr
[root@tracker ~]# tar -zxvf V5.11.tar.gz -C /usr
8. 进入libfastcommon-1.0.36
[root@tracker ~]# cd /usr/libfastcommon-1.0.36/
9. 编译并安装libfastcommon
[root@tracker libfastcommon-1.0.36]# ./make.sh && ./make.sh install
10. 进入fastdfs-5.11
[root@tracker libfastcommon-1.0.36]# cd ../fastdfs-5.11/
11. 编译并安装FastDFS
[root@tracker fastdfs-5.11]# ./make.sh && ./make.sh install
12. 进入etc目录下的fdfs目录
[root@tracker fastdfs-5.11]# cd /etc/fdfs/
13. 进行FastDFS的配置
#分别将配置文件复制一份
[root@tracker fdfs]# cp tracker.conf.sample tracker.conf
[root@tracker fdfs]# cp storage.conf.sample storage.conf
[root@tracker fdfs]# cp client.conf.sample client.conf
## ==============修改tracker.conf配置文件内容=============
[root@tracker fdfs]# vi tracker.conf
# 修改追踪服务的数据与日志文件的存储路径
base_path=/data/fastdfs/tracker
# 保存退出编辑
esc-->:wq!-->Enter
# -------创建追踪服务的数据与日志文件的存储目录-------
[root@tracker fdfs]# mkdir -p /data/fastdfs/tracker
## ==============修改storage.conf配置文件内容=============
[root@tracker fdfs]# vi storage.conf
# 修改存储节点的数据与日志文件的存储路径
base_path=/data/fastdfs/storage
# 修改上传文件在存储节点的存储路径
store_path0=/data/fastdfs/storage/store
# 修改为tracker服务器的ip(此处为本机ip)
tracker_server=192.168.:q43.136:22122
# 保存退出编辑
esc-->:wq!-->Enter
# -------创建存储节点的数据与日志文件及上传文件的存储目录-------
[root@tracker fdfs]# mkdir -p /data/fastdfs/storage/store
## ==============修改client.conf配置文件内容==============
[root@tracker fdfs]# vi client.conf
# 修改
base_path=/tmp
# 修改为tracker服务器的ip(此处为本机ip)
tracker_server=192.168.43.136:22122
# 保存退出编辑
esc键-->输入:wq!-->Enter键
14. 启动服务
-
启动 tracker server
[root@tracker fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf start
-
启动 storage server
[root@tracker fdfs]# fdfs_storaged /etc/fdfs/storage.conf start
15. 查看启动的服务
[root@tracker fdfs]# ps -ef | grep fdfs
++++++++++++++++++++++++++++ 环境搭建完成 ++++++++++++++++++++++++++++
FastDFS部分指令简介
输入fdfs_ —> 连按两下tab键 —> 显示所有FastDFS的fdfs_xxx指令
1. 监控指令 — fdfs_monitor
-
指令参数
fdfs_monitor <config_file> [-h <tracker_server>] [list|delete|set_trunk_server <group_name> [storage_id]]
注: 参数含义:
- <config_file> :配置文件路径
- [-h <tracker_server>] :(可选参数)
- [list|delete|set_trunk_server [storage_id]] :(可选参数)
-
指令使用
[root@tracker fdfs]# fdfs_monitor /etc/fdfs/client.conf
2. 上传指令 — fdfs_upload_file
-
指令参数
fdfs_upload_file <config_file> <local_filename> [storage_ip:port] [store_path_index]
注: 参数含义:
- <config_file> :配置文件路径
- <local_filename> :本地文件路径
- [storage_ip:port] :(可选参数)
- [store_path_index] :(可选参数)
-
指令使用
[root@tracker fdfs]# fdfs_upload_file /etc/fdfs/client.conf 上传的文件路径
注: 上传文件后会返回文件在FastDFS中的唯一文件标识,即卷名+文件名
3. 下载指令 — fdfs_download_file
-
指令参数
fdfs_download_file <config_file> <file_id> [local_filename] [<download_offset> <download_bytes>]
注: 参数含义:
1. <config_file> :配置文件路径 2. <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名 3. [local_filename] :文件下载地址 4. <download_offset> :(可选参数)文件下载开始时间 5. <download_bytes> :(可选参数)文件下载的字节数
-
指令使用
[root@tracker fdfs]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKhygVwnUUOAcgYHCCZp2Ahsb3g964.rpm /root/java/xxx.rpm
4. 查看文件信息指令 — fdfs_file_info
-
指令参数
fdfs_file_info <config_file> <file_id>
注: 参数含义:
- <config_file> :配置文件路径
- <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名
-
指令使用
[root@tracker fdfs]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKhygVwnUUOAcgYHCCZp2Ahsb3g964.rpm
5. 删除指令 — fdfs_delete_file
-
指令参数
fdfs_delete_file <config_file> <file_id>
注: 参数含义:
- <config_file> :配置文件路径
- <file_id> :文件在FastDFS中的唯一文件标识,即卷名+文件名
-
指令使用
[root@tracker fdfs]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKhygVwnUUOAcgYHCCZp2Ahsb3g964.rpm
注: 删除指令使用后,文件在该卷中的所有备份都会被删除,因为卷内的存储节点会相互同步,故慎用。
6. 文件追加上传及追加内容指令 — fdfs_upload_appender & fdfs_append_file
注意: 文件追加命令 fdfs_append_file 的使用前提是 要追加内容的文件是通过 文件追加上传命令 fdfs_upload_appender 上传的
-
指令参数
fdfs_upload_appender <config_file> <local_filename>
注: 参数含义:
- <config_file> :配置文件路径
- <local_filename> :将要上传的可追加内容的文件的本地路径
fdfs_append_file <config_file> <appender_file_id> <local_filename>
注: 参数含义:
- <config_file> :配置文件路径
- <appender_file_id> :需要追加内容的文件在FastDFS中的唯一文件标识,即卷名+文件名
- <local_filename> :将要追加到其他文件的文件的本地路径
-
指令使用
# ====== 上传需要追加内容的文件 ====== [root@tracker fdfs]# fdfs_upload_appender /etc/fdfs/client.conf /root/java/a.txt # ====== 追加内容 ====== [root@tracker fdfs]# fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/wKhygVwnhXWEcq9VAAAAAJXO_uM135.txt /root/java/b.txt
注: 上传文件后会返回文件在FastDFS中的唯一文件标识,即**卷名+文件名 **
二、通过javaAPI操作FastDFS
普通java环境下的javaAPI操作
FastDFS由ali的架构师yuqing开发的开源项目,并发布在GitHub上,他同时提供了操作FastDFS的 java client SDK—fastdfs-client-java 以及其他附加开源项目(https://github.com/happyfish100)。
1. 获取 fastdfs-client-java的源码
-
复制GitHub上源码的URL
-
在Git的本地版本库打开git的
-
下载源码到Git的本地版本库
$ git clone https://github.com/happyfish100/fastdfs-client-java.git
2. 安装到maven的本地仓库(源码安装)
- 进入fastdfs-client-java目录
$ cd fastdfs-client-java/
- 将fastdfs-client-java安装到maven的本地仓库
mvn clean install
3. 开发实战
1. 创建maven项目
2. 导入fastdfs-client-java的jar包
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
3. 创建配置文件.conf
配置文件名为:fdfs_client.conf(或使用其它文件名xxx_yyy.conf)
内容为:
# 设置追踪服务器ip
tracker_server = 192.168.114.129:22122
4. 创建测试类
package com.dome.test;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.junit.Before;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
/**
* 通过java API操作分布式文件系统FastDFS
*/
public class FastDFSTest {
/**
* 单元测试之前执行
*/
//存储节点客户端对象,通过此对象完成对分布式文件系统的种种操作
private StorageClient storageClient = null;
@Before
public void before(){
try {
//加载配置文件
ClientGlobal.init("fdfs_client.conf");
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
//创建一个追踪服务器的客户端对象
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = null;
try {
//获取追踪服务器连接
trackerServer = tracker.getConnection();
} catch (IOException e) {
e.printStackTrace(<