使用Docker一键搭建FastDFS Nginx分布式文件服务器

- 日理万妓 2024-04-17 19:22 153阅读 0赞

拉取镜像并启动

  1. docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=106.54.238.xxx -e WEB_PORT=8000 -v ${HOME}/fastdfs:/var/local/fdfs registry.cn-beijing.aliyuncs.com/tianzuo/fastdfs

需要开放3个端口, fastdfs-tracker(22122) fastdfs-storage(23000) fastdfs-nginx(8000)

firewall-cmd —zone=public —add-port=8000/tcp —permanent

firewall-cmd —reload

$HOME是用户地主目录,登录后缺省进入的目录, cd ~进入

其中-v ${HOME}/fastdfs:/var/local/fdfs是指:将${HOME}/fastdfs这个目录挂载到容器里的/var/local/fdfs这个目录里。

所以上传的文件将被持久化到${HOME}/fastdfs/storage/data里,IP 后面是自己的服务器公网ip或者虚拟机ip,

-e WEB_PORT=8000 指定nginx端口

如果有问题可以看一下是否占用8000端口或是否开启22122 23000端口
watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5OTQwMjA1_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5OTQwMjA1_size_16_color_FFFFFF_t_70 1

  • 引入依赖


    com.github.tobato
    fastdfs-client
  • 配置

    fdfs:
    so-timeout: 1501 #超时时间
    connect-timeout: 601 #连接超时时间
    thumb-image: #上传原图和缩略图,可返回缩略图url给前端
    width: 60
    height: 60
    tracker-list: #tracker地址

    • 106.54.238.xxx:22122 #多个 trackerServer中间以逗号分隔
  • Test

    import com.github.tobato.fastdfs.domain.StorePath;
    import com.github.tobato.fastdfs.domain.ThumbImageConfig;
    import com.github.tobato.fastdfs.service.FastFileStorageClient;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class FdfsTest {

    1. @Autowired
    2. private FastFileStorageClient storageClient;
    3. @Autowired
    4. private ThumbImageConfig thumbImageConfig;
    5. //普通上传
    6. @Test
    7. public void testUpload() throws FileNotFoundException {
    8. File file = new File("C:\\Users\\JavaDev\\Desktop\\she.jpg");
    9. // 上传
    10. StorePath storePath = this.storageClient.uploadFile(
    11. new FileInputStream(file), file.length(), "jpg", null);
    12. // 带分组的路径
    13. System.out.println(storePath.getFullPath());
    14. // 不带分组的路径
    15. System.out.println(storePath.getPath());
    16. }
    17. //上传并创建缩略图
    18. @Test
    19. public void testUploadAndCreateThumb() throws FileNotFoundException {
    20. File file = new File("D:\\image\\1.jpg");
    21. // 上传并且生成缩略图
    22. StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
    23. new FileInputStream(file), file.length(), "jpg", null);
    24. // 带分组的路径
    25. System.out.println(storePath.getFullPath());
    26. // 不带分组的路径
    27. System.out.println(storePath.getPath());
    28. // 获取缩略图路径
    29. String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
    30. System.out.println(path);
    31. }

    }

发表评论

表情:
评论列表 (有 0 条评论,153人围观)

还没有评论,来说两句吧...

相关阅读