Linux搭建FastDFS文件服務器並實現springboot上傳文件到FastDFS服務器

www.myblogs.cn/fastDFS

一,前言

(1)FastDFS是什麼

FastDFS 是用 c 語言編寫的一款開源的分佈式文件系統。FastDFS爲互聯網量身定製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,使用 FastDFS很容易搭建一套高性能的文件服務器集羣提供文件上傳、下載等服務。

(2)FastDFS結構

FastDFS 架構包括 Tracker serverStorage server。客戶端請求 Tracker server進行文件上傳、下載,通過 Tracker server 調度最終由 Storage server 完成文件上傳和下載。

Tracker server 作用是負載均衡和調度,通過 Tracker server 在文件上傳時可以根據一些策略找到 Storage server 提供文件上傳服務。可以將tracker稱爲追蹤服務器或調度服務器。

Storage server作用是文件存儲,客戶端上傳的文件最終存儲在 Storage 服務器上,Storageserver 沒有實現自己的文件系統而是利用操作系統 的文件系統來管理文件。可以將storage稱爲存儲服務器。

圖片加載失敗

二,搭建FastDFS文件服務器

1,首先需要安裝需要的依賴

yum -y install gcc-c++
yum -y install libevent

2,然後使用xftp把需要的壓縮包上傳到Linux服務器,在/usr/local文件夾中新建一個fastdfs文件夾。

在這裏插入圖片描述

3,解壓libfastcommon

tar -xvf libfastcommonV1.0.7.tar.gz

4,然後進行安裝

[root@localhost fastdfs]# ls
fastdfs-nginx-module_v1.16.tar.gz  FastDFS_v5.05.tar.gz  libfastcommon-1.0.7  libfastcommonV1.0.7.tar.gz  nginx-1.8.1.tar.gz
[root@localhost fastdfs]# cd libfastcommon-1.0.7
[root@localhost libfastcommon-1.0.7]# ls
HISTORY  INSTALL  libfastcommon.spec  make.sh  README  src
[root@localhost libfastcommon-1.0.7]# ./make.sh
[root@localhost libfastcommon-1.0.7]# ./make.sh install

libfastcommon安裝好之後會自動將庫文件拷貝至usr/lib64下,由於FastDFS程序引用/usr/lib目錄,所以需要把/usr/lib64下的庫文件拷貝至/usr/lib下。

拷貝命令

cd /usr/lib64
cp libfastcommon.so /usr/lib

查看/usr/lib目錄下是否存在該文件

[root@localhost lib64]# find /usr/lib -name libfastcommon.so
/usr/lib/libfastcommon.so

5,開始安裝FastDFS

進到/usr/local/fastdfs目錄中進行解壓

tar -xvf FastDFS_v5.05.tar.gz

安裝

[root@localhost fastdfs]# ls
FastDFS  fastdfs-nginx-module_v1.16.tar.gz  FastDFS_v5.05.tar.gz  libfastcommon-1.0.7  libfastcommonV1.0.7.tar.gz  nginx-1.8.1.tar.gz
[root@localhost fastdfs]# cd FastDFS
[root@localhost FastDFS]# ls
client  common  conf  COPYING-3_0.txt  fastdfs.spec  HISTORY  init.d  INSTALL  make.sh  php_client  README.md  restart.sh  stop.sh  storage  test  tracker
[root@localhost FastDFS]# ./make.sh
[root@localhost FastDFS]# ./make.sh install

安裝成功後將安裝目錄下的conf下的文件拷貝到/etc/fdfs下。(nginx需要)

conf文件目錄在/usr/local/fastdfs/FastDFS/conf,進到這個conf目錄

cp * /etc/fdfs/  拷貝全部文件到fdfs目錄下

6,安裝tracker服務(跟蹤服務)

[root@localhost conf]# cd /usr/local/fastdfs/FastDFS/tracker/
[root@localhost tracker]# pwd
/usr/local/fastdfs/FastDFS/tracker   這個路徑設置爲base_path路徑(下圖)
# 編輯tracker.conf配置文件
[root@localhost tracker]# vim /etc/fdfs/tracker.conf

修改base_path路徑爲

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-j1wJMvxW-1572494243007)(D:\文件筆記\image\1572441918745.png)]

保存退出,然後啓動

# 啓動
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
# 重啓的話是restart
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

7,安裝storage服務(存儲服務)

