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.
7. `<!-- Test -->`
8. `<dependency>`
9. `<groupId>org.springframework.boot</groupId>`
10. `<artifactId>spring-boot-starter-web</artifactId>`
11. `</dependency>`
12.
13. `<!-- lombok -->`
14. `<dependency>`
15. `<groupId>org.projectlombok</groupId>`
16. `<artifactId>lombok</artifactId>`
17. `<version>1.16.14</version>`
18. `</dependency>`
- 将FastDFS配置引入项目
仅需要在启动类上增加注解 @EnableSwagger2
,启动该注解使得用在Controller中的Swagger注解生效
1. `@Import(FdfsClientConfig.class)`
2. `@SpringBootApplication`
3. `public class Server {`
4.
5. `public static void main(String[] args) {`
6. `SpringApplication.run(Server.class, args);`
7. `}`
8. `}`
- 增加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.
3. `import com.github.tobato.fastdfs.domain.StorePath;`
4. `import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;`
5. `import com.github.tobato.fastdfs.service.FastFileStorageClient;`
6. `import lombok.extern.slf4j.Slf4j;`
7. `import org.apache.commons.io.FilenameUtils;`
8. `import org.apache.commons.lang3.StringUtils;`
9. `import org.springframework.beans.factory.annotation.Autowired;`
10. `import org.springframework.stereotype.Component;`
11. `import org.springframework.web.multipart.MultipartFile;`
12.
13. `import java.io.ByteArrayInputStream;`
14. `import java.io.IOException;`
15. `import java.nio.charset.Charset;`
16.
17. `/**`
18. `* @author Yezhiwei`
19. `* @date 17/12/11`
20. `*/`
21. `@Slf4j`
22. `@Component`
23. `public class FastDFSClientWrapper {`
24.
25.
26. `@Autowired`
27. `private FastFileStorageClient storageClient;`
28.
29.
30. `/**`
31. `* 上传文件`
32. `*`
33. `* @param file 文件对象`
34. `* @return 文件访问地址`
35. `* @throws IOException`
36. `*/`
37. `public String uploadFile(MultipartFile file) throws IOException {`
38. `StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);`
39. `return getAccessUrl(storePath);`
40. `}`
41.
42. `/**`
43. `* 将一段字符串生成一个文件上传`
44. `*`
45. `* @param content 文件内容`
46. `* @param fileExtension`
47. `* @return`
48. `*/`
49. `public String uploadFile(String content, String fileExtension) {`
50. `byte[] buff = content.getBytes(Charset.forName("UTF-8"));`
51. `ByteArrayInputStream stream = new ByteArrayInputStream(buff);`
52. `StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);`
53. `return getAccessUrl(storePath);`
54. `}`
55.
56. `private String getAccessUrl(StorePath storePath) {`
57. `String fileUrl = storePath.getFullPath();`
58. `return fileUrl;`
59. `}`
60.
61. `/**`
62. `* 删除文件`
63. `*`
64. `* @param fileUrl 文件访问地址`
65. `* @return`
66. `*/`
67. `public void deleteFile(String fileUrl) {`
68. `if (StringUtils.isEmpty(fileUrl)) {`
69. `return;`
70. `}`
71. `try {`
72. `StorePath storePath = StorePath.praseFromUrl(fileUrl);`
73. `storageClient.deleteFile(storePath.getGroup(), storePath.getPath());`
74. `} catch (FdfsUnsupportStorePathException e) {`
75. `log.warn(e.getMessage());`
76. `}`
77. `}`
78. `}`
客户端主要包括以下接口: TrackerClient - TrackerServer接口 GenerateStorageClient - 一般文件存储接口 (StorageServer接口) FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口) AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)
注:可根据自身的业务需求,将其它接口也添加到组件类中即可。
- 测试
1. `@RunWith(SpringJUnit4ClassRunner.class)`
2. `@SpringBootTest`
3. `@Slf4j`
4. `public class FastDFSClientTest {`
5.
6. `@Autowired`
7. `private FastDFSClientWrapper fastDFSClientWrapper;`
8.
9. `@Test`
10. `public void updateFileByContent() {`
11.
12.
13. `String url = fastDFSClientWrapper.uploadFile("This is test", "png");`
14. `log.info("url {}", url);`
15.
16. `//输出/group112/M00/BB/6D/CgAAcFoukU2sCPUsAAAADDzy24o518.png`
17. `}`
18.
19.
20. `@Test`
21. `public void deleteFile() {`
22.
23.
24. `fastDFSClientWrapper.deleteFile("http://10.0.0.112/group112/M00/BB/6D/CgAAcFoukU2sCPUsAAAADDzy24o518.png");`
25. `}`
26. `}`