zoukankan      html  css  js  c++  java
  • Docker + Spring Boot + FastDFS 搭建一套分布式文件服务器,太强了!

    首先说一下从零开始自己去搭一个fastdfs挺麻烦,后来看到有人把做好的 docker 镜像传出来了,那搭建起来就很容易了

    1.第一步安装docker:

    在 root 权限下

    yum install -y docker-io #安装docker
    service docker star #启动docker
    docker -v # 查看docker版本
    

    2. 拉取镜像

    docker pull qbanxiaoli/fastdfs
    

    启动 fastdfs

    docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=192.168.127.131 -e WEB_PORT=80 -v ${HOME}/fastdfs:/var/local/fdfs qbanxiaoli/fastdfs
    
    IP 后面是你的服务器公网ip或者虚拟机的IP,-e WEB_PORT=80 指定 nginx 端口
    

    测试fastdfs是否搭建成功

    docker exec -it fastdfs /bin/bash
    echo "Hello FastDFS!">index.html
    fdfs_test /etc/fdfs/client.conf upload index.html
    

    能返回 url 就意见搭建成功

    这样 fastdfs 就搭建好啦

    下面进入 Spring Boot 整合部分

    <groupId>com.github.tobato</groupId>
       <artifactId>fastdfs-client</artifactId>
    <version>1.26.2</version>
    

    在 Spring Boot 启动类上加

    @Import(FdfsClientConfig.class)
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
    

    创建FastDFSClient工具类

    package com.yd.client.common;
    
    import com.github.tobato.fastdfs.conn.FdfsWebServer;
    import com.github.tobato.fastdfs.domain.StorePath;
    import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
    import com.github.tobato.fastdfs.service.FastFileStorageClient;
    import org.apache.commons.io.FilenameUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.*;
    
    @Component
    public class FastDFSClient {
    
        private static Logger log =LoggerFactory.getLogger(FastDFSClient.class);
    
        private static FastFileStorageClient fastFileStorageClient;
    
        private static FdfsWebServer fdfsWebServer;
    
        @Autowired
        public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
            FastDFSClient.fastFileStorageClient = fastFileStorageClient;
            FastDFSClient.fdfsWebServer = fdfsWebServer;
        }
    
        /**
         * @param multipartFile 文件对象
         * @return 返回文件地址
         * @author qbanxiaoli
         * @description 上传文件
         */
        public static String uploadFile(MultipartFile multipartFile) {
            try {
                StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
                return storePath.getFullPath();
            } catch (IOException e) {
                log.error(e.getMessage());
                return null;
            }
        }
    
        /**
         * @param multipartFile 图片对象
         * @return 返回图片地址
         * @author qbanxiaoli
         * @description 上传缩略图
         */
        public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) {
            try {
                StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
                return storePath.getFullPath();
            } catch (Exception e) {
                log.error(e.getMessage());
                return null;
            }
        }
    
        /**
         * @param file 文件对象
         * @return 返回文件地址
         * @author qbanxiaoli
         * @description 上传文件
         */
        public static String uploadFile(File file) {
            try {
                FileInputStream inputStream = new FileInputStream(file);
                StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
                return storePath.getFullPath();
            } catch (Exception e) {
                log.error(e.getMessage());
                return null;
            }
        }
    
        /**
         * @param file 图片对象
         * @return 返回图片地址
         * @author qbanxiaoli
         * @description 上传缩略图
         */
        public static String uploadImageAndCrtThumbImage(File file) {
            try {
                FileInputStream inputStream = new FileInputStream(file);
                StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
                return storePath.getFullPath();
            } catch (Exception e) {
                log.error(e.getMessage());
                return null;
            }
        }
    
        /**
         * @param bytes byte数组
         * @param fileExtension 文件扩展名
         * @return 返回文件地址
         * @author qbanxiaoli
         * @description 将byte数组生成一个文件上传
         */
        public static String uploadFile(byte[] bytes, String fileExtension) {
            ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
            StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null);
            return storePath.getFullPath();
        }
    
        /**
         * @param fileUrl 文件访问地址
         * @param file 文件保存路径
         * @author qbanxiaoli
         * @description 下载文件
         */
        public static boolean downloadFile(String fileUrl, File file) {
            try {
                StorePath storePath = StorePath.praseFromUrl(fileUrl);
                byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
                FileOutputStream stream = new FileOutputStream(file);
                stream.write(bytes);
            } catch (Exception e) {
                log.error(e.getMessage());
                return false;
            }
            return true;
        }
    
        /**
         * @param fileUrl 文件访问地址
         * @author qbanxiaoli
         * @description 删除文件
         */
        public static boolean deleteFile(String fileUrl) {
            if (StringUtils.isEmpty(fileUrl)) {
                return false;
            }
            try {
                StorePath storePath = StorePath.praseFromUrl(fileUrl);
                fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
            } catch (Exception e) {
                log.error(e.getMessage());
                return false;
            }
            return true;
        }
    
        // 封装文件完整URL地址
        public static String getResAccessUrl(String path) {
            String url = fdfsWebServer.getWebServerUrl() + path;
            log.info("上传文件地址为:\n" + url);
            return url;
        }
    
    }
    

    配置yml文件

    # 分布式文件系统fastdfs配置
    fdfs:
      # socket连接超时时长
      soTimeout: 1500
      # 连接tracker服务器超时时长
      connectTimeout: 600
      pool:
        # 从池中借出的对象的最大数目
        max-total: 153
        # 获取连接时的最大等待毫秒数100
        max-wait-millis: 102
      # 缩略图生成参数,可选
      thumbImage:
         150
        height: 150
      # 跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加- x.x.x.x:port
      trackerList:
        - 192.168.127.131:22122
      #
      # 存储服务器storage_server访问地址
      web-server-url: http://192.168.127.131/
      spring:
        http:
          multipart:
            max-file-size: 100MB # 最大支持文件大小
            max-request-size: 100MB # 最大支持请求大小
    **测试类**@RunWith(SpringRunner.class)@SpringBootTestpublic class FileClientApplicationTests {  @Test public void Upload() {  String fileUrl = this.getClass().getResource("/test.jpg").getPath();  File file = new File(fileUrl);  String str = FastDFSClient.uploadFile(file);  FastDFSClient.getResAccessUrl(str); }  @Test public void Delete() {  FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg"); }}
    

    运行测试类

    返回的 url 就是能访问的地址

    原文链接:https://blog.csdn.net/qq_37759106/article/details/82981023

    版权声明:本文为CSDN博主「kalibiubiubiu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

    近期热文推荐:

    1.1,000+ 道 Java面试题及答案整理(2021最新版)

    2.别在再满屏的 if/ else 了,试试策略模式,真香!!

    3.卧槽!Java 中的 xx ≠ null 是什么新语法?

    4.Spring Boot 2.6 正式发布,一大波新特性。。

    5.《Java开发手册(嵩山版)》最新发布,速速下载!

    觉得不错,别忘了随手点赞+转发哦!

  • 相关阅读:
    [原创]java WEB学习笔记97:Spring学习---Spring 中的 Bean 配置:IOC 和 DI
    [原创]java WEB学习笔记96:Spring学习---Spring简介及HelloWord
    [原创]java WEB学习笔记95:Hibernate 目录
    [原创]java WEB学习笔记94:Hibernate学习之路---session 的管理,Session 对象的生命周期与本地线程绑定
    [原创]java WEB学习笔记93:Hibernate学习之路---Hibernate 缓存介绍,缓存级别,使用二级缓存的情况,二级缓存的架构集合缓存,二级缓存的并发策略,实现步骤,集合缓存,查询缓存,时间戳缓存
    [原创]java WEB学习笔记92:Hibernate学习之路-- -QBC 检索和本地 SQL 检索:基本的QBC 查询,带 AND 和 OR 的QBC,统计查询,排序,分页
    [原创]java WEB学习笔记91:Hibernate学习之路-- -HQL 迫切左外连接,左外连接,迫切内连接,内连接,关联级别运行时的检索策略 比较。理论,在于理解
    [原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询
    [原创]java WEB学习笔记89:Hibernate学习之路-- -Hibernate检索方式(5种),HQL介绍,实现功能,实现步骤,
    [原创]java WEB学习笔记88:Hibernate学习之路-- -Hibernate检索策略(立即检索,延迟检索,迫切左外连接检索)
  • 原文地址:https://www.cnblogs.com/javastack/p/15596657.html
Copyright © 2011-2022 走看看