目錄
一.環境準備
FastDFS是C語言開發,建議在linux上運行,本教程使用Centos7.4作爲安裝環境。
①安裝gcc
檢測是否安裝: gcc -v
如下圖所示表示已安裝
安裝FastDFS需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:yum install gcc-c++
②安裝 libevent
yum -y install libevent
③安裝 libfastcommon
libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS運行所需要的一些基礎庫。
將libfastcommonV1.0.7.tar.gz拷貝至 /usr/local/services(沒有則創建,該目錄可以自定義) 下
依次執行以下命令:
解壓:
tar -zxvf libfastcommonV1.0.7.tar.gz
刪除安裝包
rm libfastcommonV1.0.7.tar.gz
進入 libfastcommon-1.0.7 執行:
./make.sh
./make.sh install
libfastcommon安裝好後會自動將庫文件拷貝至/usr/lib64下,由於FastDFS程序引用usr/lib目錄所以需要將/usr/lib64下的庫文件libfastcommon.so拷貝至/usr/lib下。
要拷貝的文件如下:
cp /usr/lib64/libfastcommon.so /usr/lib/
二.安裝 tracker
①下載FastDFS安裝包:
可以去官網自行下載並上傳到安裝目錄,也可以用如下方式下載
進入安裝目錄例如:/usr/local/services
wget https://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.05/FastDFS_v5.05.tar.gz
將FastDFS_v5.05.tar.gz拷貝至/usr/local/services下
解壓
tar -zxvf FastDFS_v5.05.tar.gz
進入解壓的目錄
cd FastDFS
②編譯安裝
進入FastDFS執行以下命令
./make.sh
./make.sh install
安裝成功將安裝目錄下的conf下的文件拷貝到/etc/fdfs/下。
cp /usr/local/FastDFS/conf/* /etc/fdfs/
③修改配置
安裝成功後進入 /etc/fdfs 目錄,拷貝一份新的tracker配置文件:
cp tracker.conf.sample tracker.conf
//有覆蓋提示則確認
修改配置文件
base_path=/home/yuqing/fastdfs 改爲: base_path=/home/fastdfs
目錄不存在的話創建/home/fastdfs目錄 : mkdir -p /home/fastdfs
④啓動
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
如下表示成功
注意:在/home/fastdfs/ 目錄下生成兩個目錄, 一個是數據,一個是日誌;
⑤設置開機自動啓動
vim /etc/rc.d/rc.local
將運行命令行添加進文件:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
三.安裝storage
由於上面已經安裝過tracker,這裏只需要配置storage就好了
進入/etc/fdfs目錄,拷貝一份新的storage配置文件:
cp storage.conf.sample storage.conf
修改storage.conf
vim storage.conf
group_name=group1
base_path=/home/yuqing/FastDFS改爲:base_path=/home/FastDFS
store_path0=/home/yuqing/FastDFS改爲:store_path0=/home/FastDFS/fdfs_storage//沒有則創建目錄
#如果有多個掛載磁盤則定義多個store_path,如下
#store_path1=.....
#store_path2=......
tracker_server=IP:22122 #配置tracker服務器:IP
#如果有多個則配置多個tracker
tracker_server=ip:22122
啓動
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
如下表示成功
四.配置客戶端
切換目錄到 /etc/fdfs/ 拷貝一份新的client配置文件
cp client.conf.sample client.conf
vim /etc/fdfs/client.conf
修改爲如下內容
base_path=/home/fastdfs
tracker_server=IP:22122 #tracker服務器所在IP
五.測試
使用格式:
/usr/bin/fdfs_test 客戶端配置文件地址 upload 上傳文件地址
比如將/home下的圖片上傳到FastDFS中:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /usr/local/boy.jpg
結果如下表示成功
Java代碼測試:
public static void main(String[] args) throws FileNotFoundException, IOException, Exception {
// 1、加載配置文件,配置文件中的內容就是 tracker 服務的地址。
ClientGlobal.init("D:\\Develop\\eclipse-jee-mars-2-win32\\workspace\\fastDFS\\src\\main\\resources\\fdfs_client.conf");
// 2、創建一個 TrackerClient 對象。直接 new 一個。
TrackerClient trackerClient = new TrackerClient();
// 3、使用 TrackerClient 對象創建連接,獲得一個 TrackerServer 對象。
TrackerServer trackerServer = trackerClient.getConnection();
// 4、創建一個 StorageServer 的引用,值爲 null
StorageServer storageServer = null;
// 5、創建一個 StorageClient 對象,需要兩個參數 TrackerServer 對象、StorageServer 的引用
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 6、使用 StorageClient 對象上傳圖片。
//擴展名不帶“.”
String[] strings = storageClient.upload_file("E:/pic/girl.jpg", "jpg",null);
// 7、返回數組。包含組名和圖片的路徑。
for (String string : strings) {
System.out.println(string);
}
}
運行結果:
由於現在還沒有和nginx整合無法使用http下載
整合Nginx請看這裏 https://blog.csdn.net/qq_30162219/article/details/87989314