FastDFS Overview, Docker Installation, and Spring Boot Integration Guide
This article introduces FastDFS as a lightweight distributed file system, explains its tracker and storage components, provides step‑by‑step Docker installation commands, demonstrates how to integrate FastDFS with Spring Boot using Maven dependencies and configuration, and includes sample code for uploading, downloading, and deleting files.
FastDFS is an open‑source lightweight distributed file storage system consisting of two main components: a tracker that manages storage nodes and balances load, and a storage service that actually stores and serves files.
The guide shows how to install FastDFS using Docker. First, list available images with docker search fastdfs , then pull the chosen image (e.g., docker pull delron/fastdfs ). After pulling, start the tracker container:
docker run -d --network=host --name tracker -v /home/fastdfs/docker/fastdfs/tracker:/var/fdfs delron/fastdfs trackerNext, start a storage container, remembering to replace ip with the server’s actual IP address and optionally set a custom group name:
docker run -d --network=host --name storage -e TRACKER_SERVER=ip:22122 -v /home/fastdfs/docker/fastdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storageThree ports must be opened on the server: 8888 (NGINX proxy), 23000 (storage), and 22122 (tracker).
For Spring Boot integration, add the following Maven parent and dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<!-- FastDFS client -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>Enable FastDFS in the Spring Boot application by adding the annotations:
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
@Import(FdfsClientConfig.class)Configure FastDFS connection properties in application.yml (adjust trackerList to your tracker IP):
#fastDFS
fdfs:
connect-timeout: 600
so-timeout: 1500
trackerList: ip:22122
thumb-image:
width: 150
height: 150
pool:
max-total: 200The controller code provides three REST endpoints for file upload, delete, and download. Key snippets include:
@RestController
@RequestMapping("/file")
public class FileController {
@Resource
private FastFileStorageClient fastFileStorageClient;
private final String FASTDFSSERVERIMAGE = "http://xxxxxx:8888/";
@PostMapping("/upload")
public HashMap
upload(@RequestPart("file") MultipartFile file) {
HashMap
result = new HashMap<>();
try {
String path = fastFileStorageClient.uploadFile(
file.getInputStream(),
file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()),
null).getFullPath();
result.put("msg", FASTDFSSERVERIMAGE + path);
} catch (IOException e) { e.printStackTrace(); }
return result;
}
@DeleteMapping("/delete")
public HashMap
delete(@RequestParam String path) {
HashMap
result = new HashMap<>();
fastFileStorageClient.deleteFile(path);
result.put("msg", "删除成功!");
return result;
}
@GetMapping("/download")
public void downLoad(@RequestParam String url, HttpServletResponse response) throws IOException {
String ext = url.substring(url.lastIndexOf('.') + 1);
byte[] bytes = fastFileStorageClient.downloadFile(
url.substring(0, url.indexOf('/')),
url.substring(url.indexOf('/') + 1),
new DownloadByteArray());
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" +
URLEncoder.encode(UUIDUtils.getUUID() + "." + ext, "UTF-8"));
ServletOutputStream out = response.getOutputStream();
IOUtils.write(bytes, out);
}
}Testing shows successful file upload, browser access to the NGINX proxy (port 8888), and correct download behavior. The article concludes that FastDFS provides a reliable open‑source solution for distributed file storage with load‑balanced tracker and scalable storage nodes.
IT Xianyu
We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.