Step-by-Step Guide to Deploy FastDFS with Docker and Integrate It into a Spring Boot Application
This tutorial explains how to install Docker, pull and run a FastDFS Docker image, verify the deployment, and then integrate FastDFS with Spring Boot by adding Maven dependencies, configuration files, and a Java utility class for file operations.
In this article the author, a senior architect, shares a practical guide for setting up FastDFS using Docker and then integrating it into a Spring Boot project.
1. Install Docker
yum install -y docker-io #安装docker
service docker start #启动docker
docker -v # 查看docker版本2. Pull the FastDFS image
docker pull qbanxiaoli/fastdfs3. Run the FastDFS container
docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=192.168.127.131 -e WEB_PORT=80 -v ${HOME}/fastdfs:/var/local/fdfs qbanxiaoli/fastdfs
# IP is the server's public or virtual machine IP, -e WEB_PORT=80 sets the nginx port4. Test the FastDFS deployment
docker exec -it fastdfs /bin/bash
echo "Hello FastDFS!" >index.html
fdfs_test /etc/fdfs/client.conf upload index.htmlIf the URL is returned, the FastDFS service is running correctly.
5. Integrate FastDFS with Spring Boot
Add the Maven dependency:
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>Annotate the Spring Boot main class:
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)Create a FastDFSClient utility class (the full source code is kept unchanged below):
package com.yd.client.common;
import com.github.tobato.fastdfs.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
@Component
public class FastDFSClient {
private static Logger log = LoggerFactory.getLogger(FastDFSClient.class);
private static FastFileStorageClient fastFileStorageClient;
private static FdfsWebServer fdfsWebServer;
@Autowired
public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
FastDFSClient.fastFileStorageClient = fastFileStorageClient;
FastDFSClient.fdfsWebServer = fdfsWebServer;
}
/**
* @param multipartFile 文件对象
* @return 返回文件地址
*/
public static String uploadFile(MultipartFile multipartFile) {
try {
StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return storePath.getFullPath();
} catch (IOException e) {
log.error(e.getMessage());
return null;
}
}
// Additional methods for uploading images, byte arrays, downloading and deleting files are defined similarly.
public static String getResAccessUrl(String path) {
String url = fdfsWebServer.getWebServerUrl() + path;
log.info("上传文件地址为:\n" + url);
return url;
}
}Configure application.yml for FastDFS:
# 分布式文件系统fastdfs配置
fdfs:
soTimeout: 1500 # socket连接超时时长
connectTimeout: 600 # 连接tracker服务器超时时长
pool:
max-total: 153 # 从池中借出的对象的最大数目
max-wait-millis: 102 # 获取连接时的最大等待毫秒数
thumbImage:
width: 150
height: 150
trackerList:
- 192.168.127.131:22122 # tracker server address
web-server-url: http://192.168.127.131/
spring:
http:
multipart:
max-file-size: 100MB # 最大支持文件大小
max-request-size: 100MBWrite a simple test class to upload and delete a file:
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileClientApplicationTests {
@Test
public void Upload() {
String fileUrl = this.getClass().getResource("/test.jpg").getPath();
File file = new File(fileUrl);
String str = FastDFSClient.uploadFile(file);
FastDFSClient.getResAccessUrl(str);
}
@Test
public void Delete() {
FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg");
}
}Running the test confirms that the file URL returned can be accessed via a browser.
Finally, the article includes promotional messages and QR codes for a “Top Architect” community, but the core technical content provides a complete, reproducible guide for deploying FastDFS with Docker and using it in a Spring Boot application.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.