1. 简介
FastDFS 是用 c语言
编写的一款开源分布式文件系统。充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用高性能等指标,使用 FastDFS 很容易搭建一套高性能的文件服务器集群
,并提供文件上传下载等服务。
FastDFS 支持 Linux、FreeBSD 等 UNIX 系统类 google FS,不是通用的文件系统,只能通过专有API访问,目前提供了 C
、Java
和PHP
语言的API,FastDFS 可以看做是基于文件的 key-value(键值对)
存储系统,称作分布式文件存储服务更为合适。
2. 专用术语
TrackerServer:跟踪服务器。记录 Storage server 的状态
并做调度
工作,就像公交站里面的调务员一样,它负责通过负载均衡
选出最优的存储节点,是连接客户端和存储服务器的枢纽。
Storage server:存储服务器(存储节点)。用于存储文件和文件相关属性。
group:组(卷)。同组内服务器上的文件是完全相同的。
文件标识:由组名和文件名(包含路径)构成。
meta data:文件相关属性。以 key-value 方式存储,如:name=filename,size=1024
3. fastDFS 架构
fastDFS 架构图如下:
解读:
Client 就是我们的 java 项目,用来写 java 代码,调用 FastDFS API 控制文件上传下载层,开发人员只需要关心这一部分如何写即可。
例如 Client 发送一个写操作的需求,Tracker 跟踪器会去 Storage 存储器中调度一个最优的存储器(存储空间足够或此时无写操作的存储器),并返回可用的 Storage 存储器的 ip 和 端口号给 Client。这种行为类似于我们要在电脑保存一个文件,我们需要先知道哪个盘的空间足够存放这个文件
。
Storage 集群由多个卷(组) 组成,卷与卷之间的资源不同,Storage 存储器的资源是所有卷上资源的组合。
Storage 服务器如需扩容则增加卷即可。
一个卷内又可以分成多个服务器,卷内的服务器资源都是相同的。类似于主备服务器。当文件上传至某个卷时,卷内的服务器都会去同步一份此文件,保证卷内的每个服务器资源一致,以达到备份效果。
可以理解为 Storage 是我们的磁盘,卷(组) 是磁盘中的分区,如C盘、D盘,盘与盘之间的文件各不相同,但是为了备份,C盘内可以创建多个文件夹,每个文件夹中的文件相同。其中一个文件夹坏了还有其他的备份文件夹。
4. fastDFS 安装
4.1 下载安装包
安装包下载地址:https://github.com/happyfish100/
注意
:别全下最新版,会出现版本不对应导致安装缺少东西的问题。建议下载稳定版本
:
fastdfs 6.06
libfastcommon 1.0.43
fastdfs-nginx-module 1.22
nginx 1.16.1
fastdfs下载地址:https://github.com/happyfish100/fastdfs/tree/V6.06
libfastcommon下载地址:https://github.com/happyfish100/libfastcommon/tree/V1.0.43
fastdfs-nginx-module下载地址:https://github.com/happyfish100/fastdfs-nginx-module/tree/V1.22
nginx下载地址:http://nginx.org/en/download.html
准备好的安装包如下:
4.2 环境准备工作
由于FastDFS 是用 c语言 编写的,所以我们需要准备相关的编译依赖。
- 安装C语言
[root@localhost ~]# yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
- 安装 zip 解压命令
[root@localhost ~]# yum -y install unzip
- 创建 fastdfs 安装包存放目录
[root@localhost ~]# mkdir /usr/local/src/fastdfs
- 创建 fastdfs 的 TrackerServer 信息存储目录
[root@localhost ~]# mkdir /home/dfs/tracker
- 创建 fastdfs 的 StorageServer 信息存储目录
[root@localhost ~]# mkdir /home/dfs/storage
- 创建 fastdfs 的 StorageServer 文件数据存储目录
[root@localhost ~]# mkdir /home/dfs/storage/store
4.3 核心类库安装
libfastcommon
为FastDFS操作的基础库,FastDFS没有使用标准的库,而是封装了一 套和FastDFS相关的库。
- 进入压缩包目录
[root@localhost ~]# cd /usr/local/src/fastdfs
- 解压安装包到当前目录下
[root@localhost fastdfs]# unzip libfastcommon-1.0.43.zip -d /usr/local/src/fastdfs/
- 进入解压好的 libfastcommon-1.0.43 目录,查看解压目录,有个 make.sh 文件
[root@localhost fastdfs]# cd libfastcommon-1.0.43
- 进入 libfastcommon-1.0.43 目录进行编译:
[root@localhost libfastcommon-1.0.43]# ./make.sh
- 编译好后进行安装:
[root@localhost libfastcommon-1.0.43]# ./make.sh install
至此核心类库安装完成。
4.4 fastDFS 安装
- 进入压缩包目录
[root@localhost ~]# cd /usr/local/src/fastdfs
- 解压安装包到 fastdfs 目录下:
[root@localhost fastdfs]# unzip fastdfs-6.06.zip -d /usr/local/src/fastdfs/
- 进入解压好的 fastdfs-6.06 目录,查看解压目录,有个 make.sh 文件
[root@localhost fastdfs]# cd fastdfs-6.06
- 进行编译:
[root@localhost fastdfs-6.06]# ./make.sh
- 编译好后进行安装:
[root@localhost fastdfs-6.06]# ./make.sh install
安装后 fastDFS 主程序所在的位置如下:
- 可执行文件:默认 /usr/bin
- 配置文件:默认 /etc/fdfs
- 主程序代码:默认 /usr/bin
- 插件组:默认/usr/include/fastdfs
4.4.1 资源目录位置
安装完成后,可进入对应的资源位置查看,是否安装成功。
-
服务脚本位置:
/etc/init.d 目录下,有脚本文件
fdfs-storaged
和fdfs-trackerd
-
配置文件位置:
/etc/fdfs 目录下,有四个配置文件模板,分别是
client.conf.sample
:客户端配置storage.conf.sample
:存储节点配置storage_ids.conf.sample
:如果 storage 使用id来标识,而不是 IP,则配置此文件tracker.conf.sample
:跟踪器配置
-
内置命令位置:
/usr/bin 目录下,有 fastdfs 的内置命令。
4.5 fastdfs-nginx-module 安装
安装 fastdfs-nginx-module 可以使文件通过 http 协议直接在网页输入IP + 端口 + 卷名 + 文件名
的形式直接访问。
- 进入压缩包目录
[root@localhost ~]# cd /usr/local/src/fastdfs
- 解压到当前目录
[root@localhost fastdfs]# unzip fastdfs-nginx-module-1.22.zip -d /usr/local/src/fastdfs/
- 进入 fastdfs-nginx-module-1.22 目录
[root@localhost fastdfs]# cd fastdfs-nginx-module-1.22/src/
- 修改 config 配置
[root@localhost src]# vim config
配置 fastdfs 模块组(函数库)的位置
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/
保存退出。
4.6 nginx 安装
- 进入压缩包目录
[root@localhost ~]# cd /usr/local/src/fastdfs
- 解压到当前目录
[root@localhost fastdfs]# tar zxvf nginx-1.16.1.tar.gz -C /usr/local/src/fastdfs/
- 进入到 nginx-1.16.1 目录
[root@localhost fastdfs]# cd nginx-1.16.1/
- 添加 fastdfs-nginx-module 模块到 nginx 中
[root@localhost nginx-1.16.1]# ./configure --add-module=/usr/local/src/fastdfs/fastdfs-nginx-module-1.22/src/
- 预编译
[root@localhost nginx-1.16.1]# make
- 编译加安装
[root@localhost nginx-1.16.1]# make install
至此 nginx 安装完成。
5. fastDFS 配置
一般情况下,TrackerServer 跟踪服务器和 Storage server 存储服务器部署在不同的机子上。如果设备条件不允许,也可以一台机子同时作为跟踪器和存储器。
5.1 TrackerServer 跟踪服务器配置
- 进入配置目录
[root@localhost ~]# cd /etc/fdfs
- 复制一个 tracker.conf 作为 TrackerServer 配置文件,原先的 tracker.conf.sample 作为备份。
[root@localhost fdfs]# cp tracker.conf.sample tracker.conf
- 进入 tracker.conf 进行配置。
[root@localhost fdfs]# vim tracker.conf
base_path = /home/dfs/tracker
5.1.1 Tracker 常用配置参数说明
参数名 | 说明 | 默认值 | 备注 |
---|---|---|---|
bind_addr | 是否绑定IP | 无 | 常用于服务器有多个IP但只希望一个IP提供服务,如果不填则表示所有(一般不填) |
port | TrackerServer服务端口号 | 22122 | |
base_path | 存储StorageServer服务器卷下所有端口和节点信息 | /home/yuqing/fastdfs | 根目录必须存在,子目录会自动创建 |
store_server | 选择storage server 进行上传操作时的调度策略 | 0 | 0: 轮询方式 1: 根据ip 地址进行排序选择第一个服务器(IP地址最小者) 2: 根据优先级进行排序,上传优先级由storage server来设置 |
store_path | 选择storage server 中的哪个目录进行上传的调度策略 | 0 | 0: 轮流方式,多个目录依次存放文件 2: 选择剩余空间最大的目录存放文件 |
download_server | 选择哪个 storage server 作为下载服务器的调度策略 | 0 | 0: 轮询方式,可以下载当前文件的任一storage server 1: 哪个为源storage server 就用哪一个 |
5.2 StorageServer 存储服务器配置
- 进入配置目录
[root@localhost ~]# cd /etc/fdfs
- 复制一个 storage.conf 作为 StorageServer 配置文件,原先的 storage.conf.sample 作为备份。
[root@localhost fdfs]# cp storage.conf.sample storage.conf
- 进入 storage.conf 进行配置。
[root@localhost fdfs]# vim storage.conf
base_path = /home/dfs/storage
store_path0 = /home/dfs/storage/store
tracker_server = 192.168.X.X:22122 #你的TrackerServer服务的IP和端口
http.server_port = 8080
5.2.1 Storage 常用配置参数说明
参数名 | 说明 | 默认值 | 备注 |
---|---|---|---|
group_name | 组(卷)名称 | group1 | |
port | StorageServer服务端口号 | 23000 | |
base_path | 存放StorageServer数据及日志位置 | /home/yuqing/fastdfs | 根目录必须存在,子目录会自动创建 |
store_path0 | 真正存放上传文件的目录位置 | /home/yuqing/fastdfs | |
tracker_server | TrackerServer 追踪服务器的IP地址及端口 | 192.168.209.121:22122 192.168.209.122:22122 | TrackerServer 是集群就配多个,单机则配一个即可 |
http.server_port | http访问文件的端口 | 8888 | 看情况修改,和nginx中保持一致即可 |
5.3 fastdfs-nginx-module 配置
- 进入 fastdfs-nginx-module 的 src 目录
[root@localhost src]# cd /usr/local/src/fastdfs/fastdfs-nginx-module-1.22/src
- 复制 mod_fastdfs.conf 文件到 /etc/fdfs 目录下
[root@localhost src]# cp mod_fastdfs.conf /etc/fdfs
- 进入到 /etc/fdfs 目录下
[root@localhost src]# cd /etc/fdfs
- 修改 mod_fastdfs.conf 配置
[root@localhost fdfs]# vim mod_fastdfs.conf
tracker_server=192.168.100.10:22122 #TrackerServer服务器的IP和端口号
url_have_group_name = true #url中是否包含组名
store_path0=/home/dfs/storage/store #文件资源存储位置
保存并退出。
5.3.1 fastdfs-nginx-module 常用配置参数说明
参数名 | 说明 |
---|---|
tracker_server | TrackerServer服务器的IP和端口号 |
storage_server_port | StorageServer服务器的端口号 |
group_name | 组名 |
url_have_group_name | url中是否包含组名 |
store_path0 | 文件资源存储位置 |
5.4 nginx 配置
- 配置nginx.config
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
# user nobody 改成 user root
user root;
server {
#此端口是storage.conf中的http.server_port端口号
listen 8080;
server_name localhost;
location ~/group[0-9]/ {
ngx_fastdfs_module;
}
}
- 将 fastdfs 中的 http.conf 和 mime.types 复制进 /etc/fdfs/
[root@localhost ~]# cp /usr/local/src/fastdfs/fastdfs-6.06/conf/http.conf /etc/fdfs/
[root@localhost conf]# cp /usr/local/src/fastdfs/fastdfs-6.06/conf/mime.types /etc/fdfs/
- 修改好后重启 TrackerSrever、StorageServer 和 nginx
[root@localhost ~]# /etc/init.d/fdfs_trackerd restart
[root@localhost ~]# /etc/init.d/fdfs_storaged restart
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
进入 /usr/local/nginx/sbin 目录下,重启 nginx
[root@localhost ~]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx
6. fastDFS 命令
服务脚本位置在**/etc/init.d**目录下,进入 init.d 目录
[root@localhost ~]# cd /etc/init.d
6.1 Tracker 命令
6.1.1 Tracker 服务启动
[root@localhost ~]# /etc/init.d/fdfs_trackerd start
6.1.2 查看 Tracker 状态
/etc/init.d 目录下
[root@localhost init.d]# ./fdfs_trackerd status
6.1.3 查看服务进程
[root@localhost init.d]# ps -ef|grep fdfs
6.1.4 Tracker 服务停止
[root@localhost ~]# /etc/init.d/fdfs_trackerd stop
6.1.5 Tracker 服务重启
[root@localhost ~]# /etc/init.d/fdfs_trackerd restart
6.1.6 Tracker 开机自启动
- 进入 /etc/rc.d/rc.local 文件。
[root@localhost init.d]# vim /etc/rc.d/rc.local
- 添加指定启动文件位置,即可。
/etc/init.d/fdfs_trackerd start
6.2 Storage 命令
6.2.1 Storage 服务启动
[root@localhost ~]# /etc/init.d/fdfs_storaged start
6.2.2 查看 Storage 状态
/etc/init.d 目录下
[root@localhost init.d]# ./fdfs_storaged status
6.2.3 查看服务进程
[root@localhost init.d]# ps -ef|grep fdfs
6.2.4 Storage 服务停止
[root@localhost ~]# /etc/init.d/fdfs_storaged stop
6.2.5 Storage 服务重启
[root@localhost ~]# /etc/init.d/fdfs_storaged restart
6.2.6 Storage 开机自启动
不建议设置 Storage 开机自启,由于Storage服务的启动,前提需要先启动Tracker服务,否则会启动报错。一般手动启动即可。如能够保证Tracker服务长运行,设置Storage自启动也可以。
- 进入 /etc/rc.d/rc.local 文件。
[root@localhost init.d]# vim /etc/rc.d/rc.local
- 添加指定启动文件位置,即可。
/etc/init.d/fdfs_storaged start
7. 文件上传
7.1 文件上传流程
- StorageServer 存储服务器会定时向 TrackerServer 追踪服务器发送自身当前状态信息。
- 客户端发送一个上传文件请求给 TrackerServer。
- TrackerServer 会根据当前各个 StorageServer 状态,根据配置的调度策略调度一个最优的存储节点。
- TrackerServer 将选举的最优存储节点的 IP 和 端口号返回给客户端。
- 客户端调用 fastDFS API 上传文件到相应存储节点。
- StorageServer 给文件生成唯一文件名。
- StorageServer 将文件写进磁盘。
- 返回文件全路径(组名+虚拟磁盘路径+文件名)给客户端。
- 客户端将文件路径存储起来,比如存入数据库。
7.2 Java API 实现文件上传
使用 fastdfs 作者提供的 java API即可。
7.2.1 pom 引入 fastdfs-client 依赖
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
7.2.2 创建 fdfs_client.conf 配置文件
在 resource 文件夹下创建文件并配置 fastdfs 连接参数
#连接超时
connect_timeout=60
#网络超时
network_timeout=60
#编码格式
charset=UTF-8
#tracker端口号
http.tracker_http_port=8080
#tracker服务器IP+端口号
tracker_server=192.168.100.10:22122
7.2.3 封装文件信息类
package com.fastdfs.fastdfspro.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
/**
* 文件信息的封装类
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class FastdfsFile {
//文件名称
private String name;
//文件的内容
private byte[] content;
//文件扩展名
private String ext;
//文件的摘要
private String md5;
//文件作者
private String author;
public FastdfsFile(String name, byte[] content, String ext) {
this.name = name;
this.content = content;
this.ext = ext;
}
}
7.2.4 封装文件上传工具类
package com.fastdfs.fastdfspro.utils;
import com.fastdfs.fastdfspro.entity.FastdfsFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
/**
* 文件上传工具类
*/
public class FastdfsUtil {
//初始化Tracker的信息
static{
try {
//获取了fastdfs配置文件
String filePath=new ClassPathResource("fdfs_client.conf").getPath();
//加载tracker的配置信息
ClientGlobal.init(filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取TrackerServer对象
private static TrackerServer _getTrackerServer() throws Exception{
return new TrackerClient().getConnection();
}
//获取StorageClient客户端
private static StorageClient _getStorageClient() throws Exception{
return new StorageClient(_getTrackerServer(),null);
}
/**
* 文件上传
* @param file 要上传的文件
* @return 返回文件存入的位置(组名+虚拟磁盘路径+文件名)
*/
public static String[] upload(FastdfsFile file){
//文件信息键值对数组
NameValuePair[] meta_list=new NameValuePair[1];
//文件作者
meta_list[0]=new NameValuePair(file.getAuthor());
String[] uploadResults=null;
try {
StorageClient storageClient = _getStorageClient();
//三个参数分别为 文件内容byte[]、文件扩展名、文件信息列表
uploadResults = storageClient.upload_file(file.getContent(),file.getExt(),meta_list);
} catch (Exception e) {
e.printStackTrace();
}
return uploadResults;
}
}
7.2.5 文件上传客户端调用
package com.fastdfs.fastdfspro.controller;
import com.fastdfs.fastdfspro.entity.FastdfsFile;
import com.fastdfs.fastdfspro.utils.FastdfsUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class FastdfsController {
//文件上传
@PostMapping("/upload")
public String upload(@RequestParam(value = "file") MultipartFile file) throws Exception{
FastdfsFile fastDFSFile=new FastdfsFile(
file.getOriginalFilename(),
file.getBytes(),
StringUtils.getFilenameExtension(file.getOriginalFilename()));
String[] upload = FastdfsUtil.upload(fastDFSFile);
String url="http://192.168.100.10:8080/"+upload[0]+"/"+upload[1];
return url;
}
}
7.2.6 测试
注意:虚拟机必须关闭防火墙,不关无法使用。
[root@localhost ~]# systemctl stop firewalld.service
进入虚拟机中文件存储的路径查看,上传成功,后缀-m的文件是自动生成的备份文件。
浏览器输入 url,可以访问。