簡介
FastDFS是用c語言編寫的一款開源的分布式文件系統(tǒng),充分考慮了冗余備份、負載均衡、線性擴容等機制,并注重高可用、高性能等指標,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題。特別適合中小文件(建議范圍:4KB < file_size <500MB),對以文件為載體的在線服務,如相冊網站、視頻網站等。
安裝
如果一步步安裝FastDFS以及其依賴,極其復雜,有可能你搗鼓一上午不一定能配置好!這里建議大家使用Docker一鍵安裝,只需要把存儲目錄映射出來即可。
docker run --name fastdfs --privileged=true --net=host \
-e IP=192.168.56.10 -e WEB_PORT=8080 \
-v /home/fastdfs:/var/local/fdfs \
-d --restart=always \
registry.cn-beijing.aliyuncs.com/itstyle/fastdfs:1.0
安裝成功以后,會在 /home/fastdfs 目錄下生成兩個目錄文件夾,tracker 和 storage。
這里需要注意幾點:
一定要映射宿機存儲文件路徑
IP一定要配置為內網或者公網地址
網絡類型一定要設置為 host 類型
安裝完成以后,瀏覽器訪問:http://ip:8080 如果顯示Nginx歡迎頁說明安裝配置成功。
整合
接下來我們就要與SpringBoot整合了,pom.xml引入第三方工具類:
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.6</version>
</dependency>
在 application.properties 引入配置:
# ===================================
# 分布式文件系統(tǒng)FDFS配置
# ===================================
# 連接超時時間
fdfs.connect-timeout=600
# 讀取超時時間
fdfs.so-timeout=1500
# 縮略圖的寬高
fdfs.thumb-image.height=150
fdfs.thumb-image.width=150
# tracker服務配置地址列表,替換成自己服務的IP地址,支持多個
fdfs.tracker-list=192.168.1.15:22122
fdfs.pool.jmx-enabled=false
最后寫個工具類,基本搞定,坐等上傳:
@Component
public class FastdfsUtil {
public static final String DEFAULT_CHARSET = "UTF-8";
@Autowired
private FastFileStorageClient fastFileStorageClient;
/**
* 上傳
*/
public StorePath upload(MultipartFile file) throws IOException {
// 設置文件信息
Set<MetaData> mataData = new HashSet<>();
mataData.add(new MetaData("author", "fastdfs"));
mataData.add(new MetaData("description", file.getOriginalFilename()));
// 上傳
return fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
}
/**
* 刪除
*/
public void delete(String path) {
fastFileStorageClient.deleteFile(path);
}
/**
* 刪除
*/
public void delete(String group, String path) {
fastFileStorageClient.deleteFile(group, path);
}
/**
* 文件下載
*
* @param path 文件路徑,例如:/group1/M00/00/00/itstyle.png
* @param filename 下載的文件命名
*/
public void download(String path, String filename, HttpServletResponse response) throws IOException {
// 獲取文件
StorePath storePath = StorePath.parseFromUrl(path);
if (StringUtils.isBlank(filename)) {
filename = FilenameUtils.getName(storePath.getPath());
}
byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
response.reset();
response.setContentType("applicatoin/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.close();
}
}
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private FastdfsUtil fastdfsUtil;
@PostMapping(value = "/uploadFile")
public void uploadFile(HttpServletRequest request) {
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = mRequest.getFile("file");
try {
StorePath storePath = fastdfsUtil.upload(multipartFile);
System.out.println(storePath.getPath());
} catch (IOException e) {
e.printStackTrace();
}
}
@GetMapping(value = "/downloadFile")
public void downloadFile(HttpServletResponse response) {
String path = "/group1/M00/00/00/wKg4CmCaXRWAMDx1AAHqjF-9HMI374.log";
try {
fastdfsUtil.download(path, "start.log", response);
} catch (IOException e) {
e.printStackTrace();
}
}
}
生產環(huán)境中建議集群使用,搭建高可靠的云存儲服務。
如果是本地開發(fā)云服務器需要開放 8080、22122 端口,生產環(huán)境不建議開啟,所有的請求最好走內網。