FastDFS安装部署与Springboot实战
文章目录
安装
安装libfastcommon
unzip libfastcommon-master.zip
cd libfastcommon-master
./make.sh
./make.sh install
安装FastDFS
./make.sh //LD_LIBRARY_PATH=/usr/lib
./make.sh install
cd /etc/fdfs
mv client.conf.sample client.conf
mv storage.conf.sample storage.conf
mv tracker.conf.sample tracker.conf
创建存储目录
mkdir -p /data/storage
修改tracker.conf
base_path=/data/storage
修改storage.conf
base_path=/data/storage
store_path0=/data/storage
tracker_server=192.168.1.111:22122
修改client.conf
base_path=/data/storage
tracker_server=192.168.1.111:22122
#启动
fdfs_trackerd /etc/fdfs/tracker.conf [restart]
fdfs_storaged /etc/fdfs/storage.conf
#测试
fdfs_test /etc/fdfs/client.conf upload /opt/dev/0.jpg
FastDFS+Nginx
编译安装Nginx
-- 需要先安装fastdfs
yum install gcc gcc-c++
yum install zlib zlib-devel
yum install perl
./configure --add-module=/opt/dev/fastdfs-nginx-module-master/src/ --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module [--with-pcre=/opt/soft/pcre-8.32 --with-openssl=/opt/soft/openssl-1.0.1c]
make && make install
注:如已安装Nginx,需要重新编译nginx
配置mod_fastdfs.conf
cp /opt/dev/fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs/
mkdir /data/storage/ngx
vi /etc/fdfs/mod_fastdfs.conf
# the base path to store log files
base_path=/data/storage/ngx
# FastDFS tracker_server 可配置多个
tracker_server=192.168.9.102:22122
# 和storage.conf一样
url_have_group_name = true
store_path0=/data/storage
#group_name=group1
#[group1]
#group_name=group1
#storage_server_port=23000
#store_path_count=1
#store_path0=/home/data/fdfs
cd /opt/dev/fastdfs-5.10/conf
cp http.conf /etc/fdfs
cp mime.types /etc/fdfs
nginx.conf
listen端口号需要和tracker.conf http.server_port一样(80)
location /group1/M00 {
#root /data/storage/data0/data;
ngx_fastdfs_module;
}
- perl兼容问题:rm /usr/bin/pod2man
- 优先安装fastDFS5.1版本(硬盘/fastdfs_2)
- Linux安装防火墙注意设置排除端口号
JAVA SDK
下载fastDFS SDK包
public class FileManager {
private static final long serialVersionUID = 1L;
private static TrackerClient trackerClient;
private static TrackerServer trackerServer;
private static StorageServer storageServer;
private static StorageClient storageClient;
static {
try {
ClientGlobal.init();
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageClient = new StorageClient(trackerServer, storageServer);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String upload(FastDFSFile file, NameValuePair[] valuePairs) {
String[] uploadResults = null;
try {
synchronized (storageClient) {
uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), valuePairs);
}
} catch (Exception e) {
e.printStackTrace();
}
String groupName = uploadResults[0];
String remoteFileName = uploadResults[1];
return ClientGlobal.nginx_server + "/" + groupName + "/" + remoteFileName;
}
}
// 参数数组
NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("fileName", file.getOriginalFilename());
meta_list[1] = new NameValuePair("fileLength", String.valueOf(file.getSize()));
meta_list[2] = new NameValuePair("fileExt", ext);
try {
String filePath = FileManager.upload(dfs, meta_list);
return new UploadRet(0, filePath, "SUCCESS");
} catch (Exception e) {
return new UploadRet(1, "", "ERROR");
}
Springboot集成
maven
<groupId>com.luhuiguo</groupId>
<artifactId>fastdfs-spring-boot-starter</artifactId>
<version>0.2.0</version>
application.yml
fdfs:
so-timeout: 3000
connect_timeout: 3000
tracker-list: # 可设置多个
- 192.168.31.205:22122
接口实例
@Autowired(required = false)
private FastFileStorageClient storageClient;
@Autowired
private AppendFileStorageClient appendFileStorageClient;
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public UploadRet upload(@RequestParam("file") MultipartFile file, Integer t, HttpServletRequest request) throws IOException {
// 文件后缀
String suffix = StringUtil.getFileType(file.getOriginalFilename());
String[] type;
long size;
if (t == null) t = 1;
if (t == 2) { // 文件
type = sysConfig.getUpload().getFileType();
size = sysConfig.getUpload().getFileSize();
} else { //图片
type = sysConfig.getUpload().getImgType();
size = sysConfig.getUpload().getImgSize();
}
// 文件大小
if (file.getSize() > size * 1024 * 1024) {
return new UploadRet(1, "", "单个文件最大" + size + "MB");
}
// 文件格式
if (!Arrays.asList(type).contains(suffix.toLowerCase())) {
return new UploadRet(1, "", "文件格式不支持");
}
if (!sysConfig.getUpload().getFdfs()) {
String basePathFormat = DateUtil.getYearAndMonth(false);
String dirPath = "upload/" + basePathFormat;
if (t == 2) { // 文件
dirPath = "upload/file/" + basePathFormat;
}
String uploadPath = sysConfig.getStaticPath() + dirPath;
String filename = StringUtil.randomFilename(file.getOriginalFilename());
// String realPath = request.getSession().getServletContext().getRealPath("/" + uploadPath + basePathFormat);
File filePath = new File(uploadPath);
if (!filePath.exists()) {
filePath.mkdirs();
}
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath, filename));
// 添加水印
// FileUtils.copyInputStreamToFile(new ByteArrayInputStream(FDFSUtil.waterMark(file.getInputStream(), suffix)), new File(filePath, filename));
return new UploadRet(0, sysConfig.getStaticHost() + "static/" + dirPath + "/" + filename, "SUCCESS");
} else {
// 添加水印:FDFSUtil.waterMark(file.getInputStream(), suffix)
StorePath storePath = storageClient.uploadFile(file.getBytes(), suffix);
String filePath = sysConfig.getFdfsNginx() + storePath.getFullPath();
return new UploadRet(0, filePath, "SUCCESS");
}
}
Docker-Compose部署
docker pull registry.cn-hangzhou.aliyuncs.com/jackson/fdfs:2.0
version: "3"
services:
tracker:
image: jackson/fdfs:1.8
container_name: fdfs_tracker
volumes:
- /home/data/fdfs:/data/storage
- /home/data/fdfs/data0:/data/storage/data0
- /home/data/fdfs/ngx:/data/storage/ngx
ports:
- "22122"
environment:
TZ: Asia/Shanghai
command:
- /bin/sh
- -c
- |
fdfs_trackerd /etc/fdfs/tracker.conf
tail -F /data/storage/logs/trackerd.log
storage:
image: jackson/fdfs:1.8
container_name: fdfs_storage
volumes:
- /home/data/fdfs:/data/storage
- /home/data/fdfs/data0:/data/storage/data0
- /home/data/fdfs/ngx:/data/storage/ngx
ports:
- "23000"
environment:
TZ: Asia/Shanghai
links:
- tracker
command:
- /bin/sh
- -c
- |
fdfs_storaged /etc/fdfs/storage.conf
tail -F /data/storage/logs/storaged.log
nginx:
image: jackson/fdfs:1.8
container_name: fdfs_nginx
volumes:
- /home/data/fdfs:/data/storage
- /home/data/fdfs/data0:/data/storage/data0
- /home/data/fdfs/ngx:/data/storage/ngx
environment:
TZ: Asia/Shanghai
links:
- tracker
ports:
- "8089:80"
command:
- /bin/sh
- -c
- |
/usr/local/nginx/sbin/nginx
tail -F /usr/local/nginx/logs/access.log
networks:
default:
external:
name: docker-br0