当前位置:   article > 正文

docker部署FastDFS整合Springboot_docker fastdfs

docker fastdfs

1、FastDFS是什么?

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。
FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务

2、搭建docker环境

参考文章:

https://blog.csdn.net/JiuMen3520/article/details/134336301

3、部署fastdfs

1、搜索fastdfs镜像
docker search fastdfs
  • 1
  • 2
2、下载镜像
方法一(直接拉取):
docker pull morunchang/fastdfs

方法二(百度网盘下载):
链接:https://pan.baidu.com/s/1pAS_wOFJrpk-BlNs3lZYfQ?pwd=bfmu 
提取码:bfmu

若选择方法二,执行docker导入镜像命令:
docker load -i fastdfs_latest.tar
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
3、启动tracker
docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh
  • 1
  • 2
4.启动storage
(1)宿主机创建目录(用于磁盘挂载)
mkdir /data/fastDFS/data
(2)执行启动命令
docker run -v /data/fastDFS/data:/data/fast_data/data/00/00 -d --name storage --net=host -e TRACKER_IP=服务器访问IP:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
  • 1
  • 2
  • 3
  • 4
  • 5

启动效果:
在这里插入图片描述
修改nginx配置

1.进入storage
    docker exec -it storage容器id /bin/bash
 
2.找到nginx配置
    whereis nginx --->会看到目录在/etc/nginx/下
 
3.修改nginx配置
    vi /etc/nginx/conf/nginx.conf
 
4.修改监听端口(listen)和访问地址(server_name),我这将端口修改为了80,访问地址为公网ip
 
5.退出容器
    exit 
 
6.重启 storage
    docker restart storage容器id
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4、整合springboot

(1)新建springboot项目
(2)加入maven依赖

   <!-- FastDFS -->
   <dependency>
       <groupId>com.github.tobato</groupId>
       <artifactId>fastdfs-client</artifactId>
   </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述
(3)修改配置文件yml

# FastDFS配置
fdfs:
  domain: http://服务器IP
  soTimeout: 3000
  connectTimeout: 2000
  trackerList: 服务器IP:22122
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述
(3)创建controller类

package com.pie.file.controller;

import com.pie.common.core.domain.R;
import com.pie.common.core.utils.file.FileUtils;
import com.pie.file.service.ISysFileService;
import com.pie.system.api.domain.SysFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件请求处理
 * 
 * @author p
 */
@RestController
public class SysFileController
{
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileService sysFileService;

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file)
    {
        try
        {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            return R.ok(sysFile);
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

(4)创建service接口

package com.pie.file.service;

import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传接口
 * 
 * @author p
 */
public interface ISysFileService
{
    /**
     * 文件上传接口
     * 
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    public String uploadFile(MultipartFile file) throws Exception;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

(5)创建serviceImpl实现类

package com.pie.file.service;

import java.io.InputStream;
import com.alibaba.nacos.common.utils.IoUtils;
import com.pie.common.core.utils.file.FileTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

/**
 * FastDFS 文件存储
 *
 * @author p
 */
@Primary
@Service
public class FastDfsSysFileServiceImpl implements ISysFileService
{
    /**
     * 域名或本机访问地址
     */
    @Value("${fdfs.domain}")
    public String domain;

    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * FastDfs文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile file) throws Exception
    {
        InputStream inputStream = file.getInputStream();
        StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(),
                FileTypeUtils.getExtension(file), null);
        IoUtils.closeQuietly(inputStream);
        return domain + "/" + storePath.getFullPath();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

5、接口测试

使用postman访问接口测试

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

参考文章

参考文章

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/986556
推荐阅读
相关标签
  

闽ICP备14008679号