使用SpringBoot集成FastDFS
这篇文章我们介绍如何使用 Spring Boot 将文件上传到分布式文件系统 FastDFS 中。
1、FastDFS
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问
(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网
站、视频网站等等。
FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,并注重高可用、高性能等指标,
使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务。
FastDFS 架构包括 Tracker server 和 Storage server。客户端请求 Tracker server 进行文件上传、下载,通过
Tracker server 调度最终由 Storage server 完成文件上传和下载。
Tracker server 作用是负载均衡和调度,通过 Tracker server 在文件上传时可以根据一些策略找到Storage server
提供文件上传服务。可以将 tracker 称为追踪服务器或调度服务器。Storage server 作用是文件存储,客户端上传
的文件最终存储在 Storage 服务器上,Storageserver 没有实现自己的文件系统而是利用操作系统的文件系统来管
理文件。可以将storage称为存储服务器。
上传流程
客户端上传文件后存储服务器将文件 ID 返回给客户端,此文件 ID 用于以后访问该文件的索引信息。文件索引信
息包括:组名,虚拟磁盘路径,数据两级目录,文件名。
组名:文件上传后所在的 storage 组名称,在文件上传成功后有storage 服务器返回,需要客户端自行保存。
虚拟磁盘路径:storage 配置的虚拟路径,与磁盘选项store_path*对应。如果配置了
store_path0 则是 M00,如果配置了 store_path1 则是 M01,以此类推。
数据两级目录:storage 服务器在每个虚拟磁盘路径下创建的两级目录,用于存储数据文件。
文件名:与文件上传时不同。是由存储服务器根据特定信息生成,文件名包含:源存储服务器 IP 地址、文件创建
时间戳、文件大小、随机数和文件拓展名等信息。
2、docker 安装 FastDFS
2.1 查找Docker Hub上的fastdfs镜像
$ docker search fastdfs
2.2 拉取镜像
# 拉取最新版本
$ docker pull delron/fastdfs
2.3 查看镜像
$ docker images
2.4 使用docker镜像构建tracker容器
tracker容器跟踪服务器,起到调度的作用。
$ docker run -dti --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs -v /etc/localtime:/etc/localtime delron/fastdfs tracker
2.5 使用docker镜像构建storage容器
storage容器存储服务器,提供容量和备份服务。
$ docker run -dti --network=host --name storage -e TRACKER_SERVER=192.168.56.1:22122 -v /var/fdfs/storage:/var/fdfs -v /etc/localtime:/etc/localtime delron/fastdfs storage
TRACKER_SERVER=本机的ip地址:22122,本机ip地址不要使用127.0.0.1
进入storage容器,到storage的配置文件中配置http访问的端口,配置文件在/etc/fdfs
目录下的
storage.conf
。
默认端口是8888,也可以不进行更改。
如果重启后无法启动的话,可能是报下面错误了:
tail: cannot open '/var/fdfs/logs/storaged.log' for reading: No such file or directory
手动创建 vi /var/fdfs/logs/storaged.log
文件即可
2.6 配置nginx
进入storage,配置nginx,在/usr/local/nginx
目录下,修改nginx.conf
文件,默认配置不修改也可以
2.7 测试上传文件
使用web模块进行文件的上传,将文件上传至FastDFS文件系统
将一张照片(test.png)放置在/var/fdfs/storage
目录下:
进入storage容器,进入/var/fdfs
目录,运行下面命令:
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.png
此时将该图片已上传至文件系统,并在执行该语句后返回图片存储的uri:
通过url访问 http://192.168.56.1:8888/group1/M00/00/00/CqACD1z7SEuAXrIqAA1eBLGVLow034.png
,即
可查看到图片。
2.8 开放端口
$ firewall-cmd --zone=public --permanent --add-port=8888/tcp
$ firewall-cmd --zone=public --permanent --add-port=22122/tcp
$ firewall-cmd --zone=public --permanent --add-port=23000/tcp
重启防火墙
$ systemctl restart firewalld
2.9 开启启动容器
$ docker update --restart=always tracker
$ docker update --restart=always storage
2.10 常见问题
storage 无法启动
运行 docker container start storage
无法启动,进行如下操作即可:
可以删除/var/fdfs/storage/data
目录下的fdfs_storaged.pid
文件,然后重新运行storage。
4、SpringBoot整合FastDFS
4.1 pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</version></parent><groupId>com.example</groupId><artifactId>spring-boot-fastdfs</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-boot-fastdfs</name><description>spring-boot-fastDFS</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><dependency><groupId>org.csource</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27-SNAPSHOT</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
加入了fastdfs-client-java
包,用来调用 FastDFS 相关的 API。
4.2 配置文件
resources 目录下添加 fdfs_client.conf
文件
connect_timeout = 60
network_timeout = 60
charset = UTF-8
http.tracker_http_port = 8080
tracker_server = 192.168.79.113:22122
-
connect_timeout
:连接超时时间,单位为秒。 -
network_timeout
:通信超时时间,单位为秒。发送或接收数据时。假设在超时时间后还不能发送或接收数据,则本次网络通信失败。
-
charset
: 字符集。 -
http.tracker_http_port
:tracker的http端口。 -
tracker_server
: tracker服务器IP和端口设置。
详细内容参考:[fastdfs-client-java] https://github.com/happyfish100/fastdfs-client-java
resources 目录下添加 application.properties
文件
# max-file-size是单个文件大小
spring.servlet.multipart.max-file-size=10MB
# max-request-size是设置总上传的数据大小
spring.servlet.multipart.max-request-size=10MB
4.3 封装 FastDFS 上传工具类
封装FastDFSFile
,文件基础信息包括文件名、内容、文件类型、作者等。
package com.example.fastdfs;/*** @author zhangshixing*/
public class FastDFSFile {/*** 文件名字*/private String name;/*** 文件内容*/private byte[] content;/*** 文件扩展名*/private String ext;/*** 文件MD5摘要值*/private String md5;/*** 文件创建作者*/private String author;public FastDFSFile(String name, byte[] content, String ext, String height,String width, String author) {super();this.name = name;this.content = content;this.ext = ext;this.author = author;}public FastDFSFile(String name, byte[] content, String ext) {super();this.name = name;this.content = content;this.ext = ext;}public String getName() {return name;}public void setName(String name) {this.name = name;}public byte[] getContent() {return content;}public void setContent(byte[] content) {this.content = content;}public String getExt() {return ext;}public void setExt(String ext) {this.ext = ext;}public String getMd5() {return md5;}public void setMd5(String md5) {this.md5 = md5;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}
}
封装 FastDFSClient
类,包含常用的上传、下载、删除等方法。
首先在类加载的时候读取相应的配置信息,并进行初始化。
package com.example.fastdfs;import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;/*** @author zhangshixing*/
public class FastDFSClient {private static Logger logger = LoggerFactory.getLogger(FastDFSClient.class);/**** 初始化tracker信息*/static {try {String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();ClientGlobal.init(filePath);} catch (Exception e) {logger.error("FastDFS Client Init Fail!", e);}}/***** 文件上传* @param file : 要上传的文件信息封装->FastDFSFile* @return String[]* 1:文件上传所存储的组名* 2:文件存储路径*/public static String[] upload(FastDFSFile file) {logger.info("File Name: " + file.getName() + "File Length:" + file.getContent().length);NameValuePair[] meta_list = new NameValuePair[1];meta_list[0] = new NameValuePair("author", file.getAuthor());long startTime = System.currentTimeMillis();String[] uploadResults = null;StorageClient storageClient = null;try {storageClient = getTrackerClient();uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);} catch (IOException e) {logger.error("IO Exception when uploadind the file:" + file.getName(), e);} catch (Exception e) {logger.error("Non IO Exception when uploadind the file:" + file.getName(), e);}logger.info("upload_file time used:" + (System.currentTimeMillis() - startTime) + " ms");if (uploadResults == null && storageClient != null) {logger.error("upload file fail, error code:" + storageClient.getErrorCode());}String groupName = uploadResults[0];String remoteFileName = uploadResults[1];logger.info("upload file successfully!!!" + "group_name:" + groupName + ", remoteFileName:" + " " + remoteFileName);return uploadResults;}/**** 获取文件信息* @param groupName:组名* @param remoteFileName:文件存储完整名*/public static FileInfo getFile(String groupName, String remoteFileName) {try {StorageClient storageClient = getTrackerClient();return storageClient.get_file_info(groupName, remoteFileName);} catch (IOException e) {logger.error("IO Exception: Get File from Fast DFS failed", e);} catch (Exception e) {logger.error("Non IO Exception: Get File from Fast DFS failed", e);}return null;}/**** 文件下载* @param groupName:组名* @param remoteFileName:文件存储完整名* @return*/public static InputStream downFile(String groupName, String remoteFileName) {try {StorageClient storageClient = getTrackerClient();byte[] fileByte = storageClient.download_file(groupName, remoteFileName);InputStream ins = new ByteArrayInputStream(fileByte);return ins;} catch (IOException e) {logger.error("IO Exception: Get File from Fast DFS failed", e);} catch (Exception e) {logger.error("Non IO Exception: Get File from Fast DFS failed", e);}return null;}/**** 文件删除实现* @param groupName:组名* @param remoteFileName:文件存储完整名*/public static void deleteFile(String groupName, String remoteFileName)throws Exception {StorageClient storageClient = getTrackerClient();int i = storageClient.delete_file(groupName, remoteFileName);logger.info("delete file successfully!!!" + i);}/**** 获取组信息* @param groupName :组名*/public static StorageServer[] getStoreStorages(String groupName)throws IOException {TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();return trackerClient.getStoreStorages(trackerServer, groupName);}/**** 根据文件组名和文件存储路径获取Storage服务的IP、端口信息* @param groupName :组名* @param remoteFileName :文件存储完整名*/public static ServerInfo[] getFetchStorages(String groupName,String remoteFileName) throws IOException {TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);}/**** 获取Tracker服务地址*/public static String getTrackerUrl() throws IOException {return "http://" + getTrackerServer().getInetSocketAddress().getHostString() + ":" + ClientGlobal.getG_tracker_http_port() + "/";}/**** 获取StorageClient* @return* @throws Exception*/private static StorageClient getTrackerClient() throws IOException {TrackerServer trackerServer = getTrackerServer();StorageClient storageClient = new StorageClient(trackerServer, null);return storageClient;}/**** 获取TrackerServer*/private static TrackerServer getTrackerServer() throws IOException {TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();return trackerServer;}
}
使用 FastDFS 提供的客户端 storageClient 来进行文件上传,最后将上传结果返回。
根据 groupName 和文件名获取文件信息。
使用 FastDFS 时,直接调用 FastDFSClient 对应的方法即可。
4.4 编写上传控制类
从 MultipartFile 中读取文件信息,然后使用 FastDFSClient 将文件上传到 FastDFS 集群中。
package com.example.controller;import com.example.fastdfs.FastDFSClient;
import com.example.fastdfs.FastDFSFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;import java.io.IOException;
import java.io.InputStream;@Controller
public class UploadController {private static Logger logger = LoggerFactory.getLogger(UploadController.class);@GetMapping("/")public String index() {return "upload";}@PostMapping("/upload")public String singleFileUpload(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes) {if (file.isEmpty()) {redirectAttributes.addFlashAttribute("message", "Please select a file to upload");return "redirect:uploadStatus";}try {String path = saveFile(file);redirectAttributes.addFlashAttribute("message","You successfully uploaded '" + file.getOriginalFilename() + "'");redirectAttributes.addFlashAttribute("path","file path url '" + path + "'");} catch (Exception e) {logger.error("upload file failed", e);}return "redirect:/uploadStatus";}@GetMapping("/uploadStatus")public String uploadStatus() {return "uploadStatus";}/*** @param multipartFile* @return* @throws IOException*/public String saveFile(MultipartFile multipartFile) throws IOException {String[] fileAbsolutePath = {};String fileName = multipartFile.getOriginalFilename();String ext = fileName.substring(fileName.lastIndexOf(".") + 1);byte[] file_buff = null;InputStream inputStream = multipartFile.getInputStream();if (inputStream != null) {int len1 = inputStream.available();file_buff = new byte[len1];inputStream.read(file_buff);}inputStream.close();FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);try {fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs} catch (Exception e) {logger.error("upload file Exception!", e);}if (fileAbsolutePath == null) {logger.error("upload file failed,please upload again!");}String path = FastDFSClient.getTrackerUrl() + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];return path;}
}
请求控制,调用上面方法saveFile()
。
package com.example.controller;import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;@ControllerAdvice
public class GlobalExceptionHandler {//https://jira.spring.io/browse/SPR-14651//4.3.5 supports RedirectAttributes redirectAttributes@ExceptionHandler(MultipartException.class)public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());return "redirect:/uploadStatus";}
}
4.5 上传页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body><h1>Spring Boot file upload example</h1><form method="POST" action="/upload" enctype="multipart/form-data"><input type="file" name="file" /><br/><br/><input type="submit" value="Submit" />
</form></body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body><h1>Spring Boot - Upload Status</h1><div th:if="${message}"><h2 th:text="${message}"/>
</div><div th:if="${path}"><h2 th:text="${path}"/>
</div></body>
</html>
4.6 启动类
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
上传成功之后,将文件的路径展示到页面,效果图如下:
在浏览器中访问此Url,可以看到成功通过FastDFS展示:
4.7 测试类
package com.example;import org.csource.fastdfs.*;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.InetSocketAddress;/*** @author zhangshixing*/
public class FastdfsClientTest {/*** 文件上传** @throws Exception*/@Testpublic void upload() throws Exception {// 加载全局的配置文件String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();ClientGlobal.init(filePath);//创建TrackerClient客户端对象TrackerClient trackerClient = new TrackerClient();//通过TrackerClient对象获取TrackerServer信息TrackerServer trackerServer = trackerClient.getConnection();//获取StorageClient对象StorageClient storageClient = new StorageClient(trackerServer, null);//执行文件上传String[] jpgs = storageClient.upload_file("C:\\Users\\Administrator\\Pictures\\5b13cd6cN8e12d4aa.jpg", "jpg", null);for (String jpg : jpgs) {System.out.println(jpg);}}@Testpublic void delete() throws Exception {//加载全局的配置文件String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();ClientGlobal.init(filePath);//创建TrackerClient客户端对象TrackerClient trackerClient = new TrackerClient();//通过TrackerClient对象获取TrackerServer信息TrackerServer trackerServer = trackerClient.getConnection();//获取StorageClient对象StorageClient storageClient = new StorageClient(trackerServer, null);//执行文件上传int group1 = storageClient.delete_file("group1", "M00/00/00/wKjThF1VEiyAJ0xzAANdC6JX9KA522.jpg");System.out.println(group1);}@Testpublic void download() throws Exception {//加载全局的配置文件String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();ClientGlobal.init(filePath);//创建TrackerClient客户端对象TrackerClient trackerClient = new TrackerClient();//通过TrackerClient对象获取TrackerServer信息TrackerServer trackerServer = trackerClient.getConnection();//获取StorageClient对象StorageClient storageClient = new StorageClient(trackerServer, null);//执行文件上传byte[] bytes = storageClient.download_file("group1", "M00/00/00/wKjThF1VFfKAJRJDAANdC6JX9KA980.jpg");File file = new File("D:\\ceshi\\1234.jpg");FileOutputStream fileOutputStream = new FileOutputStream(file);BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);bufferedOutputStream.write(bytes);bufferedOutputStream.close();fileOutputStream.close();}//获取文件的信息数据@Testpublic void getFileInfo() throws Exception {//加载全局的配置文件String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();ClientGlobal.init(filePath);//创建TrackerClient客户端对象TrackerClient trackerClient = new TrackerClient();//通过TrackerClient对象获取TrackerServer信息TrackerServer trackerServer = trackerClient.getConnection();//获取StorageClient对象StorageClient storageClient = new StorageClient(trackerServer, null);//执行文件上传FileInfo group1 = storageClient.get_file_info("group1", "M00/00/00/wKjThF1VFfKAJRJDAANdC6JX9KA980.jpg");System.out.println(group1);}//获取组相关的信息@Testpublic void getGroupInfo() throws Exception {//加载全局的配置文件String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();ClientGlobal.init(filePath);//创建TrackerClient客户端对象TrackerClient trackerClient = new TrackerClient();//通过TrackerClient对象获取TrackerServer信息TrackerServer trackerServer = trackerClient.getConnection();StorageServer group1 = trackerClient.getStoreStorage(trackerServer, "group1");System.out.println(group1.getStorePathIndex());//组对应的服务器的地址 因为有可能有多个服务器.ServerInfo[] group1s = trackerClient.getFetchStorages(trackerServer, "group1", "M00/00/00/wKjThF1VFfKAJRJDAANdC6JX9KA980.jpg");for (ServerInfo serverInfo : group1s) {System.out.println(serverInfo.getIpAddr());System.out.println(serverInfo.getPort());}}@Testpublic void getTrackerInfo() throws Exception {//加载全局的配置文件String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();ClientGlobal.init(filePath);//创建TrackerClient客户端对象TrackerClient trackerClient = new TrackerClient();//通过TrackerClient对象获取TrackerServer信息TrackerServer trackerServer = trackerClient.getConnection();InetSocketAddress inetSocketAddress = trackerServer.getInetSocketAddress();System.out.println(inetSocketAddress);}}