美文网首页
docker 搭建fastdfs和springboot 接口编写

docker 搭建fastdfs和springboot 接口编写

作者: 大师兄爱上芭蕉扇 | 来源:发表于2018-11-21 19:39 被阅读181次

    一、fastdfs搭建

    1.从git上下载 fastdfs-nginx

    ### thanks  gituser 545314690
    git clone https://github.com/545314690/fastdfs-nginx.git
    

    2. 修改Conf

    • storage.conf
    ###can not use 127.0.0.1
    tracker_server=host-ip:22122
    
    • mod_fastdfs.conf
    tracker_server=127.0.0.1:22122
    
    • nginx.conf
    如果用redis记录的token,可以配置如下,没有此方面需求,自行忽略
    ## if need check token from redis 
    upstream redisbackend {
      server    redis-server-ip:port;
      keepalive 1024;
    }
    
    • [x] 启动 https(根据需求选择是否启动)

    启用 https 当然要申请https证书,放置在容器中的/etc/cert/目录,阿里云免费证书申请方案请参考:https://www.jianshu.com/p/4f3bccd29bd9

    如果启用https协议,请打开下面ssl 配置注释

    ### ssl 配置 start
    # ssl on;
    # ssl_certificate   /etc/cert/214927684720376.pem;
    # ssl_certificate_key  /etc/cert/214927684720376.key;
    # ssl_session_timeout 5m;
    # ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    # ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    # ssl_prefer_server_ciphers on;
    ### ssl 配置 end
    
    • other conf files if you needed

    3. Build 自己的docker镜像

     ## you can use your image name to replace name 'fastdfs-nginx'
    docker build -t fastdfs-nginx .
    

    4. 网络配置

    根据需求配置,这里可以不配置,具体bridge和Overlay网络区别请参考:

    https://www.cnblogs.com/atuotuo/p/6926390.html

    ## you can create your network for this server, like:
    docker network create --driver bridge --subnet 192.168.1.108/20 network0
    

    5.启动docker容器

    • 第一种方式(测试通过)--建议采用
    ## should use host network (test pass)
    docker run -itd \
      --name fastdfs-nginx \
      --network=host \
      -v /etc/localtime:/etc/localtime:ro \
      -v /var/log/fdfs/:/data/fdfs/logs/ \
      -v /data/fdfs/data/:/data/fdfs/data/ \
      -v /var/log/nginx/:/var/log/nginx/ \
      fastdfs-nginx \
      sh -c "/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart && /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart && /usr/sbin/nginx -g 'daemon off;'"
    
    • 第二种方式
    ## if you want to use your network and use ip 192.168.16.6
    ## in this case, you should update some conf to make fdfs work
    ## like set tracker_server in storage.conf to 192.168.16.6
    ## the app which use fdfs-client-java to upload file should in the same network (not test yet)
    docker run -itd \
      --name fastdfs-nginx \
      --network=network0 --ip=192.168.16.6 \
      -p 22122:22122 \
      -p 23000:23000 \
      -p 24001:24001 \
      -p 24002:24002 \
      -p 11411:11411 \
      -v /etc/localtime:/etc/localtime:ro \
      -v /var/log/fdfs/:/data/fdfs/logs/ \
      -v /data/fdfs/data/:/data/fdfs/data/ \
      -v /var/log/nginx/:/var/log/nginx/ \
      fastdfs-nginx \
      sh -c "/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart && /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart && /usr/sbin/nginx -g 'daemon off;'"
    

    6.API测试

    • 如果采用了token用下面url测试是否安装成功
    ## fetch file from server, if you need check token
    http://ip:24001/group1/M00/00/00/xxxxxx?tk=zzz&&typ=yyy
    
    • 如果无token用下面url测试是否安装成功
    ## fetch file from server directly
    http://ip:24002/group1/M00/00/00/xxxxxx.yyy
    

    二、fastdfs上传接口编写

    1.引入maven

    <!-- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client -->
            <dependency>
                <groupId>com.github.tobato</groupId>
                <artifactId>fastdfs-client</artifactId>
                <version>1.26.2</version>
            </dependency>
    

    2.上传接口

    package com.topcom.cms.spider.utils.fastdfs;
    
    import com.github.tobato.fastdfs.FdfsClientConfig;
    import com.github.tobato.fastdfs.domain.StorePath;
    import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
    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.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableMBeanExport;
    import org.springframework.context.annotation.Import;
    import org.springframework.jmx.support.RegistrationPolicy;
    import org.springframework.mock.web.MockMultipartFile;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.*;
    import java.nio.charset.Charset;
    
    /**
     * FastDFS文件上传下载包装类
     */
    @Configuration
    @Import(FdfsClientConfig.class)
    // 解决jmx重复注册bean的问题
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
    public class FastDFSClientWrapper {
    
        private final Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class);
    
        @Value("${fdfs.nginx-host}")
        private String nginxHost;
    
        @Autowired
        private FastFileStorageClient storageClient;
    
        /**
         * 上传文件
         *
         * @param file 文件对象
         * @return 文件访问地址
         * @throws IOException
         */
        public String uploadFile(MultipartFile file) throws IOException {
            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                    FilenameUtils.getExtension(file.getOriginalFilename()), null);
            return getResAccessUrl(storePath);
        }
    
        /**
         * 上传文件 File
         *
         * @param fileName 文件名
         * @return 文件访问地址
         * @throws IOException
         */
        public String uploadFile(String fileName) throws IOException {
            MultipartFile multipartFile = file2MultipartFile(fileName);
            if (null != multipartFile) {
                return uploadFile(multipartFile);
            }
            return null;
        }
    
        /**
         *根据文件名把file转化为MultipartFile
         * @param pathName
         * @return
         */
        public MultipartFile file2MultipartFile(String pathName) {
            File file = new File(pathName);
            try {
                FileInputStream inputStream = new FileInputStream(file);
                MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), "", inputStream);
                return multipartFile;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 将一段字符串生成一个文件上传
         *
         * @param content       文件内容
         * @param fileExtension
         * @return
         */
        public String uploadFile(String content, String fileExtension) {
            byte[] buff = content.getBytes(Charset.forName("UTF-8"));
            ByteArrayInputStream stream = new ByteArrayInputStream(buff);
            StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
            return getResAccessUrl(storePath);
        }
    
        // 封装图片完整URL地址
        private String getResAccessUrl(StorePath storePath) {
            String fileUrl = storePath.getFullPath();
            return nginxHost + '/' + fileUrl;
        }
    
        /**
         * 传图片并同时生成一个缩略图 "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP"
         *
         * @param file 文件对象
         * @return 文件访问地址
         * @throws IOException
         */
        public String uploadImageAndCrtThumbImage(MultipartFile file) throws IOException {
            StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
                    FilenameUtils.getExtension(file.getOriginalFilename()), null);
            return getResAccessUrl(storePath);
        }
    
        /**
         * 删除文件
         *
         * @param fileUrl 文件访问地址
         * @return
         */
        public void deleteFile(String fileUrl) {
            if (StringUtils.isEmpty(fileUrl)) {
                return;
            }
            try {
                StorePath storePath = StorePath.praseFromUrl(fileUrl);
                storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
            } catch (FdfsUnsupportStorePathException e) {
                logger.warn(e.getMessage());
            }
        }
    }
    

    3.spring-boot配置文件修改

    • application.properties
    ##fastdfs
    fdfs.tracker-list[0]=192.168.1.108:22122
    fdfs.nginx-host=http://192.168.1.108:24002
    #单个数据大小,不配置默认单个文件是1M,会报错
    spring.servlet.multipart.max-file-size= 10Mb
    #总数据大小
    spring.servlet.multipart.max-request-size=100Mb
    

    参考:

    https://github.com/545314690/fastdfs-nginx
    https://www.jianshu.com/p/4f3bccd29bd9

    相关文章

      网友评论

          本文标题:docker 搭建fastdfs和springboot 接口编写

          本文链接:https://www.haomeiwen.com/subject/bzacqqtx.html