一,前言
(1)FastDFS
是什么
FastDFS
是用 c 語言編寫的一款開源的分布式文件系統。FastDFS
為互聯網量身定制,充分考慮了冗余備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,使用 FastDFS
很容易搭建一套高性能的文件服務器集群提供文件上傳、下載等服務。
(2)FastDFS
結構
FastDFS
架構包括 Tracker server
和 Storage 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
路徑為
保存退出,然后啟動
# 啟動
/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
修改配置文件
新建一個在root
根目錄創建一個html
文件,內容為hi,fastdfs!
。
測試上傳
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/hi.html
出現下面的情況就是安裝成功了,並且文件上傳成功。
文件存在了哪里呢?這里
[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
的插件,目的是建立nginx
和FastDFS
服務器的聯系以供訪問。
1,安裝
nginx
及nginx
插件
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
全部去掉,並保存退出。
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
關聯nginx
和nginx
的插件
[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
目錄下
到/usr/local/nginx/conf
目錄下,修改nginx
的配置文件nginx.conf
,保存退出。
啟動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 "圖片上傳成功,並調皮的給您返回一個路徑";
}
控制台打印
把第一條路徑,也就是storePath.getFullPath()
這樣得到的路徑拼接到nginx
的ip地址和端口后面,在瀏覽器進行訪問,可以訪問到上傳的圖片。
說明:如果上傳的是一個文件,這里比如上傳一個docx
格式的word
文檔,那么上傳成功之后返回的那個路徑,group1/M00/00/00/wKi6gV26VG2AG36RAAeqJLEbOEY03.docx
,拼接路徑之后http://192.168.186.129/group1/M00/00/00/wKi6gV26VG2AG36RAAeqJLEbOEY03.docx
,在瀏覽器直接訪問這個路徑的話會直接把這個word
文檔下載下來,圖片的話就只是查看。
原因是什么?是因為瀏覽器的緣故,如果文件格式可以被瀏覽器解析的話,瀏覽器會展示該文件,不會下載,解析不了的文件才可以下載,為什么是這樣我也不太清楚。
FastDFS
服務器和nginx
的部署以及springboot
項目上傳文件到FastDFS
服務器圓滿結束!