一。介紹fastdfs及原理php
FastDFS是一款輕量級的開源分佈式文件系統,它用純C語言實現,支持Linux、FreeBSD、AIX等UNIX系統。只能經過 專有API對文件進行存取訪問,不支持POSIX接口方式,不能mount使用。html
1> 原理介紹:java
服務分爲兩大部件Tracker server和Storage server Tracker server負責調度 計算最適合的存儲服務器 Storage server負責存儲數組 Storage server被分爲nginx
多個組 不一樣的文件上傳分發到不一樣的組中 同一組之間能夠造成高可用的主從備份 同一組之間的服務器之間是互爲拷貝 數據徹底相同。git
2> 運行過程:
github
》》Client詢問Tracker server ,Tracker server經過負載算法 返回一臺存儲 的Storage server;
》》Tracker server 返回的數據爲該Storage server的IP地址和端口;
》》Client直接和該Storage server創建鏈接,進行文件上傳,Storage server返回新生成的文件ID,文件上傳結束。算法
二。 安裝集羣數組
1>非集羣安裝服務器
》》模擬環境 php7
Tracker Server 192.168.58.145
Storage Server 192.168.58.146
》》下載安裝
安裝gcc make 等開發工具包
yum groupinstall "Development Tools" yum install libevent-devel.x86_64 pcre-devel.x86_64 zlib-devel.x86_64
下載 FastDFS源碼 (手工下載地址:https://sourceforge.net/projects/fastdfs/files/)
wget https://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.08/FastDFS_v5.08.tar.gz下載nginx插件(提供http訪問方式 暫時下載 )
wget https://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz解壓(tar)並執行
[root@bogon fast]# tar zxvf FastDFS_v5.08.tar.gz [root@bogon fast]# ll drwxr-xr-x 10 8980 users 4096 Feb 4 2016 FastDFS -rw-r--r-- 1 root 17510 May 4 2014 fastdfs-nginx-module_v1.16.tar.gz -rw-r--r-- 1 root root 344620 Feb 4 2016 FastDFS_v5.08.tar.gz [root@bogon FastDFS]# ./make.sh cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O -DDEBUG_FLAG -c -o ../common/fdfs_global.o ../common/fdfs_global.c -I../common -I/usr/include/fastcommon../common/fdfs_global.c:20:20: fatal error: logger.h: No such file or directory #include "logger.h"出現以上錯誤 看到錯誤後 有一行 -I../common -I/usr/include/fastcommon gcc編譯 -I表示手工指定包含的頭文件 很明顯引用了一個fastcommon的源代碼 fastcomm必須去github搜索 找到以下地址 https://github.com/happyfish100/libfastcommon
[root@bogon fast]# yum -y install git [root@bogon fast]# git clone https://github.com/happyfish100/libfastcommon.git編譯安裝 libfastcomm
[root@bogon fast]# cd libfastcommon [root@bogon libfastcommon]# ll total 32 drwxr-xr-x 2 root root 114 Sep 4 18:49 doc -rw-r--r-- 1 root root 8005 Sep 4 18:49 HISTORY -rw-r--r-- 1 root root 566 Sep 4 18:49 INSTALL -rw-r--r-- 1 root root 1607 Sep 4 18:49 libfastcommon.spec -rwxr-xr-x 1 root root 3099 Sep 4 18:49 make.sh drwxr-xr-x 2 root root 4096 Sep 4 18:49 php-fastcommon -rw-r--r-- 1 root root 2763 Sep 4 18:49 README drwxr-xr-x 3 root root 4096 Sep 4 18:49 src [root@bogon libfastcommon]# ./make.sh [root@bogon libfastcommon]# ./make.sh install mkdir -p /usr/lib64 mkdir -p /usr/lib install -m 755 libfastcommon.so /usr/lib64 install -m 755 libfastcommon.so /usr/lib #這裏發現 安裝會將源代碼拷貝到/usr/include/fastcommon mkdir -p /usr/include/fastcommon install -m 644 common_define.h hash.h chain.h logger.h base64.h shared_func.h pthread_func.h ini_file_reader.h _os_define.h sockopt.h sched_thread.h http_func.h md5.h local_ip_func.h avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h fast_timer.h process_ctrl.h fast_mblock.h connection_pool.h fast_mpool.h fast_allocator.h fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h skiplist_common.h system_info.h fast_blocked_queue.h php7_ext_wrapper.h id_generator.h char_converter.h char_convert_loader.h /usr/include/fastcommon繼續安裝fastdfs
[root@bogon fast]# cd FastDFS [root@bogon fast]# make.sh [root@bogon FastDFS]# ./make.sh install mkdir -p /usr/bin mkdir -p /etc/fdfs cp -f fdfs_trackerd /usr/bin if [ ! -f /etc/fdfs/tracker.conf.sample ]; then cp -f ../conf/tracker.conf /etc/fdfs/tracker.conf.sample; fi mkdir -p /usr/bin mkdir -p /etc/fdfs #這裏日誌顯示了建立配置文件目錄 cp -f fdfs_storaged /usr/bin if [ ! -f /etc/fdfs/storage.conf.sample ]; then cp -f ../conf/storage.conf /etc/fdfs/storage.conf.sample; fi mkdir -p /usr/bin mkdir -p /etc/fdfs mkdir -p /usr/lib6458.145啓動tracker服務器
cp fastfs源代碼/conf/tracker.conf /etc/fdfs
修改 tracker.conf (配置參考http://bbs.chinaunix.net/thread-1941456-1-1.html)
#存儲日誌和數據的根目錄 tacker通常不存儲數據 那就是日誌(先建立該目錄) base_path=/home/data #tracker默認的端口號 能夠修改 port=22122啓動
[root@bogon home]# service fdfs_trackerd start Starting fdfs_trackerd (via systemctl): [ OK ] [root@bogon home]# service fdfs_trackerd status ● fdfs_trackerd.service - LSB: FastDFS tracker server Loaded: loaded (/etc/rc.d/init.d/fdfs_trackerd) Active: active (running) since Mon 2017-09-04 19:35:05 PDT; 3s ago Docs: man:systemd-sysv-generator(8) Process: 7474 ExecStop=/etc/rc.d/init.d/fdfs_trackerd stop (code=exited, status=2) Process: 7503 ExecStart=/etc/rc.d/init.d/fdfs_trackerd start (code=exited, status=0/SUCCESS) CGroup: /system.slice/fdfs_trackerd.service └─7506 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf Sep 04 19:35:05 bogon systemd[1]: Starting LSB: FastDFS tracker server... Sep 04 19:35:05 bogon fdfs_trackerd[7503]: Starting FastDFS tracker server: Sep 04 19:35:05 bogon systemd[1]: Started LSB: FastDFS tracker server.查看 22122端口是否啓動
[root@bogon home]# netstat -nlp | grep fdfs tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 7506/fdfs_trackerd
58.146啓動storage服務器
拷貝一個完整的配置文件到 /etc/fdfs目錄
cp fastfs源代碼/conf/storage.conf /etc/fdfs
修改 storage.conf
#存儲日誌和數據的根目錄 通常存儲日誌(先建立該目錄) base_path=/home/data #storage默認的端口號 能夠修改 port=23000 #該存儲服務器所屬的組 集羣環境根據group判斷 group_name=group1 #store_path數組 從0開始 指定多個存儲路徑 若是沒有任何該配置 默認使用 base_path指定路徑 store_path0=/home/data0 # 指定tracker_server的ip和端口 tracker_server=192.168.58.145:22122 # subdir_count store_path 指定的目錄下的data目錄 建立的子目錄個數 默認是 256個 subdir_count_per_path=256
啓動並查看
[root@bogon ~]# service fdfs_storaged status ● fdfs_storaged.service - LSB: FastDFS storage server Loaded: loaded (/etc/rc.d/init.d/fdfs_storaged) Active: active (running) since Thu 2017-09-07 20:11:00 PDT; 7s ago Docs: man:systemd-sysv-generator(8) Process: 15993 ExecStart=/etc/rc.d/init.d/fdfs_storaged start (code=exited, status=0/SUCCESS) CGroup: /system.slice/fdfs_storaged.service └─15996 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf Sep 07 20:11:00 bogon systemd[1]: Starting LSB: FastDFS storage server... Sep 07 20:11:00 bogon fdfs_storaged[15993]: Starting FastDFS storage server: Sep 07 20:11:00 bogon systemd[1]: Started LSB: FastDFS storage server. [root@bogon ~]# netstat -nlp | grep fdfs tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 15996/fdfs_storaged
查看存儲生成的目錄 wc -l統計 是 256個子目錄
[root@bogon data0]# cd /home/data0 && ll && cd data && ll total 12 drwxr-xr-x 258 root root 8192 Sep 7 20:11 data total 3072 drwxr-xr-x 258 root root 8192 Sep 7 20:11 00 drwxr-xr-x 258 root root 8192 Sep 7 20:11 01 drwxr-xr-x 258 root root 8192 Sep 7 20:11 02 drwxr-xr-x 258 root root 8192 Sep 7 20:11 03
查看/usr/bin下的全部fdfs工具
[root@bogon fdfs]# ls /usr/bin | grep fdfs fdfs_appender_test fdfs_appender_test1 fdfs_append_file fdfs_crc32 fdfs_delete_file fdfs_download_file fdfs_file_info fdfs_monitor fdfs_storaged fdfs_test fdfs_test1 fdfs_trackerd fdfs_upload_appender fdfs_upload_file上傳文件
上傳文件時 必須經過trackerserver 來獲取合適的storageserver 因此須要指定trackerserver 須要添加一個client.conf配置文件
[root@bogon fdfs]# vi client.conf tracker_server=192.168.58.145:22122關閉 145和146防火牆
service iptables stop iptables --flush戶端上傳文件後存儲服務器將文件ID返回給客戶端,此文件ID用於之後訪問該文件的索引信息。
文件索引信息包括:組名,虛擬磁盤路徑,數據兩級目錄,文件名。示例:group1/M00/02/44/sdfgsinsdfsdf.jpg
[root@bogon ~]# fdfs_upload_file /etc/fdfs/client.conf ~/a.txt
group1/M00/00/00/wKg6klmyEG-AWCZjAAAADZDfcRo628.txt
查看文件信息(fdfs_file_info <config_file> <file_id>)
[root@bogon 00]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKg6klmyEG-AWCZjAAAADZDfcRo628.txt source storage id: 0 source ip address: 192.168.58.146 file create timestamp: 2017-09-07 20:37:19 file size: 13 file crc32: 2430562586 (0x90DF711A)下載文件到本地(fdfs_download_file <config_file> <file_id> [local_filename])
[root@bogon 00]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg6klmyEG-AWCZjAAAADZDfcRo628.txt ~/bb.txt [root@bogon 00]# ll ~ total 168836 -rw-------. 1 root root 2913 Apr 5 2016 anaconda-ks.cfg -rw-r--r-- 1 root root 13 Sep 7 20:35 a.txt -rw-r--r-- 1 root root 13 Sep 7 20:46 bb.txt刪除文件(fdfs_delete_file <config_file> <file_id>)
[root@bogon 00]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKg6klmyEG-AWCZjAAAADZDfcRo628.txt #刪除了該文件 還能夠查看到它的信息 [root@bogon 00]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKg6klmyEG-AWCZjAAAADZDfcRo628.txt source storage id: 0 source ip address: 192.168.58.146 file create timestamp: 2017-09-07 20:37:19 file size: 13 file crc32: 2430562586 (0x90DF711A) #再次嘗試刪除 發現報錯文件不存在 [root@bogon 00]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKg6klmyEG-AWCZjAAAADZDfcRo628.txt [2017-09-07 20:48:23] ERROR - file: tracker_proto.c, line: 48, server: 192.168.58.146:23000, response status 2 != 0 delete file fail, error no: 2, error info: No such file or directory2>集羣安裝
參考(https://www.2cto.com/kf/201611/560911.html)
三。java調用
下載 fastdfsjava客戶端的jar包 (https://sourceforge.net/projects/fastdfs/files/Java%20Client%20API%20Library/)
新建一個和上傳測試同樣的client.conf文件
上傳代碼:
ClientGlobal.init("client.conf"); TrackerServer connection = new TrackerClient().getConnection(); StorageClient client=new StorageClient(connection,null); String[] uploadFile = client.upload_file("c:/無標題.png","png",null); for(String a:uploadFile){ System.out.println(a); }返回數組 返回兩個參數 第一個是存儲的組名 第二是 圖片的路徑
group1
M00/00/00/wKg6klmyN46AYa-LAAOU-yTEDcU298.png
下載代碼:
ClientGlobal.init("client.conf"); TrackerServer connection = new TrackerClient().getConnection(); StorageClient client=new StorageClient(connection,null); byte[] bt=client.download_file("group1", "M00/00/00/wKg6klmyN46AYa-LAAOU-yTEDcU298.png"); FileOutputStream fis=new FileOutputStream("c:/a.png"); fis.write(bt); fis.close();其餘相關文檔 參考http://elf8848.iteye.com/blog/1739596