SpringBoot整合FastDFS文件系统

全栈开发日记

共 4831字,需浏览 10分钟

 · 2021-06-28

FastDFS文件系统的介绍请自行网上学习,理论上的这里就不写了。


Docker搭建FastDFS服务器:


服务器环境:Linux Ubuntu

没有安装Docker的话请自行搜索安装。

① 拉取镜像:

docker pull delron/fastdfs


② 创建映射目录

fastdfs.zip下载链接: https://stand.lanzoui.com/iJZijqpqiqb

拷贝fastdfs文件夹到/home/u01目录下作为fastdfs的存储目录(注意创建/home/u01目录)。


③ 修改配置信息

修改storage目录下的storage.confclient.conf配置信息

找到如下字段将IP地址改为自己的IP:

注意:两个文件该字段都要修改。

tracker_server=x.x.x.x:22122


④ 创建tracker容器(跟踪服务器容器)

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


如果出现警告信息,并且端口并没有起到作用,可以尝试将host连接方式改为bridge,如果是内网环境下搭建,可以忽略此条。

-d:让容器在后台运行--name:指定容器创建的名称-v:容器跟宿主机之间的挂载目录
⑤ 创建storage容器(存储服务器容器)

docker run -d --restart=always --network=host \
-v /home/u01/fastdfs/storage/data:/var/fdfs/data \
-v /home/u01/fastdfs/storage/thumb:/var/fdfs/thumb \
-v /home/u01/fastdfs/storage/logs:/var/fdfs/logs \
-v /home/u01/fastdfs/storage/conf/storage.conf:/etc/fdfs/storage.conf \
-v /home/u01/fastdfs/storage/conf/client.conf:/etc/fdfs/client.conf \
-e GROUP_NAME=group1 \
--name storage \
delron/fastdfs storage


⑥ 查看监控状态

docker exec -it storage fdfs_monitor /etc/fdfs/client.conf


参数说明:

如出现如下这三个字段,说明服务正常启动。

tracker server is x.x.x.x:22122 --表示Leader Tracker
group count: 1 --表示有1个group
active server count = 2 --活动的storage有2个



通过Nginx访问上传的文件


location /group1/M00 {
 alias /data/storage/data;
}


配置Nginx配置文件:

/group1/M00:存储节点,几个节点就写几个节点;

/data/storage/data:文件路径;


访问:域名/存储地址,如:tworice.cn/存储地址


Boot项目整合FastDFS


① 导入依赖

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


② 创建配置类

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

/**
 * FastDFS文件上传配置类
 * @author 二饭
 * @email 1786705138@qq.com
 */

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfig {
}


③ 配置yml文件

# FastDFS配置
fdfs:
  connect-timeout: 30000   # 连接超时时间
  so-timeout: 20000        # 读取超时时间
  thumb-image:
    width: 60
    height: 60
  tracker-list:          # tracker-list参数,支持多个
    - x.x.x.x:22122


④ 创建工具类

@Service
public class FastDFSClient {
    @Autowired
    private FastFileStorageClient storageClient;

    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */

    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
        return getResAccessUrl(storePath);
    }


    /**
     * 删除文件
     * @param fileUrl 文件访问地址
     * @return
     */

    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            System.out.println(e.getMessage());
            /** TODO 只是测试,所以未使用日志,正式环境请修改打印方式 **/
        }
    }

}


由于代码量比较多,所以这里只放了上传删除两个方法。



点击关注公众号,查看更多内容:


浏览 39
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报