# FastDFS基础


<!--more-->

## 1. 为什么要有分布式文件系统

- 单机

  - 优点: 文件访问便利,项目直接引用,实现简单
  - 缺点: 文件和代码耦合,文件越多越混乱. 流量大时,静态访问占用资源

- 独立文件服务器

  - 优点: Web/App服务器可以更专注发挥动态处理的能力。独立存储，更方便做扩容、容灾和数据迁移；方便做图片等资源请求的负载均衡，方便应用各种缓存策略（HTTP Header、Proxy Cache等），也更加方便迁移到CDN
  - 缺点: 单机存在性能瓶颈,容灾,垂直扩展性差

- 分布式文件系统

  - 优点

    扩展能力: 毫无疑问，扩展能力是一个分布式文件系统最重要的特点；

    高可用性: 在分布式文件系统中，高可用性包含两层，一是整个文件系统的可用性，二是数据的完整和一致性；

    弹性存储:可以根据业务需要灵活地增加或缩减数据存储以及增删存储池中的资源，而不需要中断系统运行

  - 缺点: 系统复杂度高,需要更多服务器

## 2. 分布式存储解决方案对比

| 指标     | 适合类型   | 文件分布                   | 复杂度 | FUSE     | POSIX  | 备份机制               | 通讯协议接口 | 社区支持   | 开发语言 |
| -------- | ---------- | -------------------------- | ------ | -------- | ------ | ---------------------- | ------------ | ---------- | -------- |
| FastDFS  | 4kb-500MB  | 小文件合并存储不分片处理   | 简单   | 不支持   | 不支持 | 组内冗余备份           | http         | 国内用户群 | c        |
| TFS      | 所有文件   | 小文件合并,以block组织分片 | 复杂   | 不支持   | 不支持 | block存储多份,主辅灾备 | http         | 少         | c++      |
| MFS      | 大于64kb   | 分片存储                   | 复杂   | 支持     | 支持   | 多点备份,动态冗余      | 使用fuse     | 较多       | Perl     |
| HDFS     | 大文件     | 大文件分片分块存储         | 简单   | 支持     | 支持   | 多副本                 | 原生api      | 较多       | Java     |
| Ceph     | 对象文件块 | osd一主多从                | 复杂   | 支持     | 支持   | 多副本                 | 原生api      | 较少       | c++      |
| MogileFS | 海量小图片 | 不分片存储                 | 复杂   | 可以支持 | 不支持 | 动态冗余               | http原生api  | 文档少     | perl     |

## 3. FastDFS简介

FastDFS是用C语言编写的一款开源的轻量级分布式文件系统。

它对文件进行管理，功能包括：文件存储、文件同步、文件访问（文件上传、文件下载）等，解决了大容量存储和负载均衡的问题。

特别适合以文件为载体的在线服务，如相册网站、视频网站等等。

FastDFS为互联网量身定制，充分考虑了冗余备份、负载均衡、线性扩容等机制，并注重高可用、高性能等指标，使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

## 4. FastDFS特性

- 分组存储，灵活简洁、对等结构，不存在单点
- 文件不分块存储，上传的文件和OS文件系统中的文件一一对应
- 文件ID由FastDFS生成，作为文件访问凭证，FastDFS不需要传统的name server 
- 和流行的webserver无缝衔接，FastDFS已提供apache和nginx扩展模块
- 中、小文件均可以很好支持，支持海量小文件存储
- 支持多块磁盘，支持单盘数据恢复
- 支持相同内容的文件只保存一份，节约磁盘空间
- 支持在线扩容支持主从文件
- 存储服务器上可以保存文件属性（meta-data）V2.0网络通信采用libevent，支持大并发访问，整体性能更好
- 下载文件支持多线程方式，支持断点续传

## 5. FastDFS的构成

