最早接觸FastDFS還是一年之前,那會兒我們的一個項目中就用到了這個技術,但是關於FastDFS的一些基礎知識也一直沒有去研究,今天趁着有時間想啃一下這塊骨頭。
概念 |
1.什麼是FastDFS?
FastDFS是阿里的技術大佬餘慶在2008年用C語言實現的一款分佈式文件管理系統。它主要是用來解決大容量存儲和負載均衡問題,支持橫向組擴展和同步熱備。其主要的功能有以下四點:
- 文件存儲
- 文件同步
- 文件上傳
- 文件下載
2.爲什麼要用FastDFS?
FastDFS的開發初衷是希望解決互聯網大數據量、高訪問量的環境下,文件的高效訪問和高效存儲管理等問題。比如圖片文件、視頻文件我們都能夠使用FastDFS這種存儲管理系統來統一處理。
3.FastDFS系統結構?
如下圖所示:FastDFS系統結構分爲兩部分:Tracker cluster
、Storage cluster
,其中tracker
用來負責文件訪問的調度和負載平衡。而storage
用來存儲文件。storage
作爲最終的存儲部分一般會以集羣的形式進行搭建,一個storage cluster
又分爲很多個組,每個組又相當於是一個小的集羣。同組內的storage server
之間會進行數據同步,而不同組之間不會進行通信。 我們通過tracker
可以實現對storage
的負載均衡。
安裝 |
4.FastDFS安裝?
連接Linux
系統:
1. 安裝gcc
環境
yum install gcc-c++
2. 安裝libevent
庫
yum -y install libevent
3. 下載libfastcommon
將下載好的libfastcommon
使用Xftp
上傳到linux系統的/usr/local/
下,並進行解壓縮tar -zxvf libfastcommon 1.0.43.tar.gz
4. 依次執行以下命令:
cd /usr/local/libfastcommon-1.0.43
./make.sh
./make.sh install
5. 下載fastdfs
安裝包
使用Xftp
將安裝包上傳到linux的/usr/local/
目錄下
tar -zxvf fastdfs-6.06.tar.gz
cd /usr/local//fastdfs-6.06
./make.sh
./make.sh install
使用命令查看是否安裝成功:
ll /usr/bin/fdfs*
cd conf
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/
6. 修改配置
FastDFS安裝後配置文件位於/etc/fdfs/目錄下,修改該目錄下的配置文件;
cd /etc/fdfs
把所有的.sample後綴都去掉,使用 mv 命令改文件名,去掉文件名後綴;
mv client.conf.sample client.conf
mv client.conf.sample client.conf
mv storage.conf.sample storage.conf
mv storage_ids.conf.sample storage_ids.conf
mv tracker.conf.sample tracker.conf
修改tracker.conf文件
vim tracker.conf
修改如下:
創建存儲數據的文件夾
mkdir -p /home/fdfs/storage/files
修改storage.conf文件
vim storage.conf
分別修改下面幾個地方:
7. 啓動FastDFS
啓動FastDFS一定需要先啓動tracker,然後再啓動storage。
在任意目錄下執行:
fdfs_trackerd /etc/fdfs/tracker.conf
fdfs_storaged /etc/fdfs/storage.conf
然後去/home/fdfs/storage
下面看看應該會新增加兩個文件夾(多一個data和logs
)
查看storage是否已經登記到了tracker下:
fdfs_monitor /etc/fdfs/storage.conf
8. 測試文件上傳
依次執行以下命令:
mkdir /home/fdfs/client
vim /etc/fdfs/client.conf
然後在/root
目錄下準備一個a.txt
,然後輸入以下命令
fdfs_test /etc/fdfs/client.conf upload /root/a.txt
返回的url
粘貼到瀏覽器中就可以訪問到該文件了
9. 下載FastDFS的Nginx模塊擴展包
cd /usr/local/
tar -zxvf fastdfs-nginx-module-1.22.tar.gz
cd /usr/local/fastdfs-nginx-module-1.22/src
cp mod_fastdfs.conf /etc/fdfs/
vim /etc/fdfs/mod_fastdfs.conf
cd /usr/local/nginx-1.17.0
./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src
make
make install
配置Nginx請求轉發:
vim /usr/local/nginx/conf/nginx.conf
10. 啓動Nginx
cd /usr/local/nginx/sbin/
./nginx
11. IDEA中的文件上傳測試:
創建一個springboot項目,並在項目的resource
目錄下新建一個fastdfs-client.properties
配置文件
將下列配置粘貼到配置文件中:
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = 47.93.181.229:22122
fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000
在pom.xml
文件中引入依賴:
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
然後在Test類中寫文件上傳代碼進行測試:
@Test
void upload() {
try {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient1 client = new StorageClient1(trackerServer, null);
NameValuePair nvp[] = null;
//上傳到文件系統
String[] fileId = client.upload_file("D:\\1.jpg", "jpg",
nvp);
System.out.print("地址爲:");
String path = "";
for (String str : fileId) { // 組名+磁盤地址
path = path + str + "/";
}
// 進行地址處理並輸出
System.out.println(path.substring(0,path.length()-1));
} catch (Exception e) {
e.printStackTrace();
}
}
在瀏覽器中訪問該文件:
成功!