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";
}
}