1. 概述
FastDFS 是目前比較流行的分布式文件系統(tǒng),可以很容易的實現(xiàn)橫向擴展、動態(tài)擴容、災(zāi)備、高可用和負載均衡。
FastDFS 的服務(wù)分為 tracker 服務(wù) 和 storage 服務(wù), tracker 服務(wù)負責(zé)管理,storage 服務(wù)負責(zé)存儲。
FastDFS 的存儲節(jié)點有分卷(分組)的概念,不同分卷的服務(wù)器數(shù)據(jù)不同,相當(dāng)于分片,同一分卷中的服務(wù)器數(shù)據(jù)相同,相當(dāng)于副本,從而實現(xiàn)負載均衡。
今天我們就來聊一聊FastDFS在CentOS7上是如何安裝,以及如何與Springboot整合的。
2. 場景說明
服務(wù)器A IP:192.168.1.22
服務(wù)器B IP:192.168.1.8
在服務(wù)器A安裝 FastDFS 的 tracker (管理)
在服務(wù)器B安裝 FastDFS 的 storage(存儲)
3. FastDFS在CentOS7上的安裝
3.1 下載 FastDFS 安裝包
下載地址:https://github.com/happyfish100/fastdfs/releases

截圖來自于GitHub
3.2 下載 libfastcommon 安裝包
下載地址:https://github.com/happyfish100/libfastcommon/releases

截圖來自于GitHub
3.3 下載 fastdfs-nginx-module 安裝包
下載地址:https://github.com/happyfish100/fastdfs-nginx-module/releases

截圖來自于GitHub
3.4 下載最新版本的Nginx安裝包
下載地址:https://nginx.org/en/download.html

截圖來自于官網(wǎng)
3.5 將下載的安裝包統(tǒng)一拷貝到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)執(zhí)行編譯
# cd libfastcommon-1.0.53
# ./make.sh
3)執(zhí)行安裝
# ./make.sh install
3.8 安裝 FastDFS
1)解壓縮
# cd /home
# tar -zxvf fastdfs-6.07.tar.gz
2)執(zhí)行編譯
# cd fastdfs-6.07
# ./make.sh
3)執(zhí)行安裝
# ./make.sh install
3.9 拷貝配置文件模板
# cd /home/fastdfs-6.07/conf
# cp * /etc/fdfs/
3.10 3.5 到 3.9 的操作,服務(wù)器A、服務(wù)器B都要執(zhí)行
3.11 在服務(wù)器A配置 tracker 服務(wù)
1)配置 tracker 服務(wù)
# cd /etc/fdfs/
# vi tracker.conf

2)創(chuàng)建工作目錄
# mkdir -p /usr/local/fastdfs/tracker
3)啟動 tracker 服務(wù)
# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
3.12 在服務(wù)器B配置 storage 服務(wù)
1)配置 storage 服務(wù)
# cd /etc/fdfs/
# vi storage.conf



2)創(chuàng)建工作目錄
# mkdir -p /usr/local/fastdfs/storage
3)啟動 storage 服務(wù)
# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
3.13 命令行測試文件上傳
1)修改 client 配置文件
# cd /etc/fdfs
# vi client.conf

2)創(chuàng)建目錄
# mkdir -p /usr/local/fastdfs/client
3)測試文件上傳
# /usr/bin/fdfs_test /etc/fdfs/client.conf upload 1.png
結(jié)果主要內(nèi)容:
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 安裝在同一個節(jié)點上,才能通過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

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



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 # 連接池最大數(shù)量
max-total-per-key: 50 # 單個tracker最大連接數(shù)
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));
}
結(jié)果:
{
"group": "zhuifengren",
"path": "M00/00/00/wKgBCGFO7cqAPEqpAAIhUeJJR1c839.jpg",
"fullPath": "zhuifengren/M00/00/00/wKgBCGFO7cqAPEqpAAIhUeJJR1c839.jpg"
}
5. 綜述
今天聊了一下 分布式文件系統(tǒng)FastDFS在CentOS7上的安裝及與Springboot的整合,希望可以對大家的工作有所幫助。
歡迎幫忙點贊、評論、轉(zhuǎn)發(fā)、加關(guān)注 :)
關(guān)注追風(fēng)人聊Java,每天更新Java干貨。