FastDHT配合FastDFS進行文件上傳去重

爲什麼寫這個

最近因爲工作需要,使用了FastDFS,這是一款國產的開源DFS軟件,但是這個軟件本身不能對重複上傳的文件進行去重,需要我們自己去處理,一種可行的方案是,在文件上傳之前進行md5校驗,把每個文件保存在數據庫中,然後進行對比,這個md5值如果在數據庫中已經存在的話,就不上傳。不過這個效率可能不怎麼高。FastDFS作者餘慶也開源了一個解決的資源,就是FastDHT了,使用這個也可以做到去重。

關於FastDHT

FastDHT is a high-performance distributed hash system (DHT) which based key value pairs. It can store mass key value pairs such as filename mapping, session data and user related data. 
FastDHT implements data replication by it’s binlog file, so it only uses the basic storage function of the Berkeley DB. 
從這段話中可以看出,FastDHT是一個高性能的分佈式哈希系統,它是基於鍵值對存儲的,而且它需要依賴於Berkeley DB作爲數據存儲的媒介,同時需要依賴於libfastcommon。 
FastDHT集羣由一個或者多個組 group組成,同組服務器上存儲的數據是相同的,數據同步只在組的服務器之間進行;組內各個服務是對等的,對數據進行存取時,可以根據 key的hash值來決定使用哪臺機器。

安裝

下載FastDHT,建議在GitHub上下載:https://github.com/happyfish100/fastdht, 
libfastcommon下載地址:https://github.com/happyfish100/libfastcommon, 
Berkeley DB的下載地址: 
http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html 
下載完畢後,就可以開始安裝了。

先安裝libfastcommon

將源碼解壓到/usr/local/src/目錄下,

[root@tracker src]# unzip libfastcommon-master.zip 
[root@tracker src]# cd libfastcommon-master
[root@tracker libfastcommon-master]# ls
HISTORY  INSTALL  libfastcommon.spec  make.sh  README  src
[root@tracker libfastcommon-master]# ./make.sh 
[root@tracker libfastcommon-master]# ./make.sh install
mkdir -p /usr/lib64
install -m 755 libfastcommon.so /usr/lib64
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_bits.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 /usr/include/fastcommon
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

安裝Berkeley DB

其實libfastcommon在安裝FastDFS的時候就會安裝了,接下來安裝db。這裏我下載的是db-6.1.19版本。

①將db-6.1.19源碼拷貝到 /usr/local/src下
②進入db-6.1.19\build_unix,
③執行../dist/configure --prefix=/usr/local/db-6.1.19
④make
⑤make install
安裝完db,會在/usr/local目錄下生成db-6.1.19/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

安裝FastDHT

①將fastdht-master源碼解壓到 /usr/local/src下,編譯之前需要先修改make.sh文件。 
在CFLAGS=’-Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I/usr/local/db-6.1.19/include/ -L/usr/local/db-6.1.19/lib/’ 
這行裏面加上斜體字部分。 
②然後,進入/usr/local/src/fastdht-master源碼目錄下,執行./make.mk 
然後執行./make.sh install。 
③最後會在/usr/local/bin生成安裝後的文件,在/etc/fdht下生成文件

root@jz-server:/usr/local/bin# cd /etc/fdht/
root@jz-server:/etc/fdht# ll
total 24
drwxr-xr-x   2 root root 4096 Apr 22 12:45 ./
drwxr-xr-x 102 root root 4096 Apr 22 12:45 ../
-rwxr-xr-x   1 root root  910 Apr 22 12:45 fdht_client.conf*
-rwxr-xr-x   1 root root 5374 Apr 22 12:45 fdhtd.conf*
-rwxr-xr-x   1 root root   76 Apr 22 12:45 fdht_servers.conf*
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

配置FastDHT

配置fdht_client.conf文件

base_path=/data/fastdht
keep_alive=1
#include /etc/fdhtd/fdht_servers.conf
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

配置fdht_servers.conf文件

vim /etc/fdhtd/fdht_servers.conf
group_count = 1
group0 = 10.10.10.81:11411
group0 = 10.10.10.82:11411
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

配置fdhtd.conf文件

vim /etc/fdht/fdhtd.conf/
port=11411
bash_path=/data/fastdht (該目錄必須是已經存在的)
cache_size = 32MB
#include /etc/fdhtd/fdht_servers.conf  -> (本行前有#表示打開,如果想關閉此選項,則應該爲##開頭)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

配置storaged.conf文件

vim /etc/fdfs/storaged.conf
#是否檢測上傳文件已經存在。如果已經存在,則建立一個索引鏈接以節省磁盤空間 
check_file_duplicate=1 
#當上個參數設定爲1時 , 在FastDHT中的命名空間
key_namespace=FastDFS 
#長連接配置選項,如果爲0則爲短連接 1爲長連接 
keep_alive=1 
#此處特別需要注意配置
#include /etc/fdht/fdht_servers.conf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

啓動

或fdhtd /etc/fdht/fdhtd.conf 
fdhtd /etc/fdht/fdhtd.conf restart

可能遇到問題:

fdhtd /etc/fdht/fdhtd.conf 
error while loading shared libraries: libdb-6.1.so: cannot open shared object file: No such file or directory

解決辦法:

# cp /usr/local/db-6.1.19/lib/libdb-6.1.so /usr/lib/
  • 1
  • 1

然後再重新啓動就可以了。 
測試一下: 
這裏寫圖片描述 
可以看到,第一次上傳的文件爲:

-rw-r--r--   1 root root 3706 Apr 22 15:24 CgoKUlcZ0cWAUpQ4AAAOepbSnR0875.png
  • 1
  • 1

第一次上傳返回的結果爲一個軟鏈接:CgoKUlcZ0cWAYmiNAAAOejexS3k075.png,之後每次重複上傳的話都是返回一個指向第一次上傳的文件的軟鏈接。也就保證了文件只保存了一份。需要說明的是,FastDFS不會返回原始文件的索引,比如這裏的CgoKUlcZ0cWAUpQ4AAAOepbSnR0875.png,即返回的全部是軟鏈接,當所有的軟鏈接都被刪除的時候,原始文件也會從FastDFS中被刪除

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章