其他分享
首页 > 其他分享> > Docker部署Fastdfs分布式文件存储系统

Docker部署Fastdfs分布式文件存储系统

作者:互联网

Docker部署Fastdfs分布式文件存储系统

Fastdfs分布式文件存储系统

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

拉取镜像

docker pull ygqygq2/fastdfs-nginx

部署Tracker和Storage

docker run -d --network=host --name tracker -v /root/fdfs/tracker:/var/fdfs ygqygq2/fastdfs-nginx tracker

docker run -d --network=host --name storage0 -e TRACKER_SERVER=你的IP地址:22122 -v /root/fdfs/storage0:/var/fdfs ygqygq2/fastdfs-nginx storage

开放端口

tracker是22122

storage是23000

nginx是8080

SpringBoot代码示例

maven

<dependency>
	<groupId>com.luhuiguo</groupId>
	<artifactId>fastdfs-spring-boot-starter</artifactId>
	<version>0.2.0</version>
</dependency>

yml

fdfs:
  tracker-list: 你的ip:22122
  pool:
    test-while-idle: true
    test-on-return: false
    test-on-borrow: false
    time-between-eviction-runs-millis: 60000

code

import cn.hutool.core.io.FileUtil;
import com.luhuiguo.fastdfs.domain.MetaData;
import com.luhuiguo.fastdfs.domain.StorePath;
import com.luhuiguo.fastdfs.service.FastFileStorageClient;
import com.yy.treasurehouse.dto.Result;
import com.yy.treasurehouse.web.exception.GlobalException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;

@RestController
@RequestMapping(value = "/other/fdfs")
@Api(tags = "fastfdfs相关")
public class FdfsController {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @GetMapping(value = "/meta")
    @ApiOperation(value = "获取文件meta信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "group", value = "文件组", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "path", value = "文件路径", required = true, paramType = "query", dataType = "String"),
    })
    public Result<Set<MetaData>> meta(@RequestParam String group, @RequestParam String path) {
        Set<MetaData> metaData = fastFileStorageClient.getMetadata(group, path);
        return new Result<>().ok(metaData);
    }

    @GetMapping(value = "/download")
    @ApiOperation(value = "下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "group", value = "文件组", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "path", value = "文件路径", required = true, paramType = "query", dataType = "String"),
    })
    public void download(@ApiIgnore HttpServletResponse response, @RequestParam String group, @RequestParam String path) {
        byte[] bytes = fastFileStorageClient.downloadFile(group, path);
        try {
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(FileUtil.getName(path), "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setContentLength(bytes.length);
            response.setCharacterEncoding("utf-8");
            response.getOutputStream().write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ServletOutputStream outputStream = response.getOutputStream();
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "group", value = "文件组", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "path", value = "文件路径", required = true, paramType = "query", dataType = "String"),
    })
    public Result delete(@RequestParam String group, @RequestParam String path) {
        fastFileStorageClient.deleteFile(group, path);
        return new Result<>().ok();
    }

    @PostMapping
    @ApiOperation(value = "新增")
    public Result<String> save(MultipartFile multipartFile) {
        String extName = FileUtil.extName(multipartFile.getOriginalFilename());
        try {
            Set<MetaData> metaDataSet = new HashSet<>();
            metaDataSet.add(new MetaData("size", String.valueOf(multipartFile.getSize())));
            metaDataSet.add(new MetaData("extName", extName));
            metaDataSet.add(new MetaData("oldName", multipartFile.getOriginalFilename()));
            StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), extName, metaDataSet);
            return new Result<>().ok(storePath.getFullPath());
        } catch (IOException e) {
            e.printStackTrace();
            throw new GlobalException("文件上传失败");
        }
    }
}

在这里插入图片描述

标签:文件,group,String,存储系统,Fastdfs,value,path,import,Docker
来源: https://blog.csdn.net/qq_37062156/article/details/121735020