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