ITPub博客

首页 > 数据库 > 数据库开发技术 > SpringBoot集成FastDFS

SpringBoot集成FastDFS

原创 数据库开发技术 作者:HelloTech技术派 时间:2017-12-12 08:00:00 0 删除 编辑

FastDFS是什么

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

FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。

SpringBoot集成FastDFS

假如已经有FastDFS服务器,怎么与FastDFS集成?

  • 新建服务(必须是Springboot项目)添加依赖 fastdfs-client

  1. <dependency>

  2.    <groupId>com.github.tobato</groupId>

  3.    <artifactId>fastdfs-client</artifactId>

  4.    <version>1.25.2-RELEASE</version>

  5. </dependency>

  6. <!-- Test -->

  7. <dependency>

  8.    <groupId>org.springframework.boot</groupId>

  9.    <artifactId>spring-boot-starter-web</artifactId>

  10. </dependency>

  11. <!-- lombok -->

  12. <dependency>

  13.    <groupId>org.projectlombok</groupId>

  14.    <artifactId>lombok</artifactId>

  15.    <version>1.16.14</version>

  16. </dependency>

  • 将FastDFS配置引入项目

仅需要在启动类上增加注解 @EnableSwagger2,启动该注解使得用在Controller中的Swagger注解生效

  1. @Import(FdfsClientConfig.class)

  2. @SpringBootApplication

  3. public class Server {

  4.    public static void main(String[] args) {

  5.        SpringApplication.run(Server.class, args);

  6.    }

  7. }

  • 增加FastDFS配置

  1. fdfs.soTimeout=1500

  2. fdfs.connectTimeout=600

  3. #缩略图生成参数

  4. fdfs.thumbImage.width=150

  5. fdfs.thumbImage.height=150

  6. #TrackerList参数,支持多个

  7. fdfs.trackerList[0]=10.0.0.112:22122

  8. #fdfs.trackerList[1]=10.0.0.112:22122

  • 将FastDFS制作成SpringBoot组件

在目前的项目当中将FastDFS主要用于图片存储,基于FastFileStorageClient接口和SpringMVC提供的MultipartFile接口封装了一个简单的工具类

  1. package com.gemantic.fs;

  2. import com.github.tobato.fastdfs.domain.StorePath;

  3. import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;

  4. import com.github.tobato.fastdfs.service.FastFileStorageClient;

  5. import lombok.extern.slf4j.Slf4j;

  6. import org.apache.commons.io.FilenameUtils;

  7. import org.apache.commons.lang3.StringUtils;

  8. import org.springframework.beans.factory.annotation.Autowired;

  9. import org.springframework.stereotype.Component;

  10. import org.springframework.web.multipart.MultipartFile;

  11. import java.io.ByteArrayInputStream;

  12. import java.io.IOException;

  13. import java.nio.charset.Charset;

  14. /**

  15. * @author Yezhiwei

  16. * @date 17/12/11

  17. */

  18. @Slf4j

  19. @Component

  20. public class FastDFSClientWrapper {

  21.    @Autowired

  22.    private FastFileStorageClient storageClient;

  23.    /**

  24.     * 上传文件

  25.     *

  26.     * @param file 文件对象

  27.     * @return 文件访问地址

  28.     * @throws IOException

  29.     */

  30.    public String uploadFile(MultipartFile file) throws IOException {

  31.        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);

  32.        return getAccessUrl(storePath);

  33.    }

  34.    /**

  35.     * 将一段字符串生成一个文件上传

  36.     *

  37.     * @param content       文件内容

  38.     * @param fileExtension

  39.     * @return

  40.     */

  41.    public String uploadFile(String content, String fileExtension) {

  42.        byte[] buff = content.getBytes(Charset.forName("UTF-8"));

  43.        ByteArrayInputStream stream = new ByteArrayInputStream(buff);

  44.        StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);

  45.        return getAccessUrl(storePath);

  46.    }

  47.    private String getAccessUrl(StorePath storePath) {

  48.        String fileUrl = storePath.getFullPath();

  49.        return fileUrl;

  50.    }

  51.    /**

  52.     * 删除文件

  53.     *

  54.     * @param fileUrl 文件访问地址

  55.     * @return

  56.     */

  57.    public void deleteFile(String fileUrl) {

  58.        if (StringUtils.isEmpty(fileUrl)) {

  59.            return;

  60.        }

  61.        try {

  62.            StorePath storePath = StorePath.praseFromUrl(fileUrl);

  63.            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());

  64.        } catch (FdfsUnsupportStorePathException e) {

  65.            log.warn(e.getMessage());

  66.        }

  67.    }

  68. }

> 客户端主要包括以下接口: TrackerClient - TrackerServer接口 GenerateStorageClient - 一般文件存储接口 (StorageServer接口) FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口) AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)

注:可根据自身的业务需求,将其它接口也添加到组件类中即可。

  • 测试

  1. @RunWith(SpringJUnit4ClassRunner.class)

  2. @SpringBootTest

  3. @Slf4j

  4. public class FastDFSClientTest {

  5.    @Autowired

  6.    private FastDFSClientWrapper fastDFSClientWrapper;

  7.    @Test

  8.    public void updateFileByContent() {

  9.        String url = fastDFSClientWrapper.uploadFile("This is test", "png");

  10.        log.info("url {}", url);

  11.        //输出/group112/M00/BB/6D/CgAAcFoukU2sCPUsAAAADDzy24o518.png

  12.    }

  13.    @Test

  14.    public void deleteFile() {

  15.        fastDFSClientWrapper.deleteFile("http://10.0.0.112/group112/M00/BB/6D/CgAAcFoukU2sCPUsAAAADDzy24o518.png");

  16.    }

  17. }


原文作者:HelloTech技术派;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2017-12-05

  • 博文量
    181
  • 访问量
    73730