SpringBoot整合FastDFS分布式文件系统

2 篇文章 0 订阅
1 篇文章 0 订阅

简介

FastDFS是用c语言编写的一款开源的分布式文件系统,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合中小文件(建议范围:4KB < file_size <500MB),对以文件为载体的在线服务,如相册网站、视频网站等。

架构

在这里插入图片描述

软件

  • SpringBoot2.0,不说了都懂
  • Nginx,提供HTTP服务
  • Docker,一件安装容器,后面的就都不需要了
  • FastDFS,C语言编写的一款开源的分布式文件系统
  • libfastcommon,包含了FastDFS运行所需要的一些基础库
  • Fastdfs-nginx-module,Nginx结合 fastdfs-nginx-module插件去实现http协议

安装

如果一步步安装FastDFS以及其依赖,极其复杂,有可能你捣鼓一上午不一定能配置好!这里建议大家使用Docker一键安装,只需要把存储目录映射出来即可。

docker run --name fastdfs --privileged=true --net=host \
         -e IP=192.168.45.240 -e WEB_PORT=8080 \
         -v /home/fastdfs:/var/local/fdfs \
         -d --restart=always \
         registry.cn-beijing.aliyuncs.com/itstyle/fastdfs:1.0

安装成功以后,会在 /home/fastdfs 目录下生成两个目录文件夹,trackerstorage

这里需要注意几点:

  • 一定要映射宿机存储文件路径
  • IP一定要配置为内网或者公网地址
  • 网络类型一定要设置为 host 类型

安装完成以后,浏览器访问:http://ip:8080 如果显示Nginx欢迎页说明安装配置成功。

整合

接下来我们就要与SpringBoot整合了,pom.xml引入第三方工具类:

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

application.properties 引入配置:

# ===================================
# 分布式文件系统FDFS配置
# ===================================
# 连接超时时间
fdfs.connect-timeout=600
# 读取超时时间
fdfs.so-timeout=1500
# 缩略图的宽高
fdfs.thumb-image.height=150
fdfs.thumb-image.width=150
# tracker服务配置地址列表,替换成自己服务的IP地址,支持多个
fdfs.tracker-list=192.168.1.15:22122
fdfs.pool.jmx-enabled=false

yml语法:

fdfs:
  connect-timeout: 5000
  so-timeout: 1000
  tracker-list:
    - 192.168.45.240:22122

编写上传工具类:

/**
 * @author fuGang
 * @Date 2023年4月7日
 */
@Component
public class FastDfsUtils {

    public static final String DEFAULT_CHARSET = "UTF-8";

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    /**
     * 上传
     * @param file
     * @return
     * @throws IOException
     */
    public StorePath upload(MultipartFile file) throws IOException {
        // 设置文件信息
        Set<MetaData> mataData = new HashSet<>();
        mataData.add(new MetaData("author", "fastdfs"));
        mataData.add(new MetaData("description",file.getOriginalFilename()));
        // 上传
        StorePath storePath = fastFileStorageClient.uploadFile(
                file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()),
                null);
        return storePath;
    }

    /**
     * 删除
     * @param path
     */
    public void delete(String path) {
        fastFileStorageClient.deleteFile(path);
    }

    /**
     * 删除
     * @param group
     * @param path
     */
    public void delete(String group,String path) {
        fastFileStorageClient.deleteFile(group,path);
    }

    /**
     * 文件下载
     * @param path 文件路径,例如:/group1/path=M00/00/00/itstyle.png
     * @param filename 下载的文件命名
     * @return
     */
    public void download(String path, String filename, HttpServletResponse response) throws IOException {
        // 获取文件
        StorePath storePath = StorePath.parseFromUrl(path);
        if (StringUtils.isBlank(filename)) {
            filename = FilenameUtils.getName(storePath.getPath());
        }
        byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
        response.reset();
        response.setContentType("applicatoin/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        ServletOutputStream out = response.getOutputStream();
        out.write(bytes);
        out.close();
    }
}

编写上传接口测试:

@RestController
@RequestMapping("/api/fdfs")
public class FastDfsController {

    @Autowired
    private FastDfsUtils fastdfsUtils;

    @PostMapping(value = "/upload")
    public Result uploadFile(MultipartFile file) {

        String fullPath = "";

        try {
            StorePath path = fastdfsUtils.upload(file);
            fullPath = path.getFullPath();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return Result.ok(fullPath);
    }
}

调用测试:
在这里插入图片描述

注意事项

  • 生产环境中建议集群使用,搭建高可用的云存储服务
  • 如果是本地开发云服务器需要开放 8080、22122 端口,生产环境不建议开启,所有的请求最好走内网。

本文借鉴如下,非常感谢:
SpringBoot 2.0 开发案例之整合FastDFS分布式文件系统

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FastDFS是一个开源的轻量级分布式文件系统,它提供了高性能的文件存储和访问服务。Spring Boot是一个用于构建独立的、生产级别的Spring应用程序的框架。将FastDFS集成Spring Boot中可以实现在Spring应用程序中使用FastDFS提供的文件存储和访问服务。 要实现FastDFS集成Spring Boot,你可以按照以下步骤进行操作: 1. 引入FastDFS的依赖:在Spring Boot项目的pom.xml文件中,添加FastDFS的依赖。你可以在Maven中央仓库中找到FastDFS的相关依赖。 2. 配置FastDFS的连接信息:在Spring Boot的配置文件(application.properties或application.yml)中,配置FastDFS的连接信息,如Tracker服务器的地址、端口号等。 3. 编写FastDFS的客户端:在Spring Boot应用程序中编写FastDFS客户端的代码,用于连接FastDFS服务器,并实现文件的上传、下载、删除等操作。 4. 集成Swagger:如果你想方便地测试和使用FastDFS的功能,你可以集成Swagger,它提供了一个用户友好的API文档和测试界面。 通过以上步骤,你就可以实现FastDFS集成Spring Boot中,从而在Spring Boot应用程序中使用FastDFS的文件存储和访问服务。<span class="em">1</span> #### 引用[.reference_title] - *1* [FastDFS源码封装(SpringBoot集成FastDFS)](https://download.csdn.net/download/fjxcsdn/12051041)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值