Để triển khai hệ thống phân tán lưu trữ tệp FastDFS một cách nhanh chóng và nhất quán, Docker là lựa chọn lý tưởng. Dưới đây là hướng dẫn cấu hình từ cơ bản đến tích hợp ứng dụng Spring Boot.
1. Kéo và chuẩn bị môi trường
Tải image FastDFS đã được đóng gói sẵn:
docker pull season/fastdfs:1.2
Tạo các thư mục dữ liệu cần thiết trên host:
mkdir -p /opt/fastdfs/tracker/data \
/opt/fastdfs/storage/data \
/opt/fastdfs/storage/path
2. Khởi tạo tracker server
Chạy container tracker với cấu hình tự khởi động lại và gắn volume:
docker run -d \
--name fdfs-tracker \
--restart=unless-stopped \
--net=host \
-v /opt/fastdfs/tracker/data:/fastdfs/tracker/data \
season/fastdfs:1.2 tracker
3. Khởi tạo storage server
Lưu ý thay YOUR_TRACKER_IP bằng địa chỉ IP thực tế của máy chạy tracker (không dùng 127.0.0.1 nếu chạy trên máy khác):
docker run -d \
--name fdfs-storage \
--restart=unless-stopped \
--net=host \
-v /opt/fastdfs/storage/data:/fastdfs/store_path \
-e TRACKER_SERVER="YOUR_TRACKER_IP:22122" \
season/fastdfs:1.2 storage
4. Cấu hình client và nginx
Sau khi khởi chạy tracker, chỉnh sửa file /etc/fdfs/client.conf trong container hoặc trên host (nếu đã mount) để đảm bảo giá trị tracker_server đúng:
tracker_server=YOUR_TRACKER_IP:22122
Khởi tạo nginx làm web server phục vụ file tĩnh:
docker run -d \
--name fdfs-nginx \
--restart=unless-stopped \
-p 8888:80 \
-v /opt/fastdfs/storage/data:/fastdfs/store_path \
-v /opt/fastdfs/nginx.conf:/etc/nginx/conf/nginx.conf \
-e TRACKER_SERVER="YOUR_TRACKER_IP:22122" \
season/fastdfs:1.2 nginx
Nội dung /opt/fastdfs/nginx.conf cần chứa đoạn cấu hình location sau:
location / {
root /fastdfs/store_path/data;
ngx_fastdfs_module;
}
5. Kiểm tra hoạt động thủ công
Vào container tracker và tải lên một tệp kiểm thử:
docker exec -it fdfs-tracker bash
echo "test content" > test.txt
fdfs_upload_file /etc/fdfs/client.conf test.txt
Kết quả trả về dạng: group1/M00/00/00/AbCdeFgHiJkLmNoPqRsTuVwXyZ123.txt. Truy cập qua trình duyệt:
http://YOUR_NGINX_IP:8888/group1/M00/00/00/AbCdeFgHiJkLmNoPqRsTuVwXyZ123.txt
6. Tích hợp với Spring Boot
Thêm dependency vào pom.xml:
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
Cấu hình trong application.yml:
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image:
width: 150
height: 150
tracker-list: YOUR_TRACKER_IP:22122
web-server-url: http://YOUR_NGINX_IP:8888/
7. Cấu hình tự động hóa và lớp tiện ích
Tạo lớp cấu hình:
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsAutoConfiguration { }
Tạo service quản lý file:
@Service
public class FileStorageService {
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Autowired
private FdfsWebServer webServer;
public String upload(MultipartFile file) throws IOException {
StorePath path = fastFileStorageClient.uploadFile(
file.getInputStream(),
file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()),
null
);
return webServer.getWebServerUrl() + path.getFullPath();
}
public void remove(String url) {
if (url == null || url.isBlank()) return;
try {
StorePath storePath = StorePath.parseFromUrl(url);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception ignored) {}
}
public byte[] fetch(String url) {
StorePath storePath = StorePath.parseFromUrl(url);
DownloadByteArray callback = new DownloadByteArray();
return fastFileStorageClient.downloadFile(
storePath.getGroup(),
storePath.getPath(),
callback
);
}
}