[root@localhost storage]# pwd
/usr/local/fastdfs/FastDFS/storage  這個路徑設置爲base_path路徑(下圖)
[root@localhost storage]# vim /etc/fdfs/storage.conf

修改base_path路徑

在這裏插入圖片描述

啓動

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

8,到此爲止,兩個服務都安裝完成了,現在測試是否安裝成功。

/usr/local/fastdfs/FastDFS/client裏面的libfdfsclient.so拷貝到/usr/lib下,這一步非常重要

cd /usr/local/fastdfs/FastDFS/client/
cp libfdfsclient.so /usr/lib

修改配置文件

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-tTEpChXG-1572494243009)(D:\文件筆記\image\1572443620894.png)]

新建一個在root根目錄創建一個html文件,內容爲hi,fastdfs!

測試上傳

/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/hi.html

出現下面的情況就是安裝成功了,並且文件上傳成功。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-wkFWaHlm-1572494243010)(D:\文件筆記\image\1572444079589.png)]

文件存在了哪裏呢?這裏

[root@localhost 00]# pwd
/usr/local/fastdfs/FastDFS/storage/data/00/00
[root@localhost 00]# ll
total 16
-rw-r--r--. 1 root root 12 Oct 30 09:57 wKi6gV25lreAJiwIAAAADP8d-tA34_big.html
-rw-r--r--. 1 root root 49 Oct 30 09:57 wKi6gV25lreAJiwIAAAADP8d-tA34_big.html-m
-rw-r--r--. 1 root root 12 Oct 30 09:57 wKi6gV25lreAJiwIAAAADP8d-tA34.html
-rw-r--r--. 1 root root 49 Oct 30 09:57 wKi6gV25lreAJiwIAAAADP8d-tA34.html-m
[root@localhost 00]# 

其實到這一步,這個上傳的文件的路徑已經出來了。

http://192.168.186.129/group1/M00/00/00/wKi6gV25lreAJiwIAAAADP8d-tA34_big.html

但是在瀏覽器中還不能夠直接訪問文件。

三,安裝nginx及插件實現資源的訪問

爲什麼需要nginx呢?

因爲FastDFS本身並不具有處理http協議的能力,而客戶端正是通過http協議訪問的,因此在瀏覽器中不能夠直接訪問文件就講得通了。

如何處理?

說白了,需要nginx服務器作爲中間件,因爲nginx具有處理http協議的能力,然後安裝nginx+fastDFS的插件,目的是建立nginxFastDFS服務器的聯繫以供訪問。

在這裏插入圖片描述1,安裝nginxnginx插件

yum install gcc-c++ (前面已經安裝過)
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

2,進到/usr/local/fastdfs目錄下,解壓nginx-fastDFS插件

tar -zxf fastdfs-nginx-module_v1.16.tar.gz

3,進到/usr/local/fastdfs/fastdfs-nginx-module/src下,修改config配置文件

把路徑上的local全部去掉,並保存退出。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-CPSnf4Lk-1572494243011)(D:\文件筆記\image\1572484739277.png)]

4,把/usr/local/fastdfs/fastdfs-nginx-module/src下的mod_fastdfs.conf文件複製到/etc/fdfs目錄下,並編輯該文件。

[root@192 src]# cp mod_fastdfs.conf /etc/fdfs/
[root@192 src]# vim /etc/fdfs/mod_fastdfs.conf 

在這裏插入圖片描述

5,安裝nginx

進到/usr/local/fastdfs壓縮包目錄,解壓nginx壓縮包

tar -zxf nginx-1.8.1.tar.gz

關聯nginxnginx的插件

[root@192 fastdfs]# cd nginx-1.8.1/
# 關聯操作
[root@192 nginx-1.8.1]# ./configure --add-module=/usr/local/fastdfs/fastdfs-nginx-module/src
[root@192 nginx-1.8.1]# make  編譯
[root@192 nginx-1.8.1]# make install  安裝

可以看到,nginx被安裝到了/usr/local目錄下

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-OTdTvIax-1572494243013)(D:\文件筆記\image\1572486117626.png)]

/usr/local/nginx/conf目錄下,修改nginx的配置文件nginx.conf,保存退出。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-SpO9V1NB-1572494243013)(D:\文件筆記\image\1572486540981.png)]

啓動nginx

[root@192 sbin]# pwd
/usr/local/nginx/sbin
[root@192 sbin]# ./nginx

然後現在就可以在瀏覽器訪問剛纔那個文件的路徑了。

在這裏插入圖片描述

