SpringBoot整合FastDFS文件系統(tǒng)
FastDFS文件系統(tǒng)的介紹請(qǐng)自行網(wǎng)上學(xué)習(xí),理論上的這里就不寫(xiě)了。
Docker搭建FastDFS服務(wù)器:
服務(wù)器環(huán)境:Linux Ubuntu
沒(méi)有安裝Docker的話(huà)請(qǐng)自行搜索安裝。
① 拉取鏡像:
docker pull delron/fastdfs② 創(chuàng)建映射目錄
fastdfs.zip下載鏈接: https://stand.lanzoui.com/iJZijqpqiqb
拷貝fastdfs文件夾到/home/u01目錄下作為fastdfs的存儲(chǔ)目錄(注意創(chuàng)建/home/u01目錄)。
③ 修改配置信息
修改storage目錄下的storage.conf和client.conf配置信息
找到如下字段將IP地址改為自己的IP:
注意:兩個(gè)文件該字段都要修改。
tracker_server=x.x.x.x:22122④ 創(chuàng)建tracker容器(跟蹤服務(wù)器容器)
docker run -d --restart=always --network=host \
-v /home/u01/fastdfs/tracker:/var/fdfs \
--name tracker \
delron/fastdfs tracker如果出現(xiàn)警告信息,并且端口并沒(méi)有起到作用,可以嘗試將host連接方式改為bridge,如果是內(nèi)網(wǎng)環(huán)境下搭建,可以忽略此條。
?-d:讓容器在后臺(tái)運(yùn)行?--name:指定容器創(chuàng)建的名稱(chēng)?-v:容器跟宿主機(jī)之間的掛載目錄
⑤ 創(chuàng)建storage容器(存儲(chǔ)服務(wù)器容器)
docker run -d --restart=always --network=host \
-v /home/u01/fastdfs/storage/data:/var/fdfs/data \
-v /home/u01/fastdfs/storage/thumb:/var/fdfs/thumb \
-v /home/u01/fastdfs/storage/logs:/var/fdfs/logs \
-v /home/u01/fastdfs/storage/conf/storage.conf:/etc/fdfs/storage.conf \
-v /home/u01/fastdfs/storage/conf/client.conf:/etc/fdfs/client.conf \
-e GROUP_NAME=group1 \
--name storage \
delron/fastdfs storage⑥ 查看監(jiān)控狀態(tài)
docker exec -it storage fdfs_monitor /etc/fdfs/client.conf參數(shù)說(shuō)明:
如出現(xiàn)如下這三個(gè)字段,說(shuō)明服務(wù)正常啟動(dòng)。
tracker server is x.x.x.x:22122 --表示Leader Tracker
group count: 1 --表示有1個(gè)group
active server count = 2 --活動(dòng)的storage有2個(gè)通過(guò)Nginx訪問(wèn)上傳的文件
location /group1/M00 {
alias /data/storage/data;
}配置Nginx配置文件:
/group1/M00:存儲(chǔ)節(jié)點(diǎn),幾個(gè)節(jié)點(diǎn)就寫(xiě)幾個(gè)節(jié)點(diǎn);
/data/storage/data:文件路徑;
訪問(wèn):域名/存儲(chǔ)地址,如:tworice.cn/存儲(chǔ)地址
Boot項(xiàng)目整合FastDFS
① 導(dǎo)入依賴(lài)
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>② 創(chuàng)建配置類(lèi)
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
/**
* FastDFS文件上傳配置類(lèi)
* @author 二飯
* @email [email protected]
*/
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfig {
}③ 配置yml文件
# FastDFS配置
fdfs:
connect-timeout: 30000 # 連接超時(shí)時(shí)間
so-timeout: 20000 # 讀取超時(shí)時(shí)間
thumb-image:
width: 60
height: 60
tracker-list: # tracker-list參數(shù),支持多個(gè)
- x.x.x.x:22122④ 創(chuàng)建工具類(lèi)
@Service
public class FastDFSClient {
@Autowired
private FastFileStorageClient storageClient;
/**
* 上傳文件
* @param file 文件對(duì)象
* @return 文件訪問(wèn)地址
* @throws IOException
*/
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
return getResAccessUrl(storePath);
}
/**
* 刪除文件
* @param fileUrl 文件訪問(wèn)地址
* @return
*/
public void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
System.out.println(e.getMessage());
/** TODO 只是測(cè)試,所以未使用日志,正式環(huán)境請(qǐng)修改打印方式 **/
}
}
}由于代碼量比較多,所以這里只放了上傳和刪除兩個(gè)方法。
點(diǎn)擊關(guān)注公眾號(hào),查看更多內(nèi)容:
