使用Docker搭建FastDFS+FastDHT(单机+集群版)文件服务器
1、搜索fastdfs文件服务器镜像
docker search fastdfs
DockerHub文件服务器镜像地址:https://hub.docker.com/r/qbanxiaoli/fastdfs
2、linux环境下可用如下命令拉取镜像后直接运行容器,其中IP为容器所在宿主机的内网地址,集群部署IP用逗号分隔
docker pull qbanxiaoli/fastdfs
docker run -d --restart=always --net=host --name=fastdfs -e IP=192.168.23.21 -v ~/fastdfs:/var/local qbanxiaoli/fastdfs
3、测试fastdfs是否搭建成功
docker exec -it fastdfs /bin/bash
echo "Hello FastDFS!">index.html
fdfs_test /etc/fdfs/client.conf upload index.html
搭建成功后效果如下图所示:
4、文件上传成功后默认nginx访问端口为80端口,可省略,访问路径如下图,如需修改配置请参考:https://github.com/qbanxiaoli/fastdfs
Spring Boot集成FastDFS
1、使用maven引入第三方jar包,来源于https://github.com/tobato/FastDFS_Client
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
2、添加fastdfs配置
# 分布式文件系统fastdfs配置
fdfs:
# socket连接超时时长
so-timeout: 1500
# 连接tracker服务器超时时长
connect-timeout: 600
pool:
#从池中借出的对象的最大数目(配置为-1表示不限制)
max-total: -1
#获取连接时的最大等待毫秒数(默认配置为5秒)
max-wait-millis: 5000
#每个key最大连接数
max-total-per-key: 50
#每个key对应的连接池最大空闲连接数
max-idle-per-key: 10
#每个key对应的连接池最小空闲连接数
min_idle_per_key: 5
# 缩略图生成参数,可选
thumb-image:
width: 150
height: 150
# 跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加- x.x.x.x:port
tracker-list:
- ${FDFS_TRACKER:127.0.0.1}:${FDFS_PORT:22122}
# 存储服务器storage_server访问地址
web-server-url: http://${WEB_SERVER_URL:127.0.0.1}:${WEB_PORT:80}/
FastDFS文件操作工具类
@Component
public class FastDFSUtil {
private static ThumbImageConfig thumbImageConfig;
private static FastFileStorageClient fastFileStorageClient;
private static FdfsWebServer fdfsWebServer;
public FastDFSUtil(ThumbImageConfig thumbImageConfig, FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
FastDFSUtil.thumbImageConfig = thumbImageConfig;
FastDFSUtil.fastFileStorageClient = fastFileStorageClient;
FastDFSUtil.fdfsWebServer = fdfsWebServer;
}
/**
* @param multipartFile 文件对象
* @return 返回文件地址
* @author qbanxiaoli
* @description 上传文件
*/
@SneakyThrows
public static String uploadFile(MultipartFile multipartFile) {
StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return storePath.getFullPath();
}
/**
* @param multipartFile 图片对象
* @return 返回图片地址
* @author qbanxiaoli
* @description 上传缩略图
*/
@SneakyThrows
public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) {
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
return thumbImageConfig.getThumbImagePath(storePath.getFullPath());
}
/**
* @param file 文件对象
* @return 返回文件地址
* @author qbanxiaoli
* @description 上传文件
*/
@SneakyThrows
public static String uploadFile(File file) {
@Cleanup FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
}
/**
* @param file 图片对象
* @return 返回图片地址
* @author qbanxiaoli
* @description 上传缩略图
*/
@SneakyThrows
public static String uploadImageAndCrtThumbImage(File file) {
@Cleanup FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return thumbImageConfig.getThumbImagePath(storePath.getFullPath());
}
/**
* @param bytes byte数组
* @param fileExtension 文件扩展名
* @return 返回文件地址
* @author qbanxiaoli
* @description 将byte数组生成一个文件上传
*/
@SneakyThrows
public static String uploadFile(byte[] bytes, String fileExtension) {
@Cleanup ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null);
return storePath.getFullPath();
}
/**
* @param filePath 文件访问地址
* @author qbanxiaoli
* @description 下载文件
*/
@SneakyThrows
public static byte[] downloadFile(String filePath) {
StorePath storePath = StorePath.parseFromUrl(filePath);
return fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
}
/**
* @param filePath 文件访问地址
* @author qbanxiaoli
* @description 删除文件
*/
@SneakyThrows
public static void deleteFile(String filePath) {
StorePath storePath = StorePath.parseFromUrl(filePath);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
}
// 获取文件服务器地址
public static String getWebServerUrl() {
return fdfsWebServer.getWebServerUrl();
}
}
版权声明:本文为qq_36143819原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。