Docker + FastDFS + Spring Boot 一鍵式搭建分布式文件服務(wù)器
點(diǎn)擊上方藍(lán)色“肉眼品世界”,選擇“設(shè)為星標(biāo)” 深度價(jià)值體系傳遞!
首先說(shuō)一下從零開始自己去搭一個(gè)fastdfs挺麻煩,后來(lái)看到有人把做好的 docker 鏡像傳出來(lái)了,那搭建起來(lái)就很容易了
有服務(wù)器的可以自己在服務(wù)器上玩玩,沒(méi)有的可以新建一個(gè)centos7.5虛擬機(jī)玩玩,遇到虛擬機(jī)不能上網(wǎng)和換阿里云的源的問(wèn)題可以參考https://blog.csdn.net/qq_37759106/article/details/82985113這篇文章
PS:更多 Docker 和 Spring Boot 的文章可以關(guān)注微信公眾號(hào)「Java后端」回復(fù)「666」下載技術(shù)棧手冊(cè)。
1.第一步安裝docker:
在 root 權(quán)限下
yum install -y docker-io #安裝docker
service docker star #啟動(dòng)docker
docker -v # 查看docker版本2. 拉取鏡像
docker pull qbanxiaoli/fastdfs啟動(dòng) fastdfs
docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=192.168.127.131 -e WEB_PORT=80 -v ${HOME}/fastdfs:/var/local/fdfs qbanxiaoli/fastdfsIP 后面是你的服務(wù)器公網(wǎng)ip或者虛擬機(jī)的IP,-e WEB_PORT=80 指定 nginx 端口
測(cè)試fastdfs是否搭建成功
docker exec -it fastdfs /bin/bash
echo "Hello FastDFS!">index.html
fdfs_test /etc/fdfs/client.conf upload index.html
能返回 url 就意見(jiàn)搭建成功

這樣 fastdfs 就搭建好啦
下面進(jìn)入 Spring Boot 整合部分
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
package com.yd.client.common;
import com.github.tobato.fastdfs.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
@Component
public class FastDFSClient {
private static Logger log =LoggerFactory.getLogger(FastDFSClient.class);
private static FastFileStorageClient fastFileStorageClient;
private static FdfsWebServer fdfsWebServer;
@Autowired
public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
FastDFSClient.fastFileStorageClient = fastFileStorageClient;
FastDFSClient.fdfsWebServer = fdfsWebServer;
}
/**
* @param multipartFile 文件對(duì)象
* @return 返回文件地址
* @author qbanxiaoli
* @description 上傳文件
*/
public static String uploadFile(MultipartFile multipartFile) {
try {
StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return storePath.getFullPath();
} catch (IOException e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param multipartFile 圖片對(duì)象
* @return 返回圖片地址
* @author qbanxiaoli
* @description 上傳縮略圖
*/
public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) {
try {
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param file 文件對(duì)象
* @return 返回文件地址
* @author qbanxiaoli
* @description 上傳文件
*/
public static String uploadFile(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param file 圖片對(duì)象
* @return 返回圖片地址
* @author qbanxiaoli
* @description 上傳縮略圖
*/
public static String uploadImageAndCrtThumbImage(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
} catch (Exception e) {
log.error(e.getMessage());
return null;
}
}
/**
* @param bytes byte數(shù)組
* @param fileExtension 文件擴(kuò)展名
* @return 返回文件地址
* @author qbanxiaoli
* @description 將byte數(shù)組生成一個(gè)文件上傳
*/
public static String uploadFile(byte[] bytes, String fileExtension) {
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null);
return storePath.getFullPath();
}
/**
* @param fileUrl 文件訪問(wèn)地址
* @param file 文件保存路徑
* @author qbanxiaoli
* @description 下載文件
*/
public static boolean downloadFile(String fileUrl, File file) {
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
FileOutputStream stream = new FileOutputStream(file);
stream.write(bytes);
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
return true;
}
/**
* @param fileUrl 文件訪問(wèn)地址
* @author qbanxiaoli
* @description 刪除文件
*/
public static boolean deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return false;
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
return true;
}
// 封裝文件完整URL地址
public static String getResAccessUrl(String path) {
String url = fdfsWebServer.getWebServerUrl() + path;
log.info("上傳文件地址為:\n" + url);
return url;
}
}# 分布式文件系統(tǒng)fastdfs配置
fdfs:
# socket連接超時(shí)時(shí)長(zhǎng)
soTimeout: 1500
# 連接tracker服務(wù)器超時(shí)時(shí)長(zhǎng)
connectTimeout: 600
pool:
# 從池中借出的對(duì)象的最大數(shù)目
max-total: 153
# 獲取連接時(shí)的最大等待毫秒數(shù)100
max-wait-millis: 102
# 縮略圖生成參數(shù),可選
thumbImage:
width: 150
height: 150
# 跟蹤服務(wù)器tracker_server請(qǐng)求地址,支持多個(gè),這里只有一個(gè),如果有多個(gè)在下方加- x.x.x.x:port
trackerList:
- 192.168.127.131:22122
#
# 存儲(chǔ)服務(wù)器storage_server訪問(wèn)地址
web-server-url: http://192.168.127.131/
spring:
http:
multipart:
max-file-size: 100MB # 最大支持文件大小
max-request-size: 100MB # 最大支持請(qǐng)求大小測(cè)試類
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileClientApplicationTests {
@Test
public void Upload() {
String fileUrl = this.getClass().getResource("/test.jpg").getPath();
File file = new File(fileUrl);
String str = FastDFSClient.uploadFile(file);
FastDFSClient.getResAccessUrl(str);
}
@Test
public void Delete() {
FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg");
}
}


blog.csdn.net/qq_37759106/article/details/82981023
推薦閱讀:
世界的真實(shí)格局分析,地球人類社會(huì)底層運(yùn)行原理
企業(yè)IT技術(shù)架構(gòu)規(guī)劃方案
華為內(nèi)網(wǎng)最火的文章:什么是內(nèi)卷?
不是你需要中臺(tái),而是一名合格的架構(gòu)師(附各大廠中臺(tái)建設(shè)PPT)
華為內(nèi)部幾近滿分的項(xiàng)目管理PPT,牛逼了
阿里達(dá)摩院《機(jī)器學(xué)習(xí)算法學(xué)習(xí)指南》火了,限時(shí)開放下載!

