大家好今兒給大家帶來的是springboot 整合分布式檔案存盤服務器fastDFS并使用docker部署fastDFS
目錄
- 大家好今兒給大家帶來的是springboot 整合分布式檔案存盤服務器fastDFS并使用docker部署fastDFS
- 1.fastDFS簡單介紹(個人理解)
- 2.使用docker安裝fastDFS
- 3.springboot整合fastDFS
- 4.測驗
- 5.總結
1.fastDFS簡單介紹(個人理解)
1.FastDFS是一個開源的輕量級分布式檔案存盤系統,可以供我們去完成上傳下載,
2.在FastDFS中有兩部分組成一個叫tracker一個叫storage,
| tracker | storage |
|---|---|
| tracker 是專門去管理storage的,在FastDFS中所有的訪問都會先經過tracker ,并由tracker 去找有哪些 storage壓力小或者比較空閑那么tracker 就會回傳一個storage地址供我們去上傳下載 | storage的職責其實就是保存上傳的資料以及對資料進行下載 |
2.使用docker安裝fastDFS
1.查看 fastdfs 都有哪些版本
docker search fastdfs
2.我這里選擇的是delron/fastdfs版本(其他的版本也是可以的不要用太新的版本)
docker pull delron/fastdfs
3.安裝完畢后可以在docekr鏡像庫中看到

4.首先啟動tracker
docker run -d --network=host --name tracker -v /home/fastdfs/docker/fastdfs/tracker:/var/fdfs delron/fastdfs tracker
5.啟動storage
注意:
1.TRACKER_SERVER=ip:22122,ip要自己改成當前服務器的ip
2.GROUP_NAME=group1,storage組名稱可以自己隨便寫
docker run -d --network=host --name storage -e TRACKER_SERVER=ip:22122 -v /home/fastdfs/docker/fastdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage
注意:全部啟動成功后fastdfs默認的埠有三個8888,23000,22122,大家需要在服務器中放行這三個埠
8888:是默認的nginx代理埠 delron/fastdfs這個版本包含有nginx
23000:storage服務埠
22122:tracker服務埠
3.springboot整合fastDFS
1.我的boot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
2.fastDFS坐標
<!--分布式檔案存盤 fastDFS-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
<!--上傳下載 間接包-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
3.需要在boot啟動類再加上兩個注解
//加載fastDFS
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
@Import(FdfsClientConfig.class)
4.在yml中添加連接fastdfs的地址
#fastDFS
注意這個trackerList:如果是集群tracker的話以逗號分隔開就行
fdfs:
connect-timeout: 600
so-timeout: 1500
trackerList: 服務器tracker的ip:22122
thumb-image:
width: 150
height: 150
pool:
max-total: 200
5.最后就是代碼部分啦直接上代碼
@RestController
@RequestMapping("/file")
@Api(value = "fastDFs分布式檔案存盤 -- 檔案上傳下載",description = "fastDFs分布式檔案存盤 -- 檔案上傳下載")
@CrossOrigin//跨域的意思
public class FileController {
//FastFileStorageClient 直接注入就能用 fastdsf自帶的
@Resource
private FastFileStorageClient fastFileStorageClient;
//fastDFS storage存盤節點路徑 xxxxxx:服務器ip
private final String FASTDFSSERVERIMAGE = "http://xxxxxx:8888/";
/**
* 檔案上傳
* @return result
*/
@ApiOperation("上傳")
@Async("asyncServiceExecutor")
@RequestMapping(value = "/upload",method = RequestMethod.POST)
public HashMap<String, String> upload(@RequestPart("file") MultipartFile file){
HashMap<String,String> result = new HashMap<>();
try {
//這里msg回傳的是上傳后的圖片存盤位置getFullPath()獲取檔案位置
result.put("msg",FASTDFSSERVERIMAGE+fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()),null).getFullPath());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 檔案洗掉
* @param path
* @return
*/
@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
@ApiOperation("洗掉")
public HashMap<String, Object> delete(@RequestParam String path) {
HashMap<String,Object> result = new HashMap<>();
fastFileStorageClient.deleteFile(path);
result.put("msg","大哥啦~~~!!,洗掉成功!");
return result;
}
/**
* 檔案下載
* @param url 路徑
* @return
*/
@RequestMapping(value = "/download",method = RequestMethod.GET)
@ApiOperation("下載")
public void downLoad(@RequestParam String url, HttpServletResponse response) throws IOException {
//檔案后綴
String substring = url.substring(url.lastIndexOf(".") + 1);
byte[] bytes = fastFileStorageClient.downloadFile(url.substring(0, url.indexOf("/")), url.substring(url.indexOf("/") + 1), new DownloadByteArray());
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(UUIDUtils.getUUID() +"."+substring, "UTF-8"));
// 寫出
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.write(bytes, outputStream);
}
4.測驗
1.上傳


瀏覽器訪問

5.總結
FastDFS是一個開源的分布式檔案存盤系統,有兩部分組成tracker(有負載均衡的作用)負責管理storage(可集群),如有問題歡迎留言,今兒就到這里吧,下期見,

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/246203.html
標籤:其他
上一篇:2021.01.04
