springboot 整合分布式文件存储服务器fastDFS并使用docker部署fastDFS

大家好今儿给大家带来的是springboot 整合分布式文件存储服务器fastDFS并使用docker部署fastDFS

1.fastDFS简单介绍(个人理解)

1.FastDFS是一个开源的轻量级分布式文件存储系统,可以供我们去完成上传下载。
2.在FastDFS中有两部分组成一个叫tracker一个叫storage。

trackerstorage
tracker 是专门去管理storage的,在FastDFS中所有的访问都会先经过tracker ,并由tracker 去找有哪些 storage压力小或者比较空闲那么tracker 就会返回一个storage地址供我们去上传下载storage的职责其实就是保存上传的数据以及对数据进行下载

2.使用docker安装fastDFS

1.查看 fastdfs 都有哪些版本

docker search fastdfs

2.我这里选择的是delron/fastdfs版本(其他的版本也是可以的不要用太新的版本)

docker pull delron/fastdfs

3.安装完毕后可以在docekr镜像库中看到
在这里插入图片描述
4.首先启动tracker

docker run -d --network=host --name tracker -v /home/fastdfs/docker/fastdfs/tracker:/var/fdfs delron/fastdfs tracker

5.启动storage
注意:
1.TRACKER_SERVER=ip:22122。ip要自己改成当前服务器的ip

2.GROUP_NAME=group1。storage组名称可以自己随便写

docker run -d --network=host --name storage -e TRACKER_SERVER=ip:22122 -v /home/fastdfs/docker/fastdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage

注意:全部启动成功后fastdfs默认的端口有三个8888,23000,22122,大家需要在服务器中放行这三个端口
8888:是默认的nginx代理端口 delron/fastdfs这个版本包含有nginx
23000:storage服务端口
22122:tracker服务端口

3.springboot整合fastDFS

1.我的boot版本

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>

2.fastDFS坐标

<!--分布式文件存储  fastDFS-->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.2</version>
        </dependency>
        <!--上传下载 间接包-->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>

3.需要在boot启动类加上两个注解

//加载fastDFS
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
@Import(FdfsClientConfig.class)  

4.在yml中添加连接fastdfs的地址

#fastDFS
注意这个trackerList:如果是集群tracker的话以逗号分隔开就行
fdfs:
  connect-timeout: 600
  so-timeout: 1500
  trackerList: 服务器tracker的ip:22122
  thumb-image:
    width: 150
    height: 150
  pool:
    max-total: 200

5.最后就是代码部分啦直接上代码

@RestController
@RequestMapping("/file")
@Api(value = "fastDFs分布式文件存储 -- 文件上传下载",description = "fastDFs分布式文件存储 -- 文件上传下载")
@CrossOrigin//跨域的意思
public class FileController {

//FastFileStorageClient 直接注入就能用 fastdsf自带的
    @Resource
    private FastFileStorageClient fastFileStorageClient;

    //fastDFS storage存储节点路径  xxxxxx:服务器ip
    private  final String  FASTDFSSERVERIMAGE = "http://xxxxxx:8888/";

    /**
     * 文件上传
     * @return result
     */
    @ApiOperation("上传")
    @Async("asyncServiceExecutor")
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public HashMap<String, String> upload(@RequestPart("file") MultipartFile file){
        HashMap<String,String> result = new HashMap<>();
        try {

//这里msg返回的是上传后的图片存储位置getFullPath()获取文件位置            
            result.put("msg",FASTDFSSERVERIMAGE+fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(),
                            FilenameUtils.getExtension(file.getOriginalFilename()),null).getFullPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 文件删除
     * @param path
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation("删除")
    public HashMap<String, Object> delete(@RequestParam String path) {
        HashMap<String,Object> result = new HashMap<>();
        fastFileStorageClient.deleteFile(path);
        result.put("msg","大哥啦~~~!!,删除成功!");
        return result;
    }


    /**
     * 文件下载
     * @param url 路径
     * @return
     */
    @RequestMapping(value = "/download",method = RequestMethod.GET)
    @ApiOperation("下载")
    public void downLoad(@RequestParam String url, HttpServletResponse response) throws IOException {
        //文件后缀
        String substring = url.substring(url.lastIndexOf(".") + 1);
        byte[] bytes = fastFileStorageClient.downloadFile(url.substring(0, url.indexOf("/")), url.substring(url.indexOf("/") + 1), new DownloadByteArray());
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(UUIDUtils.getUUID() +"."+substring, "UTF-8"));
        // 写出
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.write(bytes, outputStream);
    }

4.测试

1.上传
在这里插入图片描述
在这里插入图片描述

浏览器访问
在这里插入图片描述

5.总结

FastDFS是一个开源的分布式文件存储系统,有两部分组成tracker(有负载均衡的作用)负责管理storage(可集群),如有问题欢迎留言,今儿就到这里吧。下期见。

在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值