四,springboot訪問FastDFS實現文件上傳

1,添加依賴

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

2,在啓動類上添加註解引入配置文件類

@Import(FdfsClientConfig.class)

3,在application.properties加入FastDFS的相關配置

# 超時時間
fdfs.so-timeout=1500
# 連接的超時時間
fdfs.connect-timeout=600
# 連接池
fdfs.pool.jmx-enabled=false
# 縮略圖尺寸
fdfs.thumb-image.height=100
fdfs.thumb-image.width=100
# tracker服務跟蹤器的地址
fdfs.tracker-list=192.168.186.129:22122

這裏插一句,提前先把linux的防火牆設置爲22122端口和23000端口放行,否則後續的測試會因爲連不上跟蹤器報錯。

    @Autowired
    FastFileStorageClient fastFileStorageClient;//直接引入

    @RequestMapping("/fastdfs")
    @ResponseBody
    public String fastdfs() throws FileNotFoundException {
        File file=new File("D://文件筆記//image//1571884758247.png");
        //文件名
        String fileName=file.getName();
        //後綴名
        String extName=fileName.substring(fileName.lastIndexOf(".")+1);
        //創建流
        FileInputStream fileInputStream=new FileInputStream(file);
        //四個參數(輸入流,文件大小,後綴名,null),返回一個路徑
        StorePath storePath = fastFileStorageClient.uploadFile(fileInputStream, file.length(), extName, null);
        //不同路徑
        System.out.println(storePath.getFullPath());
        System.out.println(storePath.getPath());
        System.out.println(storePath.getGroup());
        return "圖片上傳成功,並調皮的給您返回一個路徑";
    }

分別看瀏覽器和控制檯

在這裏插入圖片描述

然後在瀏覽器輸入訪問路徑(nginx的ip地址和端口加上storePath.getFullPath()的路徑),例如

http://192.168.186.129:80/group1/M00/00/00/wKi6gV26SV6ALJu6AAB5lQx82SU564.png

然後就可以訪問剛纔上傳的圖片了

在這裏插入圖片描述

五,改造成業務代碼

html表單項

		<form action="/demo/fastdfs" method="post" enctype="multipart/form-data">
			fastDFS測試:<input type="file" name="test"><input type="submit" value="測試上傳">
		</form>

controller上傳業務代碼

  @Autowired
    FastFileStorageClient fastFileStorageClient;

    /**
     *fastDFS服務器測試文件上傳
     */
    @RequestMapping("/fastdfs")
    @ResponseBody
    public String fastdfs(@RequestParam(value = "test") MultipartFile test) throws IOException {

        //文件名
        String fileName=test.getOriginalFilename();
        //後綴名
        String extName=fileName.substring(fileName.lastIndexOf(".")+1);
        //四個參數(輸入流,文件大小,後綴名,null),返回一個路徑
        StorePath storePath = fastFileStorageClient.uploadFile(test.getInputStream(),test.getSize(), extName, null);
        //不同路徑
        System.out.println(storePath.getFullPath());
        System.out.println(storePath.getPath());
        System.out.println(storePath.getGroup());
        return "圖片上傳成功,並調皮的給您返回一個路徑";
    }

控制檯打印

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-FjUqMhh8-1572494243015)(D:\文件筆記\image\1572491669585.png)]

把第一條路徑,也就是storePath.getFullPath()這樣得到的路徑拼接到nginx的ip地址和端口後面,在瀏覽器進行訪問,可以訪問到上傳的圖片。

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-mFT2LoC2-1572494243016)(D:\文件筆記\image\1572491793124.png)]

說明:如果上傳的是一個文件,這裏比如上傳一個docx格式的word文檔,那麼上傳成功之後返回的那個路徑,group1/M00/00/00/wKi6gV26VG2AG36RAAeqJLEbOEY03.docx,拼接路徑之後http://192.168.186.129/group1/M00/00/00/wKi6gV26VG2AG36RAAeqJLEbOEY03.docx,在瀏覽器直接訪問這個路徑的話會直接把這個word文檔下載下來,圖片的話就只是查看。

原因是什麼?是因爲瀏覽器的緣故,如果文件格式可以被瀏覽器解析的話,瀏覽器會展示該文件,不會下載,解析不了的文件纔可以下載,爲什麼是這樣我也不太清楚。

在這裏插入圖片描述

FastDFS服務器和nginx的部署以及springboot項目上傳文件到FastDFS服務器圓滿結束!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章