安装过Fastdfs的朋友都知道,过程是非常复杂,有一个步骤错了,可能就要重头来过,最近我在做毕业设计,就想着能不能用docker搭建一个Fastdfs,学过docker不用岂不是浪费了,最后还是安装好了,而且过程非常简单,下面我来分享一下我的安装过程。
docker pull delron/fastdfs
cd /
mkdir fastdfs
cd /fastdfs
mkdir tracker
mkdir storage
docker run -di --network=host --name=tracker -v /fastdfs/tracker/:/var/fdfs delron/fastdfs tracker
docker run -di --network=host --name storage -e TRACKER_SERVER=ip:22122 -v /fastdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage
之后可以使用docker ps命令查看是否安装好了
5. 现在就已经安装好了,可以测试上传了,如果不放心的话,可进去storage里看一下
docker exec -it storage /bin/bash
可以先看下nginx的配置文件
cd /usr/local/nginx/conf
vim nginx.conf
可以看到fastdfs模块已经配置好了,再看看storage的配置文件
cd /etc/fdfs
vim storage.conf
默认访问端口是8888
6. 测试
将一张图片放到/fastdfs/storage的目录下,进入storage的命令行,执行下面命令
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg
上传成功后,下面会返回一个路劲,在浏览器可以通过ip:8888/路径来访问你上传的图片了
这里是Spring Boot应用程序
<dependency>
<groupId>com.github.tobatogroupId>
<artifactId>fastdfs-clientartifactId>
<version>1.26.7version>
dependency>
fdfs:
connect-timeout: 601
so-timeout: 1500
thumb-image:
height: 60
width: 60
tracker-list:
- ip:22122
@SpringBootTest
@RunWith(SpringRunner.class)
public class FastDFSTest {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;
@Test
public void testUpload() throws FileNotFoundException {
// 要上传的文件
File file = new File("C:\\Users\\14759\\Pictures\\壁纸\\猫2.jpg");
// 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他
StorePath storePath = this.storageClient.uploadFile(
new FileInputStream(file), file.length(), "jpg", null);
// 带分组的路径
System.out.println(storePath.getFullPath());
// 不带分组的路径
System.out.println(storePath.getPath());
}
@Test
public void testUploadAndCreateThumb() throws FileNotFoundException {
File file = new File("C:\\Users\\14759\\Pictures\\壁纸\\猫2.jpg");
// 上传并且生成缩略图
StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
new FileInputStream(file), file.length(), "png", null);
// 带分组的路径
System.out.println(storePath.getFullPath());
// 不带分组的路径
System.out.println(storePath.getPath());
// 获取缩略图路径
String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
System.out.println(path);
}
}
完成后会在控制台打印路径,通过浏览器访问ip:端口/路径就好了