1.需求知识与环境
- 了解Docker的基础命令和基础原理.
- Nginx,Fastdfs的基本结构与运行原理.
- Centos7.x,Docker安装成功。
- 防火墙:开启80,8080,22122,23000端口.
2.本文目的
- 讲解如何用Docker快速配置FastDfs(分布式文件管理系统),因此关于Docker与Nginx,Fastdfs的知识不再谈论.
3.阿里云使用Docker配置FastDfs
// 启动docker
service docker start
// 拉取fastDfs镜像
docker pull morunchang/fastdfs
// 运行tracker
docker run -d --name FastDfs_Tracker --net=host morunchang/fastdfs sh tracker.sh
// 运行Storage
docker run -d --name FastDfs_Storage --net=host -e TRACKER_IP=你的公网Ip:22122 -e GROUP_NAME=group1 morunchang/fastdfs sh storage.sh
// 修改nginx的内容,防止拦截请求
// 进入Fastdfs中
docker exec -it FastDfs_Storage /bin/bash
// 编辑nginx.conf内容
vim /data/nginx/conf/nginx.conf
// 添加以下内容
location /group1/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache http-cache;
proxy_cache_valid 200 304 12h;
proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group1;
expires 30d;
}
// 退出FastDfs
exit
// 重新启动FastDfs_Storage
docker restart FastDfs_Storage
4.测试代码
// 使用封装的FastDFSClient工具类进行测试
@Test
public void test() throws Exception{
// 填写配置文件的路径绝对/相对路径
FastDFSClient fastDFSClient = new FastDFSClient("D:/workspaces-itcast/e3-manager-web/src/main/resources/conf/client.conf");
// uploadFile返回存储路径
String string = fastDFSClient.uploadFile("图片路径");
System.out.println(string);
}
// 控制台信息
group1/M00/00/00/rBEbtVzRZl6ALUMBAACxCrxWSoU212.jpg
访问URL=你的公网IP:8080+方法返回路径;
5.测试需要的jar包和工具类
- 上传工具类
// 工具类源码
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
// 需要把配置文件放在classpath下
public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}
<!-- https://mvnrepository.com/artifact/org.csource/fastdfs-client-java -->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
- 配置文件
tracker_server=112.74.166.230:22122