龙空技术网

SpringBoot 2.0 开发案例之整合FastDFS分布式文件系统

Java技术那些事 271

前言:

此时咱们对“dockerfastdfsnginx”都比较关怀,小伙伴们都想要剖析一些“dockerfastdfsnginx”的相关文章。那么小编也在网上网罗了一些关于“dockerfastdfsnginx””的相关内容,希望各位老铁们能喜欢,你们快快来学习一下吧!

原作者:小柒
地址:

前言

最近在做一款图床服务,前面的文章也有提到,之前整合了阿里云 OSS 做图片存储,虽然小批量的存储并不贵,然而再少也是肉,还是有点心疼的。

恰好,双十一搞活动,顺手购买了腾讯云的2C4G3MB的云服务器,不得不说真香!入手六百多,真香,毕竟是3MB的带宽。

不要问我双十一为嘛不买阿里云,因为我等老用户狗都不如,只有续费和满多少减几块的所谓福利了。

简介

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.1.156 -e WEB_PORT=8080 \ -v /home/fastdfs:/var/local/fdfs \ -d --restart=always \ registry.cn-beijing.aliyuncs.com/itstyle/fastdfs:1.0

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

这里需要注意几点:

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

安装完成以后,浏览器访问: 如果显示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=150fdfs.thumb-image.width=150# tracker服务配置地址列表,替换成自己服务的IP地址,支持多个fdfs.tracker-list=192.168.1.15:22122fdfs.pool.jmx-enabled=false

最后写个工具类,基本搞定,坐等上传:

@Componentpublic 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(); }}

图床预览

注意事项

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

标签: #dockerfastdfsnginx