![img](https://img.zhaojq.top/20260727184918470.png "img")

- 客户端

  客户端（client），作为业务请求的发起方，通过专有接口，使用TCP/IP协议与跟踪器服务器或存储节点进行数据交互。

- 跟踪器 tracker

  Tracker server作用是负载均衡和调度，通过Tracker server在文件上传时可以根据一些策略找到Storage server提供文件上传服务。可以将tracker称为追踪服务器或调度服务器。

  跟踪器在访问上起负载均衡的作用。可以随时增加或下线而不会影响线上服务。

- 存储节点

  Storage server作用是文件存储，客户端上传的文件最终存储在Storage服务器上，Storageserver没有实现自己的文件系统而是利用操作系统的文件系统来管理文件。可以将storage称为存储服务器。

  存储节点中的服务器均可以随时增加或下线而不会影响线上服务。

## 6. Linux下FastDFS安装

### 6.1 安装

- 安装编译环境

  ```shell
  # centos
  yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
  
  # Debian
  apt-get -y install git gcc g++ make automake autoconf libtool pcre2-utils libpcre2-dev zlib1g zlib1g-dev openssl libssh-dev wget vim
  ```

- 先定义两个目录

  | 说明              | 目录           |
  | ----------------- | -------------- |
  | 所有安装包        | /usr/local/src |
  | 数据/日志存储位置 | /home/fastdfs  |

  ```shell
  mkdir /home/fastdfs -p
  #先创建目录,避免后序操作忽略
  ```

- 安装libfastcommon

  ```shell
  git clone https://github.com/happyfish100/libfastcommon.git --depth 1
  cd libfastcommon/
  ./make.sh && ./make.sh install #编译安装
  ```

- 安装fastDFS

  ```shell
  cd ../ #返回上一级目录
  git clone https://github.com/happyfish100/fastdfs.git --depth 1
  或者速度慢的话
  git clone https://github.91chi.fun//https://github.com/happyfish100/fastdfs.git --depth 1
  cd fastdfs/
  ./make.sh && ./make.sh install #编译安装
  #配置文件准备
  cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
  cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
  cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客户端文件，测试用
  cp /usr/local/src/fastdfs/conf/http.conf /etc/fdfs/ #供nginx访问使用
  cp /usr/local/src/fastdfs/conf/mime.types /etc/fdfs/ #供nginx访问使用
  ```

- 安装fastdfs-nginx-module

  ```shell
  cd ../ #返回上一级目录
  git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
  或者速度慢的话
  git clone https://github.91chi.fun//https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
  cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
  ```

- 安装nginx

  ```shell
  wget http://nginx.org/download/nginx-1.15.4.tar.gz #下载nginx压缩包
  tar -zxvf nginx-1.15.4.tar.gz #解压
  cd nginx-1.15.4/
  #添加fastdfs-nginx-module模块
  ./configure --add-module=/usr/local/src/fastdfs-nginx-module/src/ 
  make && make install #编译安装
  ```

### 6.2 单机部署测试

- tracker配置

  ```properties
  vim /etc/fdfs/tracker.conf
  #需要修改的内容如下
  port=22122  # tracker服务器端口（默认22122,一般不修改）
  base_path=/home/fastdfs  # 存储日志和数据的根目录
  ```

- storage配置

  ```properties
  vim /etc/fdfs/storage.conf
  #需要修改的内容如下
  port=23000  # storage服务端口（默认23000,一般不修改）
  base_path=/home/fastdfs  # 数据和日志文件存储根目录
  store_path0=/home/fastdfs  # 第一个存储目录
  tracker_server=127.0.0.1:22122  # tracker服务器IP和端口
  http.server_port=8888  # http访问文件的端口(默认8888,看情况修改,和nginx中保持一致)
  ```

- client配置

  ```properties
  vim /etc/fdfs/client.conf
  #需要修改的内容如下
  base_path=/home/fastdfs
  tracker_server=127.0.0.1:22122    #tracker服务器IP和端口
  ```

- 关闭防火墙或者开放端口

  ```shell
  #不关闭防火墙的话无法使用
  systemctl stop firewalld.service #关闭
  systemctl restart firewalld.service #重启
  ```

- 启动tracker

  ```shell
  /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start #启动tracker服务
  /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart #重启动tracker服务
  /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop #停止tracker服务
  chkconfig fdfs_trackerd on #自启动tracker服务
  ```

- 启动storage

  ```shell
  /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start #启动storage服务
  /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart #重动storage服务
  /usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop #停止动storage服务
  chkconfig fdfs_storaged on #自启动storage服务
  ```

- 通过端口查看是否启动成功

  ```shell
  查看所有运行的端口 
  netstat -ntlp
  ```

- 测试

  ```shell
  #保存后测试,返回ID表示成功 如：group1/M00/00/00/xx.tar.gz
  /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /home/1.txt
  
  # 成功后返回fileId:  group1/M00/00/00/cnOr3GJqiQCAdkKmAAAAFIf91zs954.txt
  ```

### 6.3 配置nginx访问

```shell
vim /etc/fdfs/mod_fastdfs.conf
#需要修改的内容如下
tracker_server=192.168.52.2:22122  # 服务器1
#tracker_server=192.168.52.3:22122  # 服务器2
#tracker_server=192.168.52.4:22122  # 服务器3
url_have_group_name=true
store_path0=/home/dfs
```

```nginx
vim /usr/local/nginx/conf/nginx.conf

#添加如下配置
server {
    listen       8888;    ## 该端口为storage.conf中的http.server_port相同
    server_name  localhost;
    location ~/group[0-9]/ {
        ngx_fastdfs_module;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    root   html;
    }
}
```

启动nginx

```shell
/usr/local/nginx/sbin/nginx #启动nginx
/usr/local/nginx/sbin/nginx -s reload #重启nginx
/usr/local/nginx/sbin/nginx -s stop #停止nginx
```

测试下载

```http
http://192.168.211.136:8888/group1/M00/00/00/xxx.png
```



## 7. java访问fastDFS

可以参考官方案例 https://github.com/happyfifish100/fastdfs-client-java

示例代码: [fastdfs-java-demo]( https://gitee.com/ixinglan/fastdfs-demo.git)

