0
点赞
收藏
分享

微信扫一扫

【docker】安装FastDFS,并使用java测试上传

墨春 2021-09-21 阅读 51

常用的分布式文件存储系统

1.FastDFS(适合存储小文件)
2.Hdfs(适合存储大文件)
3.gfs(适合存储大文件)

FastDFS


Tracker 文件跟踪

Storage 文件存储

组:storage名称一致,为同一组,同一组,文件相同,文件备份

不同组:文件不相同,主要做负载

镜像安装(以下出现的ip都改为自己的服务器ip)

Run as a tracker

docker run -d --name tracker --net=host morunchang/fastdfs sh tracker.sh

Run as a storage server

docker run -d --name storage --net=host -e TRACKER_IP=<your>:22122 -e GROUP_NAME=<group name> morunchang/fastdfs sh storage.sh

运行成功后


修改nginx配置

进入容器

docker exec -it storage sh

编辑nginx配置文件

vi etc/nginx/conf/nginx.conf

将8080改为80


java上传测试

参考项目地址:https://github.com/tobato/FastDFS_Client

1.导入依赖

<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>

2.添加ComponetImport 配置类

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.jmx.support.RegistrationPolicy;

/**
* 导入FastDFS-Client组件
* @author tobato
*/

@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {
// 导入依赖组件
}
  1. yml配置
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: #缩略图生成参数
width: 150
height: 150
tracker-list: #TrackerList参数,支持多个
- 192.168.40.128:22122
pool:
max-total: 50

4.上传工具类

import java.io.IOException;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

@Component
public class FastDfsUtils {
@Autowired
FastFileStorageClient client;

/**
* 上传文件
*/

public String uploadFile(String groupName,MultipartFile file) {
String filePath = null;
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
// 组名,文件流,文件大小,文件的后缀名
StorePath uploadFile = client.uploadFile(groupName, inputStream, inputStream.available(), getFileSuffix(file.getOriginalFilename()));
// 组名+文件路径和文件名,访问时前面要加服务器地址
filePath = uploadFile.getFullPath();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}

/**
* 删除文件
* groupName "g1"
* path "M00/00/00/wKgogF0ADUSAdolnAAAY5E7F2PQ080.jpg"
*/

public void deleteFile(String groupName, String path) {
client.deleteFile(groupName, path);
}

/**
* 获取文件后缀名
*/

private String getFileSuffix(String fileName) {
return fileName.substring(fileName.lastIndexOf(".")+1);
}

}

5.访问
工具类返回的地址为:
g1/M00/00/00/wKgogF0ADUSAdolnAAAY5E7F2PQ080.jpg
访问时需在前面加上服务器地址:
http://192.168.40.128/g1/M00/00/00/wKgogF0ADUSAdolnAAAY5E7F2PQ080.jpg
若未修改nginx配置则访问地址为:
http://192.168.40.128:8080/g1/M00/00/00/wKgogF0ADUSAdolnAAAY5E7F2PQ080.jpg

举报

相关推荐

0 条评论