环境
- VMware Workstation Pro 16
- CentOS-7-x86_64-Minimal-2009(虚拟机地址:
192.168.175.128
)- Docker version 20.10.14, build a224086
- season/fastdfs:1.2
安装步骤
安装docker
这里使用官方脚本一键安装curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
拉取镜像
-
docker pull season/fastdfs:1.2
-
创建需要的目录
-
新增用户zeriter
useradd zeriter
-
修改密码
passwd zeriter
-
/etc/sudoers文件追加
zeriter ALL=(ALL) ALL
-
关闭防火墙
systemctl stop firewalld.service
-
切换用户
su zeriter
-
进入家目录
cd ~
-
创建需要的目录
mkdir -p ~/fdfs/tracker/data
mkdir -p ~/fdfs/storage/data
mkdir -p ~/fdfs/storage/path
-
启动
sudo docker run -id --name tracker -p 22122:22122 --restart=always --net host -v ~/fdfs/tracker/data:/fastdfs/tracker/data season/fastdfs:1.2 tracker
sudo docker run -id --name storage --restart=always --net host -v ~/fdfs/storage/data:/fastdfs/store_path -e TRACKER_SERVER="192.168.175.128:22122" season/fastdfs:1.2 storage
-
复制nginx文件
sudo docker cp storage:/etc/nginx/conf/nginx.conf ~/fdfs/nginx/
-
修改nginx.conf配置文件
location / { root /fastdfs/store_path/data; ngx_fastdfs_module; }
sudo docker run -id --name fdfs_nginx --restart=always -v ~/fdfs/storage/data:/fastdfs/store_path -v ~/fdfs/nginx/nginx.conf:/etc/nginx/conf/nginx.conf -p 80:80 -e TRACKER_SERVER=192.168.175.128:22122 season/fastdfs:1.2 nginx
-
测试
代码:
package com.example.demo;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.*;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private FastFileStorageClient storageClient;
@Test
void contextLoads() {
InputStream is = null;
try{
// 获取文件源
File source = new File("C:\\Users\\ZERITER\\Pictures\\wallhaven-xlljqv.jpg");
// 获取文件流
is = new FileInputStream(source);
// 进行文件上传
StorePath storePath = storageClient.uploadFile(is, source.length(), FilenameUtils.getExtension(source.getName()), null);
// 获得文件上传后访问地址
String fullPath = storePath.getFullPath();
// 打印访问地址
System.out.println("fullPath = " + fullPath);
}
catch (FileNotFoundException e){
e.printStackTrace();
}
finally{
try
{
if(is != null){
// 关闭流资源
is.close();
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
}
结果: