1.緒論
最近要用到fastDFS,所以自己研究了一下,在搭建FastDFS的過程中遇到過很多的問題,爲了能幫忙到以後搭建FastDFS的同學,少走彎路,與大家分享一下。FastDFS的作者淘寶資深架構餘慶,這個優秀的輕量及的分佈式文件系統的開源沒多久,立馬就火了。由於篇幅較大,本博文共四篇,第一篇主要介紹FastDFS,下載相關軟件包,爲搭建做好準備。第二篇會講到在CentOS下搭建FastDFS的詳細過程。第三篇,會講到整合nginx詳細內容。第四篇主要講FastDFS在怎麼用fastdfs-client-java連接,並結合實例。
2.應用場景
FastDFS是爲互聯網應用量身定做的一套分佈式文件存儲系統,非常適合用來存儲用戶圖片、視頻、文檔等文件。對於互聯網應用,和其他分佈式文件系統相比,優勢非常明顯。其中有好幾家是做網盤的公司。其中存儲量最大的一家,集羣中存儲group數有400個,存儲服務器超過800臺,存儲容量達到6PB,文件數超過1億,Group持續增長中。
以下是使用FastDFS的用戶列表:
UC (http://www.uc.cn/,存儲容量超過10TB)
支付寶(http://www.alipay.com/)
京東商城(http://www.360buy.com/)
淘淘搜(http://www.taotaosou.com/)
飛信(http://feixin.1008.cn/)
趕集網(http://www.ganji.com/)
淘米網(http://www.61.com/)
迅雷(http://www.xunlei.com/)
螞蜂窩(http://www.mafengwo.cn/)
丫丫網(http://www.iyaya.com/)
虹網(http://3g.ahong.com)
5173(http://www.5173.com/)
華夏原創網(http://www.yuanchuang.com/)
華師京城教育雲平臺(http://www.hsjdy.com.cn/)
視友網(http://www.cuctv.com/)
搜道網(http://www.sodao.com/)
58同城(http://www.58.com/)
商務聯盟網(http://www.biz72.com/)
中青網(http://www.youth.cn/)
繽麗網 (http://www.binliy.com/)
飛視雲視頻(http://www.freeovp.com/)
夢芭莎(http://www.moonbasa.com/)
活動幫(http://www.eventsboom.com)
51CTO(http://www.51cto.com/)
搜房網(http://www.soufun.com/)
3.詳細介紹
FastDFS是一個開源的分佈式文件系統,她對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題。特別適合以文件爲載體的在線服務,如相冊網站、視頻網站等等。
FastDFS服務端有兩個角色:跟蹤器(tracker)和存儲節點(storage)。跟蹤器主要做調度工作,在訪問上起負載均衡的作用。
存儲節點存儲文件,完成文件管理的所有功能:存儲、同步和提供存取接口,FastDFS同時對文件的meta data進行管理。所謂文件的meta data就是文件的相關屬性,以鍵值對(key value pair)方式表示,如:width=1024,其中的key爲width,value爲1024。文件meta data是文件屬性列表,可以包含多個鍵值對。
FastDFS系統結構如下圖所示:
跟蹤器和存儲節點都可以由一臺多臺服務器構成。跟蹤器和存儲節點中的服務器均可以隨時增加或下線而不會影響線上服務。其中跟蹤器中的所有服務器都是對等的,可以根據服務器的壓力情況隨時增加或減少。
爲了支持大容量,存儲節點(服務器)採用了分卷(或分組)的組織方式。存儲系統由一個或多個卷組成,卷與卷之間的文件是相互獨立的,所有卷 的文件容量累加就是整個存儲系統中的文件容量。一個卷可以由一臺或多臺存儲服務器組成,一個卷下的存儲服務器中的文件都是相同的,卷中的多臺存儲服務器起 到了冗餘備份和負載均衡的作用。
在卷中增加服務器時,同步已有的文件由系統自動完成,同步完成後,系統自動將新增服務器切換到線上提供服務。
當存儲空間不足或即將耗盡時,可以動態添加捲。只需要增加一臺或多臺服務器,並將它們配置爲一個新的卷,這樣就擴大了存儲系統的容量。
FastDFS中的文件標識分爲兩個部分:卷名和文件名,者缺一不可。
3.1 上傳文件交互過程:
-
- client詢問tracker上傳到的storage,不需要附加參數;
- tracker返回一臺可用的storage;
- client直接和storage通訊完成文件上傳
3.2 下載文件交互過程:
- client詢問tracker下載文件的storage,參數爲文件標識(卷名和文件名);
- tracker返回一臺可用的storage;
- client直接和storage通訊完成文件下載。
4.FastDFS搭建工具下載
作者的GitHub地址:https://github.com/happyfish100
這次搭建的所有工具,都可以在上面下載到。我搭建的是目前最新版本Version 5.11 2017-05-26。作者還有一個5.10的發行版本,你如果沒有下載最近的,到時候整合nginx的時候可能會遇到:
local/fastdfs-nginx-module/src/common.c:1245: 錯誤:‘FDFSHTTPParams’沒有名爲‘support_multi_range’的成員
make[1]: *** [objs/addon/src/ngx_http_fastdfs_module.o] 錯誤 1
make[1]: Leaving directory `/usr/local/nginx-1.10.1'
遇到這個錯誤的原因是,在fastdfs-nginx-module的HISTORY中你可以到:
整合的時候,fastdfs-nginx-module中的support_multi_range在Version 5.10中找不到。
Version 5.11對應的fastdfs-nginx-module的Version 1.20
Version 5.10對應的fastdfs-nginx-module的Version 1.19
之所以在安裝前寫了這麼一段話,是因爲這個很重要,版本不對應會給接下來的安裝帶來各種問題。
把源碼下載下來3個zip包,再去下個nginx:
下面開始安裝
1.1 安裝libfastcommon
安裝成功後解壓libfastcommon-master.zip
unzip libfastcommon-master.zip
[root@sybmfw12-lnsy libfastcommon-1.0.36]# ll drwxr-xr-x. 2 root root 4096 4月 5 2017 doc -rw-r--r--. 1 root root 8005 4月 5 2017 HISTORY -rw-r--r--. 1 root root 566 4月 5 2017 INSTALL -rw-r--r--. 1 root root 1606 4月 5 2017 libfastcommon.spec -rwxr-xr-x. 1 root root 3099 4月 5 2017 make.sh drwxr-xr-x. 2 root root 4096 4月 5 2017 php-fastcommon -rw-r--r--. 1 root root 2763 4月 5 2017 README drwxr-xr-x. 3 root root 4096 11月 8 12:30 src
當我們./make.sh的時候可能會提示,gcc命令沒有找到。
用yum安裝gcc:
yum -y install gcc-c++
這個時候分別執行./make.sh和./make.sh install,正常情況是可以成功的。
libfastcommon默認會被安裝到/usr/lib64/libfastcommon.so但是FastDFS的主程序卻在/usr/local/lib目錄下
這個時候我們就要建立一個軟鏈接了,實際上也相當於windows上的快捷方式。
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
1.2安裝FastDFS
解壓FastDFS安裝包
unzip fastdfs-5.11.zip
解壓後看到:
[root@sybmfw12-lnsy fastdfs-5.11]# ll 總用量 136 drwxr-xr-x. 3 root root 4096 11月 8 12:32 client drwxr-xr-x. 2 root root 4096 11月 8 12:32 common drwxr-xr-x. 2 root root 4096 6月 3 21:09 conf -rw-r--r--. 1 root root 35067 6月 3 21:09 COPYING-3_0.txt -rw-r--r--. 1 root root 3171 6月 3 21:09 fastdfs.spec -rw-r--r--. 1 root root 33100 6月 3 21:09 HISTORY drwxr-xr-x. 2 root root 4096 6月 3 21:09 init.d -rw-r--r--. 1 root root 7755 6月 3 21:09 INSTALL -rwxr-xr-x. 1 root root 5548 6月 3 21:09 make.sh drwxr-xr-x. 2 root root 4096 6月 3 21:09 php_client -rw-r--r--. 1 root root 2380 6月 3 21:09 README.md -rwxr-xr-x. 1 root root 1768 6月 3 21:09 restart.sh -rwxr-xr-x. 1 root root 1680 6月 3 21:09 stop.sh drwxr-xr-x. 4 root root 4096 11月 8 12:32 storage drwxr-xr-x. 2 root root 4096 6月 3 21:09 test drwxr-xr-x. 2 root root 4096 11月 8 12:32 track
進到剛解壓的目錄:
cd fastdfs-5.11 ./make.sh ./make.sh install
如果沒有報錯那麼就成功了。安裝log中會提示FastDFS安裝到了/etc/fdfs目錄下。
成功後查看安裝目錄:
[root@sybmfw12-lnsy fastdfs-5.11]# ll /etc/fdfs/ -rw-r--r--. 1 root root 1463 11月 8 12:52 client.conf.sample -rw-r--r--. 1 root root 7938 11月 8 14:55 storage.conf.sample -rw-r--r--. 1 root root 7397 11月 8 12:36 tracker.conf.sample
我們需要把這三個示例文件複製一份,去掉.sample
cp client.conf.sample client.conf cp storage.conf.sample storage.conf cp tracker.conf.sample tracker.conf
FastDFS安裝結束。
1.3安裝tracker
1.3.1創建tracker工作目錄
這個目錄可以自定義,用來保存tracker的data和log
根據個人習慣,我創建了下面的目錄:
mkdir /opt/fastdfs_tracker
1.3.2 配置tracker
vim /etc/fdfs/tracker.conf 打開後重點關注下面4個配置: 1.disabled=false 2.port=22122 #默認端口號 3.base_path=/opt/fastdfs_tracker #我剛剛創建的目錄 4.http.server_port=8080 #默認端口是8080 5.bind_addr= 0.0.0.0 監聽地址
修改完的文件爲:
disabled=false bind_addr= 0.0.0.0 port=22122 connect_timeout=30 network_timeout=60 base_path=/opt/fastdfs_tracker max_connections=512 accept_threads=1 work_threads=4 min_buff_size = 8KB max_buff_size = 128KB store_lookup=2 store_group=group2 store_server=0 store_path=0 download_server=0 reserved_storage_space = 10% log_level=info run_by_group= run_by_user= allow_hosts=* sync_log_buff_interval = 10 check_active_interval = 120 thread_stack_size = 64KB storage_ip_changed_auto_adjust = true storage_sync_file_max_delay = 86400 storage_sync_file_max_time = 300 use_trunk_file = false slot_min_size = 256 slot_max_size = 16MB trunk_file_size = 64MB trunk_create_file_advance = false trunk_create_file_time_base = 02:00 trunk_create_file_interval = 86400 trunk_create_file_space_threshold = 20G trunk_init_check_occupying = false trunk_init_reload_from_binlog = false trunk_compress_binlog_min_interval = 0 use_storage_id = false storage_ids_filename = storage_ids.conf id_type_in_filename = ip store_slave_file_use_link = false rotate_error_log = false error_log_rotate_time=00:00 rotate_error_log_size = 0 log_file_keep_days = 0 use_connection_pool = false connection_pool_max_idle_time = 3600 http.server_port=8080 http.check_alive_interval=30 http.check_alive_type=tcp http.check_alive_uri=/status.html
1.3.3啓動tracker
保存配置後啓動tracker,命令如下:
service fdfs_trackerd start
成功後應該可以看到:
[root@sybmfw12-lnsy fastdfs-5.11]#service fdfs_trackerd start Starting fdfs_trackerd (via systemctl): [ OK ]
進行剛剛創建的tracker目錄,發現目錄中多了data和log兩個目錄
[root@sybmfw12-lnsy fastdfs-5.11]# ll /opt/fastdfs_tracker/ drwxr-xr-x. 2 root root 4096 11月 8 15:01 data drwxr-xr-x. 2 root root 4096 11月 8 12:37 logs
最後我們需要給tracker加入開機啓動
echo "service fdfs_trackerd start" |tee -a /etc/rc.d/rc.local
查看一下tracker的端口監聽情況
[root@sybmfw12-lnsy fastdfs-5.11]# netstat -unltp|grep fdfs tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 70382/fdfs_trackerd
端口22122成功監聽。
1.4 安裝storage
storage的安裝與tracker很類似。
1.4.1 爲storage配置工作目錄
與tracker不同的是,由於storage還需要一個目錄用來存儲數據,所以我另外多建了兩個目錄fastdfs_storage_data,fastdfs_storage
下面是我的目錄結構:
[root@sybmfw12-lnsy fastdfs-5.11]# ll /opt/ drwxr-xr-x. 4 root root 4096 11月 8 12:48 fastdfs_storage drwxr-xr-x. 3 root root 4096 11月 8 15:02 fastdfs_storage_data drwxr-xr-x. 4 root root 4096 11月 8 12:37 fastdfs_tracker
1.4.2 修改storage配置文件
修改storage.conf
1.disabled=false 2.group_name=group1 #組名,根據實際情況修改 3.port=23000 #設置storage的端口號,默認是23000,同一個組的storage端口號必須一致 4.base_path=/opt/fastdfs_storage #設置storage數據文件和日誌目錄 5.store_path_count=1 #存儲路徑個數,需要和store_path個數匹配 6.store_path0=/opt/fastdfs_storage_data #實際文件存儲路徑 7.tracker_server=172.20.132.57:22122 #我CentOS7的ip地址 8.http.server_port=8888 #設置 http 端口號
修改完的文件爲:
disabled=false group_name=group1 bind_addr= 0.0.0.0 client_bind=true port=23000 connect_timeout=30 network_timeout=60 heart_beat_interval=30 stat_report_interval=60 base_path=/opt/fastdfs_storage max_connections=256 buff_size = 256KB accept_threads=1 work_threads=4 disk_rw_separated = true disk_reader_threads = 1 disk_writer_threads = 1 sync_wait_msec=50 sync_interval=0 sync_start_time=00:00 sync_end_time=23:59 write_mark_file_freq=500 store_path_count=1 store_path0=/opt/fastdfs_storage_data subdir_count_per_path=256 tracker_server=172.20.132.57:22122 log_level=info run_by_group= run_by_user= allow_hosts=* file_distribute_path_mode=0 file_distribute_rotate_count=100 fsync_after_written_bytes=0 sync_log_buff_interval=10 sync_binlog_buff_interval=10 sync_stat_file_interval=300 thread_stack_size=512KB upload_priority=10 if_alias_prefix= check_file_duplicate=0 file_signature_method=hash key_namespace=FastDFS keep_alive=0 use_access_log = false rotate_access_log = false access_log_rotate_time=00:00 rotate_error_log = false error_log_rotate_time=00:00 rotate_access_log_size = 0 rotate_error_log_size = 0 log_file_keep_days = 0 file_sync_skip_invalid_record=false use_connection_pool = false connection_pool_max_idle_time = 3600 http.domain_name= http.server_port=8888
修改保存後創建軟引用
ln -s /usr/bin/fdfs_storaged /usr/local/bin
1.4.3 啓動storage
service fdfs_storaged start Starting fdfs_storaged (via systemctl): [ OK ]
同樣的,設置開機啓動:
修改rc.local
echo "service fdfs_storaged start" |tee -a /etc/rc.d/rc.local
查看一下服務是否啓動
[root@sybmfw12-lnsy fastdfs-5.11]#netstat -unltp | grep fdfs tcp 0 0 0.0.0.0:22122 0.0.0.0:* LISTEN 2231/fdfs_trackerd tcp 0 0 0.0.0.0:23000 0.0.0.0:* LISTEN 2323/fdfs_storaged
服務已正常啓動。
1.4.4 校驗整合
到這裏,fastdfs的東西都已安裝完成,最後我們還要確定一下,storage是否註冊到了tracker中去。
查看命令:
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
成功後可以看到:
ip_addr = 172.20.132.57 (localhost.localdomain) ACTIVE
[root@sybmfw12-lnsy fastdfs-5.11]# /usr/bin/fdfs_monitor /etc/fdfs/storage.conf [2017-11-08 18:05:55] DEBUG - base_path=/opt/fastdfs_storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0 server_count=1, server_index=0 tracker server is 172.20.132.57:22122 group count: 1 Group 1: group name = group1 disk total space = 34140 MB disk free space = 29282 MB trunk free space = 0 MB storage server count = 1 active server count = 1 storage server port = 23000 storage HTTP port = 8888 store path count = 1 subdir count per path = 256 current write server index = 0 current trunk file id = 0 Storage 1: id = 172.20.132.57 ip_addr = 172.20.132.57 (sybmfw12-lnsy) ACTIVE http domain = version = 5.11 join time = 2017-11-08 12:48:05 up time = 2017-11-08 15:02:04 total storage = 34140 MB free storage = 29282 MB upload priority = 10 store_path_count = 1 subdir_count_per_path = 256 storage_port = 23000 storage_http_port = 8888 current_write_path = 0 source storage id = if_trunk_server = 0 connection.alloc_count = 256 connection.current_count = 0 connection.max_count = 1 total_upload_count = 2 success_upload_count = 2 total_append_count = 0 success_append_count = 0 total_modify_count = 0 success_modify_count = 0 total_truncate_count = 0 success_truncate_count = 0 total_set_meta_count = 0 success_set_meta_count = 0 total_delete_count = 0 success_delete_count = 0 total_download_count = 0 success_download_count = 0 total_get_meta_count = 0 success_get_meta_count = 0 total_create_link_count = 0 success_create_link_count = 0 total_delete_link_count = 0 success_delete_link_count = 0 total_upload_bytes = 179368 success_upload_bytes = 179368 total_append_bytes = 0 success_append_bytes = 0 total_modify_bytes = 0 success_modify_bytes = 0 stotal_download_bytes = 0 success_download_bytes = 0 total_sync_in_bytes = 0 success_sync_in_bytes = 0 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 2 success_file_open_count = 2 total_file_read_count = 0 success_file_read_count = 0 total_file_write_count = 2 success_file_write_count = 2 last_heart_beat_time = 2017-11-08 18:05:37 last_source_update = 2017-11-08 15:24:21 last_sync_update = 1970-01-01 08:00:00 last_synced_timestamp = 1970-01-01 08:00:00
1.測試
前面兩篇博文已對FastDFS的安裝和配置,做了比較詳細的講解。FastDFS的基礎模塊都搭好了,現在開始測試下載。
1.1 配置客戶端
同樣的,需要修改客戶端的配置文件:
/etc/fdfs/client.conf base_path=/opt/fastdfs_tracker #tracker服務器文件路徑 tracker_server=172.20.132.57:22122 #tracker服務器IP地址和端口號 http.tracker_server_port=8080 # tracker 服務器的 http端口號,必須和tracker的設置對應起來
修改完成的文件爲:
connect_timeout=30 network_timeout=60 base_path=/opt/fastdfs_tracker tracker_server=172.20.132.57:22122 log_level=info use_connection_pool = false connection_pool_max_idle_time = 3600 load_fdfs_parameters_from_tracker=false use_storage_id = false storage_ids_filename = storage_ids.conf http.tracker_server_port=8080
View Code
1.2 模擬上傳
確定圖片位置後,我們輸入上傳圖片命令:
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/1.jpg #這後面放的是圖片的位置
成功後會返回圖片的路徑:
[root@sybmfw12-lnsy fastdfs-5.11]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/2.png group1/M00/00/00/rBSEOVoC2QCAJVdoAAFeVJMpopw987.png
組名:group1
磁盤:M00
目錄:00/00
文件名稱:rBSEOVoC2QCAJVdoAAFeVJMpopw987.png
我們上傳的圖片會被上傳到我們創建的storage_data目錄下,讓我們去看看:
[root@sybmfw12-lnsy ~]# cd /opt/fastdfs_storage_data/data/ [root@sybmfw12-lnsy data]# ls 00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C 40 44 48 4C 50 54 58 5C 60 64 68 6C 70 74 78 7C 80 84 88 8C 90 94 98 9C A0 A4 A8 AC B0 B4 B8 BC C0 C4 C8 CC D0 D4 D8 DC E0 E4 E8 EC F0 F4 F8 FC 01 05 09 0D 11 15 19 1D 21 25 29 2D 31 35 39 3D 41 45 49 4D 51 55 59 5D 61 65 69 6D 71 75 79 7D 81 85 89 8D 91 95 99 9D A1 A5 A9 AD B1 B5 B9 BD C1 C5 C9 CD D1 D5 D9 DD E1 E5 E9 ED F1 F5 F9 FD 02 06 0A 0E 12 16 1A 1E 22 26 2A 2E 32 36 3A 3E 42 46 4A 4E 52 56 5A 5E 62 66 6A 6E 72 76 7A 7E 82 86 8A 8E 92 96 9A 9E A2 A6 AA AE B2 B6 BA BE C2 C6 CA CE D2 D6 DA DE E2 E6 EA EE F2 F6 FA FE 03 07 0B 0F 13 17 1B 1F 23 27 2B 2F 33 37 3B 3F 43 47 4B 4F 53 57 5B 5F 63 67 6B 6F 73 77 7B 7F 83 87 8B 8F 93 97 9B 9F A3 A7 AB AF B3 B7 BB BF C3 C7 CB CF D3 D7 DB DF E3 E7 EB EF F3 F7 FB FF
[root@sybmfw12-lnsy data]# ls 00/
00 04 08 0C 10 14 18 1C 20 24 28 2C 30 34 38 3C 40 44 48 4C 50 54 58 5C 60 64 68 6C 70 74 78 7C 80 84 88 8C 90 94 98 9C A0 A4 A8 AC B0 B4 B8 BC C0 C4 C8 CC D0 D4 D8 DC E0 E4 E8 EC F0 F4 F8 FC
01 05 09 0D 11 15 19 1D 21 25 29 2D 31 35 39 3D 41 45 49 4D 51 55 59 5D 61 65 69 6D 71 75 79 7D 81 85 89 8D 91 95 99 9D A1 A5 A9 AD B1 B5 B9 BD C1 C5 C9 CD D1 D5 D9 DD E1 E5 E9 ED F1 F5 F9 FD
02 06 0A 0E 12 16 1A 1E 22 26 2A 2E 32 36 3A 3E 42 46 4A 4E 52 56 5A 5E 62 66 6A 6E 72 76 7A 7E 82 86 8A 8E 92 96 9A 9E A2 A6 AA AE B2 B6 BA BE C2 C6 CA CE D2 D6 DA DE E2 E6 EA EE F2 F6 FA FE
03 07 0B 0F 13 17 1B 1F 23 27 2B 2F 33 37 3B 3F 43 47 4B 4F 53 57 5B 5F 63 67 6B 6F 73 77 7B 7F 83 87 8B 8F 93 97 9B 9F A3 A7 AB AF B3 B7 BB BF C3 C7 CB CF D3 D7 DB DF E3 E7 EB EF F3 F7 FB FF
[root@sybmfw12-lnsy data]# ls 00/00/
rBSEOVoC2QCAJVdoAAFeVJMpopw987.png
果然通過剛剛返回的路徑,我們成功找到了圖片。
我們仔細看一下,實際文件存儲路徑下有創建好的多級目錄。data下有256個1級目錄,每級目錄下又有256個2級子目錄,總共65536個文件,新寫的文件會以hash的方式被路由到其中某個子目錄下,然後將文件數據直接作爲一個本地文件存儲到該目錄中。
如果要訪問剛上傳的圖片,我們得需要結合nginx來實現
1.4 HTTP請求不能訪問文件的原因
我們在使用FastDFS部署一個分佈式文件系統的時候,通過FastDFS的客戶端API來進行文件的上傳、下載、刪除等操作。同時通過FastDFS的HTTP服務器來提供HTTP服務。但是FastDFS的HTTP服務較爲簡單,無法提供負載均衡等高性能的服務,所以FastDFS的開發者——淘寶的架構師餘慶同學,爲我們提供了Nginx上使用的FastDFS模塊(也可以叫FastDFS的Nginx模塊)。
FastDFS通過Tracker服務器,將文件放在Storage服務器存儲,但是同組之間的服務器需要複製文件,有延遲的問題.假設Tracker服務器將文件上傳到了172.20.132.57,文件ID已經返回客戶端,這時,後臺會將這個文件複製到172.20.132.57,如果複製沒有完成,客戶端就用這個ID在172.20.132.57取文件,肯定會出現錯誤。這個fastdfs-nginx-module可以重定向連接到源服務器取文件,避免客戶端由於複製延遲的問題,出現錯誤。
正是這樣,FastDFS需要結合nginx,所以取消原來對HTTP的直接支持。
2. FastDFS的nginx模塊安裝
在安裝nginx之前要安裝nginx所需的依賴lib:
yum -y install pcre pcre-devel yum -y install zlib zlib-devel yum -y install openssl openssl-devel
2.2 安裝nginx並添加fastdfs-nginx-module
解壓nginx,和fastdfs-nginx-module:
tar -zxvf nginx-1.10.3.tar.gz unzip fastdfs-nginx-module-master.zip
解壓後進入nginx目錄編譯安裝nginx,並添加fastdfs-nginx-module:
./configure --prefix=/usr/local/nginx --add-module=/root/fastdfs-nginx-module-master/src #解壓後fastdfs-nginx-module所在的位置
如果配置不報錯的話,就開始編譯:
make make install
如果報錯的話,很可能是版本的原因,在我的第二篇博文中提供了我測試成功不報錯的版本下載。
nginx的默認目錄是/usr/local/nginx,安裝成功後查看:
[root@sybmfw12-lnsy ~]# ll /usr/local/nginx/ drwx------. 2 nobody root 4096 11月 8 13:10 client_body_temp drwxr-xr-x. 2 root root 4096 11月 8 15:22 conf drwx------. 2 nobody root 4096 11月 8 13:10 fastcgi_temp drwxr-xr-x. 2 root root 4096 11月 8 13:00 html drwxr-xr-x. 2 root root 4096 11月 8 13:10 logs drwx------. 2 nobody root 4096 11月 8 13:10 proxy_temp drwxr-xr-x. 2 root root 4096 11月 8 13:00 sbin drwx------. 2 nobody root 4096 11月 8 13:10 scgi_temp drwx------. 2 nobody root 4096 11月 8 13:10 uwsgi_temp
2.3 配置storage nginx
修改nginx.conf:
修改監聽端口 listen 9999, 新增location
server { listen 9991; server_name localhost; location / { root html; index index.html index.htm; } location ~/group1/M00 { root /opt/fastdfs_storage/data; ngx_fastdfs_module; } location = /50x.html { root html; } }
View Code
然後進入FastDFS安裝時的解壓過的目錄,將http.conf和mime.types拷貝到/etc/fdfs目錄下:
[root@sybmfw12-lnsy conf]# cp http.conf mime.types /etc/fdfs/
[root@sybmfw12-lnsy conf]# cp mime.types /etc/fdfs/
另外還需要把fastdfs-nginx-module安裝目錄中src目錄下的mod_fastdfs.conf也拷貝到/etc/fdfs目錄下:
[root@sybmfw12-lnsy src]# cp mod_fastdfs.conf /etc/fdfs/
對剛剛拷貝的mod_fastdfs.conf文件進行修改:
/etc/fdfs/mod_fastdfs.conf base_path=/opt/fastdfs_storage #保存日誌目錄 tracker_server=172.20.132.57:22122 #tracker服務器的IP地址以及端口號 storage_server_port=23000 #storage服務器的端口號 url_have_group_name = true #文件 url 中是否有 group 名 store_path0=/opt/fastdfs_storage_data #存儲路徑 group_count = 1 #設置組的個數
在文件的最後,設置group
[group1] group_name=group1 storage_server_port=23000 store_path_count=1 store_path0=/opt/fastdfs_storage_data
創建M00至storage存儲目錄的符號連接:
ln -s /opt/fastdfs_storage_data/data/ /opt/fastdfs_storage_data/data/M00
啓動nginx:
/usr/local/nginx/sbin/nginx ngx_http_fastdfs_set pid=8142
[root@sybmfw12-lnsy src]# curl localhost:9991 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
恭喜你,storage的nginx已配置成功。接下來,我們還要繼續配置tracker的nginx。
2.4 配置tracker nginx
在nginx.cfg裏在添加一個虛擬主機
upstream fdfs_group1 { server 127.0.0.1:9991; } server { listen 80; server_name localhost; location /group1/M00 { proxy_pass http://fdfs_group1; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
重啓nginx
/usr/local/nginx/sbin/nginx -s reload ngx_http_fastdfs_set pid=8141
[root@sybmfw12-lnsy ~]# curl localhost <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
View Code
2.6 HTTP測試
可以成功訪問。
現在我們再去訪問一下,原來我們上傳過的文件:
172.20.132.57/group1/M00/00/00/rBSEOVoCsSWAZG_WAAFeVJMpopw658.png
這個時候已經能成功訪問。感謝你能堅持到現在,FastDFS已部署完成。
貼上完整nginx配置
http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 9991; server_name localhost; location / { root html; index index.html index.htm; } location ~/group1/M00 { root /opt/data/fastdfs_storage/data; ngx_fastdfs_module; } location = /50x.html { root html; } } upstream fdfs_group1 { server 127.0.0.1:9991; } server { listen 80; server_name localhost; location /group1/M00 { proxy_pass http://fdfs_group1; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }