分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

时间:2023-03-09 19:31:35
分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

1. 概述

FastDFS 是目前比较流行的分布式文件系统,可以很容易的实现横向扩展、动态扩容、灾备、高可用和负载均衡。

FastDFS 的服务分为 tracker 服务 和 storage 服务, tracker 服务负责管理,storage 服务负责存储。

FastDFS 的存储节点有分卷(分组)的概念,不同分卷的服务器数据不同,相当于分片,同一分卷中的服务器数据相同,相当于副本,从而实现负载均衡。

今天我们就来聊一聊FastDFS在CentOS7上是如何安装,以及如何与Springboot整合的。

2. 场景说明

服务器A IP:192.168.1.22

服务器B IP:192.168.1.8

在服务器A安装 FastDFS 的 tracker (管理)

在服务器B安装 FastDFS 的 storage(存储)

3. FastDFS在CentOS7上的安装

3.1 下载 FastDFS 安装包

下载地址:https://github.com/happyfish100/fastdfs/releases

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

截图来自于GitHub 

3.2 下载 libfastcommon 安装包

下载地址:https://github.com/happyfish100/libfastcommon/releases

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

截图来自于GitHub

3.3 下载 fastdfs-nginx-module 安装包

下载地址:https://github.com/happyfish100/fastdfs-nginx-module/releases

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

截图来自于GitHub

3.4 下载最新版本的Nginx安装包

下载地址:https://nginx.org/en/download.html

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

截图来自于官网

3.5 将下载的安装包统一拷贝到CentOS7的 /home 目录下

3.6 安装依赖包

# yum install -y gcc gcc-c++

# yum install -y libevent

# yum install -y perl

3.7 安装 libfastcommon

1)解压缩

# cd /home

# tar -zvxf libfastcommon-1.0.53.tar.gz

2)执行编译

# cd libfastcommon-1.0.53

# ./make.sh

3)执行安装

# ./make.sh install

3.8 安装 FastDFS

1)解压缩

# cd /home

# tar -zxvf fastdfs-6.07.tar.gz

2)执行编译

# cd fastdfs-6.07

# ./make.sh

3)执行安装

# ./make.sh install

3.9 拷贝配置文件模板

# cd /home/fastdfs-6.07/conf

# cp * /etc/fdfs/

3.10  3.5 到 3.9 的操作,服务器A、服务器B都要执行

3.11 在服务器A配置 tracker 服务

1)配置 tracker 服务

# cd /etc/fdfs/

# vi tracker.conf

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

2)创建工作目录

# mkdir -p /usr/local/fastdfs/tracker

3)启动 tracker 服务

# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

3.12 在服务器B配置 storage 服务

1)配置 storage 服务

# cd /etc/fdfs/

# vi storage.conf

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

2)创建工作目录

# mkdir -p /usr/local/fastdfs/storage

3)启动 storage 服务

# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf

3.13 命令行测试文件上传

1)修改 client 配置文件

# cd /etc/fdfs

# vi client.conf

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

2)创建目录

# mkdir -p /usr/local/fastdfs/client

3)测试文件上传

# /usr/bin/fdfs_test /etc/fdfs/client.conf upload 1.png

结果主要内容:

group_name=zhuifengren, remote_filename=M00/00/00/wKgBCGFOzh6AB7GRAABdyNYCZvQ876.png
source ip address: 192.168.1.8
file timestamp=2021-09-25 15:22:06
file size=24008
file crc32=3590481652
example file url: http://192.168.1.8/zhuifengren/M00/00/00/wKgBCGFOzh6AB7GRAABdyNYCZvQ876.png

4)在 storage 工作目录查看文件

# cd /usr/local/fastdfs/storage/data/00/00

# ll

