原文地址:FastDFS 環境搭建
博客地址:http://www.extlight.comnginx
最近閒下來,整理了一下筆記,今天就分享一下 FastDFS 環境搭建吧。git
FastDFS 是用 C 語言編寫的一款開源的分佈式文件系統。FastDFS 爲互聯網量身定製,充分考慮了冗餘備份、負載均衡、線性擴容等機制,並注重高可用、高性能等指標,使用 FastDFS 很容易搭建一套高性能的文件服務器集羣提供文件上傳、下載等服務。github
FastDFS 架構包括 Tracker Server 和 Storage Server。客戶端請求 Tracker Server 進行文件上傳、下載,經過 Tracker Server 調度最終由 Storage Server 完成文件上傳和下載。web
Tracker Server 做用是負載均衡和調度,經過 Tracker Server 在文件上傳時能夠根據一些策略找到 Storage server 提供文件上傳服務。能夠將 tracker 稱爲追蹤服務器或調度服務器。vim
Storage Server 做用是文件存儲,客戶端上傳的文件最終存儲在 Storage 服務器上,Storage Server 沒有實現本身的文件系統而是利用操做系統 的文件系統來管理文件。能夠將 storage 稱爲存儲服務器。瀏覽器
本次測試使用 ip 爲 192.168.10.110 的 CentOS 7 系統的虛擬機服務器
libfastcommon 是 FastDFS 官方提供的,其包含了 FastDFS 運行所須要的一些基礎庫。架構
git clone https://github.com/happyfish100/libfastcommon.git cd libfastcommon ./make.sh ./make.sh install
git clone https://github.com/happyfish100/fastdfs.git cd fastdfs ./make.sh ./make.sh install
安裝 FastDFS 成功後,會在以下目錄中生成相應的文件。app
[root@fastdfs fdfs]# pwd /etc/fdfs [root@fastdfs fdfs]# ll total 24 -rw-r--r--. 1 root root 1461 Oct 16 22:26 client.conf.sample -rw-r--r--. 1 root root 7978 Oct 16 22:26 storage.conf.sample -rw-r--r--. 1 root root 105 Oct 16 22:26 storage_ids.conf.sample -rw-r--r--. 1 root root 7441 Oct 16 22:26 tracker.conf.sample
咱們須要將「.simple」綴後刪除,這些配置文件才能生效。負載均衡
或者進入到 fdfs/conf 目錄中,以下:
[root@fastdfs conf]# pwd /root/fastdfs/conf [root@fastdfs conf]# ll total 84 -rw-r--r--. 1 root root 23981 Oct 16 22:19 anti-steal.jpg -rw-r--r--. 1 root root 1461 Oct 16 22:19 client.conf -rw-r--r--. 1 root root 955 Oct 16 22:19 http.conf -rw-r--r--. 1 root root 31172 Oct 16 22:19 mime.types -rw-r--r--. 1 root root 7978 Oct 16 22:19 storage.conf -rw-r--r--. 1 root root 105 Oct 16 22:19 storage_ids.conf -rw-r--r--. 1 root root 7441 Oct 16 22:19 tracker.conf [root@fastdfs conf]#
該目錄也生成好 FastDFS 運行時所須要的配置文件,咱們也能夠將這些文件一併拷貝到 /etc/fdfs 中。
[root@fastdfs bin]# pwd /usr/bin [root@fastdfs bin]# ll fdfs* -rwxr-xr-x. 1 root root 317632 Oct 16 22:26 fdfs_appender_test -rwxr-xr-x. 1 root root 317408 Oct 16 22:26 fdfs_appender_test1 -rwxr-xr-x. 1 root root 304264 Oct 16 22:26 fdfs_append_file -rwxr-xr-x. 1 root root 304008 Oct 16 22:26 fdfs_crc32 -rwxr-xr-x. 1 root root 304320 Oct 16 22:26 fdfs_delete_file -rwxr-xr-x. 1 root root 305048 Oct 16 22:26 fdfs_download_file -rwxr-xr-x. 1 root root 304648 Oct 16 22:26 fdfs_file_info -rwxr-xr-x. 1 root root 322560 Oct 16 22:26 fdfs_monitor -rwxr-xr-x. 1 root root 1112112 Oct 16 22:26 fdfs_storaged -rwxr-xr-x. 1 root root 327576 Oct 16 22:26 fdfs_test -rwxr-xr-x. 1 root root 326784 Oct 16 22:26 fdfs_test1 -rwxr-xr-x. 1 root root 454944 Oct 16 22:26 fdfs_trackerd -rwxr-xr-x. 1 root root 305248 Oct 16 22:26 fdfs_upload_appender -rwxr-xr-x. 1 root root 306272 Oct 16 22:26 fdfs_upload_file
首先建立 4 個目錄:
mkdir -p /data/fastdfs/tracker mkdir -p /data/fastdfs/storage mkdir -p /data/fastdfs/client mkdir -p /data/fastdfs/tmp
這些目錄用於存放 fastDFS 服務產生的數據文件和日誌。
爲了簡化步驟,進入到 fdfs/conf 目錄,將裏邊的全部文件複製到 /etc/fdfs 中:
cp /root/fastdfs/conf/* /etc/fdfs
修改 tracker.conf 文件:vim /etc/fdfs/tracker.conf
port=22122 base_path=/data/fastdfs/tracker
啓動 Tracker Server 服務:
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
檢測啓動狀況:
[root@fastdfs fdfs]# ps -ef | grep tracker root 2136 1 0 23:55 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start root 2147 1294 0 23:55 pts/0 00:00:00 grep --color=auto tracker
修改 storage.conf 文件:vim /etc/fdfs/storage.conf
port=23000 base_path=/data/fastdfs/storage store_path0=/data/fastdfs/storage tracker_server=192.168.10.110:22122 group_name=light http.server_port=80
注意:tracker_server 中,ip 爲本機 ip,端口爲 tracker.conf 中設置的值
啓動 Storage Server 服務:
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
檢測啓動狀況:
[root@fastdfs fdfs]# ps -ef | grep storage root 2155 1 10 00:00 ? 00:00:01 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start root 2167 1294 0 00:00 pts/0 00:00:00 grep --color=auto storage
此步驟的配置是爲了測試
修改 client.conf 文件:vim /etc/fdfs/client.conf
base_path=/data/fastdfs/client tracker_server=192.168.10.110:22122
準備圖片,名爲 test.jpg
[root@fastdfs ~]# pwd /root [root@fastdfs ~]# ll total 52 drwxr-xr-x. 11 root root 270 Oct 16 22:19 fastdfs drwxr-xr-x. 6 root root 147 Oct 16 22:22 libfastcommon -rw-r--r--. 1 root root 49608 Oct 17 01:51 test.jpg
經過 fdfs 命令上傳該圖片:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /root/test.jpg
最終,圖片會被上傳到 /data/fastdfs/storage/data/ 目錄中。
運行結果以下圖:
不過目前該圖片沒法經過瀏覽器訪問,由於咱們尚未設置 web 服務器將外部請求與本地文件系統中的目錄的映射關聯。最簡單的實現方式就是整合 nginx 服務器來實現 http 訪問請求,該功能將在下篇文章介紹。