
docker 鏡像管理平台。連結,https://hub.docker.com。
找到 ygqygq2/fastdfs-nginx 。
安裝。
// 示例中建立了網卡,我們直接用已有的 host 網卡就好
// 查詢已有網卡
docker network ls
// 搜尋鏡像
docker search fastdfs
// 拉取鏡像
docker pull ygqygq2/fastdfs-nginx
// 運作 tracker
docker run -dit --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs ygqygq2/fastdfs-nginx:latest tracker
// 運作 storage0, ip 要改為自己虛拟機的
docker run -dit --network=host --name storage0 -e TRACKER_SERVER=10.36.144.17:22122 -v /var/fdfs/storage0:/var/fdfs ygqygq2/fastdfs-nginx:latest storage
// 删除 容器
docker rm storage0
// 網頁測試是否連接配接到 nginx
http://10.36.144.17:8080/
進入容器内部檢視配置檔案的相關指令。
// 檢視 nginx 是否運作
ps -ef | grep nginx
// 列出所有nginx程序
netstat -nap | grep nginx
// 殺死程序
kill pid
// 進入 storage0
docker exec -it storage0 /bin/bash
// fastdfs 的配置檔案
cd /etc/fdfs
vi mod_fastdfs.conf
vi storage.conf
// nginx 的配置檔案
cd /usr/local/nginx/conf/conf.d
vi storage.conf
// 重新開機 nginx
nginx -s reload
java 中使用 fastfds 。
// pom.xml
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
</dependency>
// Application.java 添加注解
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
// application.yml
fdfs:
tracker-list:
- 10.36.144.17:22122
thumb-image:
height: 150
width: 150
path: http://10.36.144.17:8080/
// FileUploadController.java
@RestController
@RequestMapping("/fileUpload")
public class FileUploadController {
@Autowired
private FastFileStorageClient client;
@Value("${fdfs.path}")
private String nginxPath;
@RequestMapping("/upload")
public String upload(MultipartFile file) {
try {
// 1.擷取檔案的字尾
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
// 2.把檔案上傳到FAstDFS中
StorePath storePath = client.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), extension, null);
// 3.擷取檔案的路徑
String fullPath = storePath.getFullPath();
return nginxPath + fullPath;
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
}