天天看點

分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

目錄

1. 概述

2. 場景說明

3. FastDFS在CentOS7上的安裝

4. 與 Springboot 整合

5. 綜述

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 安裝包

下載下傳位址:Releases · happyfish100/fastdfs · GitHub

分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

 截圖來自于GitHub 

3.2 下載下傳 libfastcommon 安裝包

下載下傳位址:Releases · happyfish100/libfastcommon · GitHub

分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

 截圖來自于GitHub

3.3 下載下傳 fastdfs-nginx-module 安裝包

下載下傳位址:Releases · happyfish100/fastdfs-nginx-module · GitHub

分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

 截圖來自于GitHub

3.4 下載下傳最新版本的Nginx安裝包

下載下傳位址:nginx: download

分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

 截圖來自于官網

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的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

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的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述
分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述
分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

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的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

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的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

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的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述
分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述
分布式檔案系統FastDFS在CentOS7上的安裝及與Springboot的整合1. 概述2. 場景說明3. FastDFS在CentOS7上的安裝4. 與 Springboot 整合5. 綜述

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幹貨。

繼續閱讀