Docker + FastDFS + Spring Boot 一鍵式搭建分布式文件服務(wù)器
點(diǎn)擊上方 好好學(xué)java ,選擇 星標(biāo) 公眾號(hào)
重磅資訊,干貨,第一時(shí)間送達(dá) 今日推薦:14 個(gè) github 項(xiàng)目!
個(gè)人原創(chuàng)100W +訪(fǎng)問(wèn)量博客:點(diǎn)擊前往,查看更多
首先說(shuō)一下從零開(kāi)始自己去搭一個(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這篇文章
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 文件訪(fǎng)問(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 文件訪(fǎng)問(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訪(fǎng)問(wèn)地址
web-server-url: http://192.168.127.131/
spring:
http:
multipart:
max-file-size: 100MB # 最大支持文件大小
max-request-size: 100MB # 最大支持請(qǐng)求大小測(cè)試類(lèi)
@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
推薦文章
955 互聯(lián)網(wǎng)公司白名單來(lái)了!這些公司月薪20k,沒(méi)有996!福利榜國(guó)內(nèi)大廠(chǎng)只有這家!
一款基于 Spring Boot 的現(xiàn)代化社區(qū)(論壇/問(wèn)答/社交網(wǎng)絡(luò)/博客)
更多項(xiàng)目源碼
評(píng)論
圖片
表情
