SpringBoot項目整合FastDFS+Nginx實現(xiàn)圖片上傳功能
FastDFS概述
- FastDFS是一個開源的輕量級分布式文件系統(tǒng),它對文件進(jìn)行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負(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)度,它管理著存儲服務(wù)(Storage Server),可以理解為:“大管家,追蹤者,調(diào)度員”;
- Tracker Server可以集群,實現(xiàn)高可用,策略為“輪詢”。
Storage (貯存器)
- 作用是文件存儲,客戶端上傳的文件最終存儲到storage服務(wù)器上;
- storage集群采用分組的方式,同組內(nèi)的每臺服務(wù)器是平等關(guān)系,數(shù)據(jù)同步,目的是實現(xiàn)數(shù)據(jù)備份,從而高可用,而不同組的服務(wù)器之間是不通信的;
- 同組內(nèi)的每臺服務(wù)器的存儲量不一致的情況下,會選取容量最小的那個,所以同組內(nèi)的服務(wù)器之間軟硬件最好保持一致。
- Storage Server會連接集群中的所有Tracker Server,定時向他們匯報自己的狀態(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運行時會向此目錄存儲storage的管理數(shù)據(jù)) base_path=/usr/local/fastdfs
如果base_path不存在,則需要創(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、啟動服務(wù) 啟動tracker
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
啟動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 #指定文件存儲路徑(上面配置的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,并啟動nginx
pkill -9 nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

9、啟動nginx

SpringBoot整合Fastdfs
1、創(chuàng)建SpringBoot項目 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、啟動類上配置
@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項目整合FastDFS+Nginx實現(xiàn)圖片上傳的文章就介紹到這了,更多相關(guān)SpringBoot整合FastDFS+Nginx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一次nginx 504 Gateway Time-out錯誤排查、解決記錄
這篇文章主要介紹了一次nginx 504 Gateway Time-out錯誤排查、解決記錄,經(jīng)過反復(fù)檢查,發(fā)現(xiàn)造成這個問題的原因就是PHP的CURL沒有設(shè)置超時時間,解決辦法只要設(shè)置超時時間或者修改一下nginx的配置即可解決,需要的朋友可以參考下2014-05-05
nginx開啟https配置之后網(wǎng)頁無法訪問的問題處理解決
最近新購服務(wù)器部署nginx之后按照之前的方式部署前端項目并配置https之后訪問頁面無法顯示,本文主要介紹了nginx開啟https配置之后網(wǎng)頁無法訪問的問題處理解決,具有一定的參考價值,感興趣的可以了解一下2023-11-11
nginx配置請求轉(zhuǎn)發(fā)不生效的實現(xiàn)
本文主要介紹了nginx配置請求轉(zhuǎn)發(fā)不生效的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02

