參考http://www.itdecent.cn/p/51146dbe51d2,經(jīng)過驗證可行。轉(zhuǎn)載僅用于個人學習實踐。
FastDFS架構(gòu)包括Tracker server和Storage server。客戶端請求Tracker server進行文件上傳、下載,通過Tracker server調(diào)度最終由Storage server完成文件上傳和下載。
Tracker server作用是負載均衡和調(diào)度,通過Tracker server在文件上傳時可以根據(jù)一些策略找到Storage server提供文件上傳服務。可以將tracker稱為追蹤服務器或調(diào)度服務器。
Storage server作用是文件存儲,客戶端上傳的文件最終存儲在Storage服務器上,Storage server沒有實現(xiàn)自己的文件系統(tǒng)而是利用操作系統(tǒng) 的文件系統(tǒng)來管理文件。可以將storage稱為存儲服務器。
1.依賴環(huán)境下載
系統(tǒng): vagrant(centos7)
- yum install -y gcc-c++ #需要依賴gcc
- yum install -y pcre #用于支持rewrite模塊 pcre-8.32-17
- yum install -y libevent #FastDFS依賴libevent庫 libevent-2.0.21-4.el7.x86_64
- yum install -y zlib zlib-devel #用于支持gzip模塊 zlib-1.2.7-17.el7.x86_64
- libfastcommon-master.zip #libfastcommon包含了FastDFS運行所需要的一些基礎(chǔ)庫https://github.com/happyfish100/libfastcommon/releases
- fastdfs-nginx-module-master.zip # https://codeload.github.com/happyfish100/fastdfs-nginx-module/zip/master
- FastDFS版本:fastdfs-5.11.zip #https://github.com/happyfish100/fastdfs/archive/V5.11.zip
- Nginx版本:nginx-1.14.tar.gz : #官網(wǎng)下載: http://nginx.org/en/download.html
2.安裝libfastcommon
unzip libfastcommon-1.0.39.zip
mv libfastcommon-1.0.39 /usr/local/libfastcommon
cd /usr/local/libfastcommon/
./make.sh #編譯
./make.sh install #安裝
# 檢查確認/usr/lib64 和 /usr/lib 目錄下都生成了 libfastcommon.so (有些版本可能/usr/lib下不會生成)
ls /usr/lib64 | grep libfastcommon.so
ls /usr/lib | grep libfastcommon.so
3.安裝tracker
cd /download
unzip fastdfs-5.11.zip
mv fastdfs-5.11 /usr/local/FastDFS
cd /usr/local/FastDFS/
./make.sh && ./make.sh install #編譯之后接著進行安裝
#確認安裝成功,沒有報錯后,拷貝配置文件到/etc/fdfs
cp /usr/local/FastDFS/conf/* /etc/fdfs/
#,進入/etc/fdfs 目錄, 如果沒有tracker.conf就拷貝一份tracker.conf.sample 去掉sample
cp tracker.conf.sample track.conf
#配置tracker.conf
vim track.conf # 修改以下配置
#===============
base_path=/home/fastdfs #基礎(chǔ)目錄,以后的data 和日志目錄都會放在此目錄下
http.server_port=80 #配置http服務端口,這個端口跟后面nginx的監(jiān)聽端口對應
mkdir /home/fastdfs
#啟動tracker,運行如下命令
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
#檢查是否啟動成功:默認端口22122
ps -ef | grep fdfs
#檢查/home/fastdfs下是否生成data / logs 兩個目錄
ls /home/fastdfs/
4.安裝storage
cd /etc/fdfs
#如果沒有storage.conf 拷貝一份storage.conf.sample 命名為storage.conf
cp storage.conf.sample storage.conf
#配置storage.conf
vi storage.conf #修改如下配置
#===========================
group_name=group1 #配置組名,同一個組的storage 互為備份
base_path=/home/fastdfs #基礎(chǔ)目錄
#store存放文件的位置(store_path), 可以配置多個, 記得創(chuàng)建路徑
store_path0=/home/fdfs_storage
#如果有多個掛載磁盤則定義多個store_path,如下
#store_path1=.....
#store_path2=......
#配置tracker服務器:IP,阿里云服務器如果需要外部訪問請配置公網(wǎng)ip, 不要使用私有ip
tracker_server=192.168.33.111:22122
#如果有多個tracker則配置多個tracker
#tracker_server=192.168.33.112:22122
#配置http端口
http.server_port=80
#==========完===========
#創(chuàng)建 /home/fdfs_storage 目錄
mkdir -p /home/fdfs_storage
#啟動storage
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
#查看啟動是否成功
fdfs_monitor /etc/fdfs/storage.conf #查看fdfs狀態(tài)
ps -ef | grep fdfs
#查看目錄是否創(chuàng)建,/home/fdfs_storage/data 應該會自動生成256個文件夾
ls /home/fdfs_storage/data
5.測試上傳文件
cd /etc/fdfs
cp client.conf.sample client.conf
vim client.conf #修改如下配置
#=================
base_path=/home/fastdfs
tracker_server=192.168.33.111:22122
#=======完========
#準備一張圖片進行測試
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /download/fengjing.jpg
#一切正常的話會返回圖片地址等信息
example file url: http://192.168.33.111/group1/M00/00/00/wKghb1vqf7GAIdYqAAJo1aj_C9s003_big.jpg
#檢查文件是否存在(文件名被存放在/home/fdfs_storage/data/00/00對應的目錄中),如果存在即是保存 OK,此時還無法使用http下載
cd /home/fdfs_storage/data/00/00 #查看是否存在wKghb1vqf7GAIdYqAAJo1aj_C9s003_big.jpg
6.FastDFS 和Nginx整合
#解壓 fastdfs-nginx-module 到 /usr/local目錄下;
cd /download
unzip fastdfs-nginx-module-master.zip
mv fastdfs-nginx-module-master /usr/local/fastdfs-nginx-module
cd /usr/local/fastdfs-nginx-module/src
#修改config文件
# 將文件中的所有 /usr/local/ 路徑改為 /usr/ ,
#如果后面步驟中編譯nginx報錯把下面兩項做以下修改
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
#拷貝配置文件到/etc/fdfs
cp mod_fastdfs.conf /etc/fdfs/
#修改 /etc/fdfs/mod_fastdfs.conf ;
vi /etc/fdfs/mod_fastdfs.conf
#======修改以下內(nèi)容========
base_path=/home/fastdfs #保存日志的路徑
tracker_server=192.168.33.111:22122 #track_server配置的服務端口
url_have_group_name=true #url中是否包含group名稱
store_path0=/home/fdfs_storage #指定文件存儲路徑(必須和storage.conf配置相同)
ls /usr/lib | grep libfdfsclient.so
ls /usr/lib64 | grep libfdfsclient.so
mkdir -p /var/temp/nginx/client
tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/
#進入解壓目錄, 日志目錄可自己定義,主要是add-modoule
cd /usr/local/nginx-1.14.0
#添加模塊
./configure \
--prefix=/usr/local/nginx \
--add-module=/usr/local/fastdfs-nginx-module/src
#在nginx1.14.0目錄下執(zhí)行
make && make install
vi /usr/local/nginx/conf/nginx.conf
#======添加配置===========
server {
listen 80;
server_name 192.168.33.111;
location /group1/M00/ {
root /home/fdfs_storage/data;
ngx_fastdfs_module;
}
}
/usr/local/nginx/sbin/nginx
#檢查是否啟動成功,不成功就去查看錯誤日志/var/log/nginx/error.log
ps -ef | grep nginx
systemctl stop firewalld.service
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /download/123.jpg
example file url: http://192.168.33.111/group1/M00/00/00/wKghb1vqf7GAIdYqAAJo1aj_C9s003_big.jpg
http://1192.168.33.111/group1/M00/00/00/wKghb1vqf7GAIdYqAAJo1aj_C9s003_big.jpg
#查看nginx 日志可以看到訪問記錄
tail -f -n 100 /var/log/nginx/access.log
7.項目中如何使用fastdfs
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
配置文件: fdfs.conf
# 連接tracker服務器超時時長
connect_timeout = 10
# socket連接超時時長
network_timeout = 30
# 文件內(nèi)容編碼
charset = UTF-8
# tracker服務器端口
http.tracker_http_port = 80
http.anti_steal_token = no
#密碼
http.secret_key = 123456
# tracker服務器IP和端口(可以寫多個)
tracker_server = 192.168.33.111:22122
package com.zc;
import org.csource.fastdfs.*;
public class FastdfsTest {
public static void main(String[] args) throws Exception {
//1、把FastDFS提供的jar包添加到工程中
//2、初始化全局配置。加載一個配置文件。
ClientGlobal.init("fdfs.conf");
//3、創(chuàng)建一個TrackerClient對象。
TrackerClient trackerClient = new TrackerClient();
//4、創(chuàng)建一個TrackerServer對象。
TrackerServer trackerServer = trackerClient.getConnection();
//5、聲明一個StorageServer對象,null。
StorageServer storageServer = null;
//6、獲得StorageClient對象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//7、直接調(diào)用StorageClient對象方法上傳文件即可。
String[] result = storageClient.upload_file("/Users/zc21/Downloads/img.jpg", "jpg", null);
StringBuilder sb = new StringBuilder("http://192.168.33.111/");
sb.append(result[0]).append("/").append(result[1]);
System.out.println("圖片訪問地址: " + sb.toString());
}
}
參考原文地址:
作者:叩丁狼教育
鏈接:http://www.itdecent.cn/p/51146dbe51d2