SpringBoot項(xiàng)目整合FastDFS+Nginx實(shí)現(xiàn)圖片上傳功能
FastDFS概述
- FastDFS是一個(gè)開源的輕量級分布式文件系統(tǒng),它對文件進(jìn)行管理,功能包括:文件存儲(chǔ)、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲(chǔ)和負(fù)載均衡的問題。特別適合以文件為載體的在線服務(wù),如相冊網(wǎng)站、視頻網(wǎng)站等等。
- FastDFS為互聯(lián)網(wǎng)量身定制,充分考慮了冗余備份、負(fù)載均衡、線性擴(kuò)容等機(jī)制,并注重高可用、高性能等指標(biāo),使用FastDFS很容易搭建一套高性能的文件服務(wù)器集群提供文件上傳、下載等服務(wù)。
- FastDFS由阿里資深架構(gòu)師余慶開發(fā)。
Fastdfs原理
FastDFS包含Tracker Server和Storage Server; 客戶端請求Tracker Server進(jìn)行文件的上傳與下載; Tracker Server調(diào)度Storage Server最終完成上傳與下載。
Tracker (追蹤者)
- 作用是負(fù)載均衡和調(diào)度,它管理著存儲(chǔ)服務(wù)(Storage Server),可以理解為:“大管家,追蹤者,調(diào)度員”;
- Tracker Server可以集群,實(shí)現(xiàn)高可用,策略為“輪詢”。
Storage (貯存器)
- 作用是文件存儲(chǔ),客戶端上傳的文件最終存儲(chǔ)到storage服務(wù)器上;
- storage集群采用分組的方式,同組內(nèi)的每臺(tái)服務(wù)器是平等關(guān)系,數(shù)據(jù)同步,目的是實(shí)現(xiàn)數(shù)據(jù)備份,從而高可用,而不同組的服務(wù)器之間是不通信的;
- 同組內(nèi)的每臺(tái)服務(wù)器的存儲(chǔ)量不一致的情況下,會(huì)選取容量最小的那個(gè),所以同組內(nèi)的服務(wù)器之間軟硬件最好保持一致。
- Storage Server會(huì)連接集群中的所有Tracker Server,定時(shí)向他們匯報(bào)自己的狀態(tài),例如:剩余空間,文件同步情況,文件上傳下載次數(shù)等信息。
上傳文件流程
查詢文件流程
安裝Fastdfs
1、安裝gcc
yum install -y gcc gcc-c++
2、下載libfastcommon到/usr/local下
cd /usr/local wget https://github.com/happyfish200/libfastcommon/archive/V1.0.7.tar.gz
3、解壓libfastcommon
tar -zxvf V1.0.7.tar.gz cd libfastcommon-1.0.7
4、安裝libfastcommon
./make.sh ./make.sh install
5、下載fastdfs
wget https://github.com/happyfish200/fastdfs/archive/V5.05.tar.gz
6、解壓fastdfs并安裝
tar -zxvf V5.05.tar.gz cd fastdfs-5.05/ ./make.sh ./make.sh install
7、將conf目錄下的所有文件復(fù)制到/etc/fdfs/
cp /usr/local/fastdfs-5.05/conf/* /etc/fdfs/
8、配置tracker
cd /etc/fdfs vi tracker.conf
主要配置
#端口號 port=22122 #基礎(chǔ)目錄(Tracker運(yùn)行時(shí)會(huì)向此目錄存儲(chǔ)storage的管理數(shù)據(jù)) base_path=/usr/local/fastdfs
如果base_path不存在,則需要?jiǎng)?chuàng)建目錄
mkdir /usr/local/fastdfs
9、配置storage
cd /etc/fdfs vi storage.conf
主要配置
#配置組名 group_name=group1 #端口 port=23000 #向tracker心跳間隔(秒) heart_beat_interval=30 #基礎(chǔ)目錄,目錄不存在,需要自行創(chuàng)建 base_path=/usr/local/fastdfs #存放文件的位置,目錄不存在,需要自行創(chuàng)建 store_path0=/usr/local/fastdfs/fdfs_storage #配置tracker服務(wù)器:IP tracker_server=192.168.31.168:22122
10、啟動(dòng)服務(wù) 啟動(dòng)tracker
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
啟動(dòng)storage
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
查看服務(wù)
netstat -ntlp
整合Nginx模塊
1、上傳fastdfs-nginx-module_v1.16.tar.gz 到/usr/local 2、解壓nginx模塊
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
3、修改config文件,將文件中的 /usr/local/ 路徑改為 /usr/
cd /usr/local/fastdfs-nginx-module/src vi config
4、將fastdfs-nginx-module/src下的mod_fastdfs.conf拷貝至/etc/fdfs下
cp mod_fastdfs.conf /etc/fdfs/
5、修改/etc/fdfs/mod_fastdfs.conf
vi /etc/fdfs/mod_fastdfs.conf 內(nèi)容: base_path=/usr/local/fastdfs tracker_server=192.168.31.168:22122 #url中包含group名稱 url_have_group_name=true #指定文件存儲(chǔ)路徑(上面配置的store路徑) store_path0=/usr/local/fastdfs/fdfs_storage
6、將libfdfsclient.so拷貝至/usr/lib下
cp /usr/lib64/libfdfsclient.so /usr/lib/
7、創(chuàng)建nginx/client目錄
mkdir -p /var/temp/nginx/client
安裝Nginx
1、 將nginx-1.8.0.tar.gz上傳到/usr/local 2、解壓:tar -zxvf nginx-1.8.0.tar.gz 3、安裝依賴庫
yum install pcre yum install pcre-devel yum install zlib yum install zlib-devel yum install openssl yum install openssl-devel
4、進(jìn)入nginx解壓的目錄下:
cd /usr/local/nginx-1.8.0
5、安裝
./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi \ --add-module=/usr/local/fastdfs-nginx-module/src
編譯、安裝
make make install
安裝成功 6、拷貝配置文件http.conf和mime.types
cd /usr/local/fastdfs-5.0.5/conf cp http.conf mime.types /etc/fdfs/
7、修改nginx配置文件
cd /usr/local/nginx/conf/ vi nginx.conf
8、關(guān)閉nginx,并啟動(dòng)nginx
pkill -9 nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
9、啟動(dòng)nginx
SpringBoot整合Fastdfs
1、創(chuàng)建SpringBoot項(xiàng)目 2、引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.4</version> </dependency>
3、啟動(dòng)類上配置
@Import(FdfsClientConfig.class) @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
4、配置文件
fdfs.so-timeout=3000 fdfs.connect-timeout=1000 fdfs.thumb-image.height=60 fdfs.thumb-image.width=60 fdfs.tracker-list=192.168.31.168:22122
5、控制器
@Controller public class UploadController { public static final String DIR = "http://192.168.31.168/"; @Autowired private FastFileStorageClient client; @RequestMapping("login") public String login(){ return "login"; } @ResponseBody @RequestMapping(value = "/upload",method = RequestMethod.POST) public JsonResult upload(MultipartFile file) throws IOException { //獲得后綴名 String extension = FilenameUtils.getExtension(file.getOriginalFilename()); //上傳 StorePath storePath = client.uploadFile(file.getInputStream(), file.getSize(), extension, null); System.out.println("save:" + storePath.getFullPath()); return new JsonResult(1,DIR + storePath.getFullPath()); } }
Java對象
public class JsonResult { private Integer code; private Object data; //get/set/constructor }
6、測試頁面 頁面使用了Vue+ElementUI
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>上傳</title> <link rel="stylesheet" href="/elementui/index.css" rel="external nofollow" > <style> ... </style> </head> <body> <div id="app"> <el-card > <el-upload class="avatar-uploader" action="/upload" :show-file-list="false" :on-success="handleAvatarSuccess"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-card> </div> <script src="/vue/vue.js"></script> <script src="/elementui/index.js"></script> <script> new Vue({ el:"#app", data(){ return{ imageUrl: '' } }, methods:{ handleAvatarSuccess(res, file) { console.log(res); this.imageUrl = res.data; } } }) </script> </body> </html>
上傳效果
到此這篇關(guān)于SpringBoot項(xiàng)目整合FastDFS+Nginx實(shí)現(xiàn)圖片上傳的文章就介紹到這了,更多相關(guān)SpringBoot整合FastDFS+Nginx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Nginx服務(wù)器中配置Sysguard模塊預(yù)防高負(fù)載的方案
這篇文章主要介紹了詳解Nginx服務(wù)器中配置Sysguard模塊預(yù)防高負(fù)載的方案,該模塊由阿里巴巴的團(tuán)隊(duì)開發(fā),能夠設(shè)置負(fù)載閥值,比較強(qiáng)大,需要的朋友可以參考下2016-01-01Nginx可視化管理工具結(jié)合cpolar實(shí)現(xiàn)遠(yuǎn)程訪問的步驟詳解
Nginx Proxy Manager 是一個(gè)開源的反向代理工具,本文將給大家介紹在Linux 安裝Nginx Proxy Manager并且結(jié)合 cpolar內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)遠(yuǎn)程訪問管理界面,同等,當(dāng)我們使用Nginx Proxy Manager配置其他本地服務(wù),并且需要遠(yuǎn)程訪問,也是同樣的方式,需要的朋友可以參考下2023-09-09詳解nginx配置location總結(jié)及rewrite規(guī)則寫法
本篇文章主要介紹了詳解nginx配置location總結(jié)及rewrite規(guī)則寫法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02nginx開啟https配置之后網(wǎng)頁無法訪問的問題處理解決
最近新購服務(wù)器部署nginx之后按照之前的方式部署前端項(xiàng)目并配置https之后訪問頁面無法顯示,本文主要介紹了nginx開啟https配置之后網(wǎng)頁無法訪問的問題處理解決,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11NGINX服務(wù)器配置404錯(cuò)誤頁面轉(zhuǎn)向的方法
這篇文章主要為大家詳細(xì)介紹了NGINX服務(wù)器配置404錯(cuò)誤頁面轉(zhuǎn)向的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Nginx圖片服務(wù)器配置之后圖片訪問404的問題解決
本文主要介紹了Nginx圖片服務(wù)器配置之后圖片訪問404的問題解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Nginx設(shè)置靜態(tài)頁面壓縮和緩存過期時(shí)間的方法
這篇文章主要介紹了Nginx設(shè)置靜態(tài)頁面壓縮和緩存過期時(shí)間的方法,也是服務(wù)器架設(shè)后的必備設(shè)置,需要的朋友可以參考下2015-07-07