-rw-r--r--. 1 root root 24008 Sep 25 15:22 wKgBCGFOzh6AB7GRAABdyNYCZvQ876_big.png
-rw-r--r--. 1 root root 49 Sep 25 15:22 wKgBCGFOzh6AB7GRAABdyNYCZvQ876_big.png-m
-rw-r--r--. 1 root root 24008 Sep 25 15:22 wKgBCGFOzh6AB7GRAABdyNYCZvQ876.png
-rw-r--r--. 1 root root 49 Sep 25 15:22 wKgBCGFOzh6AB7GRAABdyNYCZvQ876.png-m

3.14 安装并配置Nginx

Nginx 必须和 storage 安装在同一个节点上,才能通过Http的方式访问图片。

1)解压 fastdfs-nginx-module

# cd /home

# tar -zxvf fastdfs-nginx-module-1.22.tar.gz

2)修改 config 配置文件

# cd /home/fastdfs-nginx-module-1.22/src

# vi config

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

3)修改 mod_fastdfs.conf 配置文件

# cd /home/fastdfs-nginx-module-1.22/src

# cp mod_fastdfs.conf /etc/fdfs/

# mkdir -p /usr/local/fastdfs/tmp

# cd /etc/fdfs/

# vi mod_fastdfs.conf

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合

4)安装Nginx 的依赖包

# yum install -y gcc-c++
# yum install -y pcre pcre-devel
# yum install -y zlib zlib-devel
# yum install -y openssl openssl-devel

5)解压Nginx

# cd /home

# tar -zxvf nginx-1.20.1.tar.gz

6)安装Nginx

# cd nginx-1.20.1

# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/local/nginx/nginx.pid \
--lock-path=/var/local/nginx/nginx.lock \
--error-log-path=/var/local/nginx/error.log \
--http-log-path=/var/local/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/local/nginx/client \
--http-proxy-temp-path=/var/local/nginx/proxy \
--http-fastcgi-temp-path=/var/local/nginx/fastcgi \
--http-uwsgi-temp-path=/var/local/nginx/uwsgi \
--http-scgi-temp-path=/var/local/nginx/scgi \
--add-module=/home/fastdfs-nginx-module-1.22/src

# make

# make install

7)修改Nginx配置文件

# cd /usr/local/nginx/conf

# vi nginx.conf

server {
listen 8888; # 该端口为storage.conf中的http.server_port相同
server_name localhost; location /zhuifengren/M00 {
ngx_fastdfs_module;
} }

8)启动 Nginx

# cd /usr/local/nginx/sbin

# ./nginx

9)打开之前上传的图片

http://192.168.1.8:8888/zhuifengren/M00/00/00/wKgBCGFOzh6AB7GRAABdyNYCZvQ876.png

可以正常打开 :)

4. 与 Springboot 整合

4.1 引入依赖

        <dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>

4.2 配置Springboot

打开 application.yml

fdfs:
connect-timeout: 300 # 连接超时时间
so-timeout: 300 # 读取超时时间
tracker-list: 192.168.1.22:22122
pool:
max-total: 100 # 连接池最大数量
max-total-per-key: 50 # 单个tracker最大连接数
max-wait-millis: 5000 # 连接耗尽最大等待时间 毫秒

4.3 文件上传

    @Autowired
private FastFileStorageClient fastFileStorageClient; @Test
public void upload() throws FileNotFoundException { File file = new File("C:\\tmp\\2.jpg");
FileInputStream fileInputStream = new FileInputStream(file); StorePath storePath = fastFileStorageClient.uploadFile
(fileInputStream, file.length(), "jpg", null); System.out.println(JsonUtils.objectToJson(storePath));
}

结果:

{
"group": "zhuifengren",
"path": "M00/00/00/wKgBCGFO7cqAPEqpAAIhUeJJR1c839.jpg",
"fullPath": "zhuifengren/M00/00/00/wKgBCGFO7cqAPEqpAAIhUeJJR1c839.jpg"
}

5. 综述

今天聊了一下 分布式文件系统FastDFS在CentOS7上的安装及与Springboot的整合,希望可以对大家的工作有所帮助。

欢迎帮忙点赞、评论、转发、加关注 :)

关注追风人聊Java,每天更新Java干货。