在這裏,按如下配置fdfs的集羣:php
跟蹤服務器:
Tracker Server1 IP: 192.168.50.232
Tracker Server2 IP: 192.168.50.233
存儲服務器:
Storage Server1 IP: 192.168.50.232
Storage Server2 IP: 192.168.50.233html
1、在各自的機器上安裝fdfs,主要步驟以下nginx
安裝libfastcommongit
sudo wget https://github.com/happyfish100/libfastcommon/archive/master.zip sudo unzip master.zip sudo ./make.sh sudo ./make.sh install
安裝FastFDSgithub
sudo wget https://github.com/happyfish100/fastdfs/archive/master.zip sudo unzip master.zip cd fastdfs-master sudo ./make.sh sudo ./make.sh install
2、配置fdfsweb
1.修改tracker.conf的配置(/etc/fdfs/tracker.conf)
在192.168.50.232的機器上修改tracker.conf的配置以下:
bind_addr=192.168.50.232
base_path=/home/data/fastdfs 該目錄根據實際須要自行修改
use_storage_id = true
id_type_in_filename = id
http.server_port=8088 端口可根據實際須要自行修改服務器
在192.168.50.233的機器上修改tracker.conf的配置以下:
bind_addr=192.168.50.233
base_path=/home/data/fastdfs 該目錄根據實際須要自行修改
use_storage_id = true
id_type_in_filename = id
http.server_port=8088 端口可根據實際須要自行修改app
2.修改storage.conf的配置(/etc/fdfs/storage.conf)
在192.168.50.232的機器上修改tracker.conf的配置以下:
bind_addr=192.168.50.232
base_path=/home/data/fastdfs 該目錄根據實際須要自行修改
store_path0=/home/data/fastdfs
tracker_server=192.168.50.232:22122
tracker_server=192.168.50.233:22122
http.server_port=8088 端口可根據實際須要自行修改post
在192.168.50.233的機器上修改tracker.conf的配置以下:
bind_addr=192.168.50.233
base_path=/home/data/fastdfs 該目錄根據實際須要自行修改
store_path0=/home/data/fastdfs
tracker_server=192.168.50.232:22122
tracker_server=192.168.50.233:22122
http.server_port=8088 端口可根據實際須要自行修改測試
3.修改storage_ids.conf的配置(/etc/fdfs/storage_ids.conf)都配置成同樣便可
100001 group1 192.168.50.232
100002 group1 192.168.50.233
4.啓動服務,先分別啓動tracker.conf,再分別啓動storage_ids.conf
/usr/bin//fdfs_trackerd /etc/fdfs/tracker.conf start
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
ps -ef | grep fdfs查看是否啓動成功,在/home/data/fastdfs/logs下,也就是上面咱們配置的數據目錄下能夠看到報錯或者啓動的日誌
5.修改測試
tracker_server=192.168.50.233:22122
base_path=/home/data/fastdfs
use_storage_id = true
http.tracker_server_port=8088
測試:sudo fdfs_test /etc/fdfs/client.conf upload client.conf
3、php擴展安裝
cd /home/fastdfs/fastdfs-master/php_client/ phpize ./configure --with-php-config=/usr/bin/php-config make make install sudo sh -c "cat /home/fastdfs/fastdfs-master/php_client/fastdfs_client.ini >> /usr/local/php/etc/php.ini" 而後重啓php便可
四,應用例子
本人封裝好的類:
<?php /** * Created by PhpStorm. * User: tjj * Date: 17-4-11 * Time: 上午10:15 */ class fdsf { public function __construct() { } /** * 輸出fdfs的版本號 */ public function get_client_version() { return fastdfs_client_version(); } /** * 獲取錯誤記錄數 * 返回值:int */ public function fastdfs_get_last_error_no() { return fastdfs_get_last_error_no(); } /** * 獲取錯誤信息 * 返回值:string */ public function fastdfs_get_last_error_info() { return fastdfs_get_last_error_info(); } /** * 鏈接到全部的tracker * 返回值:boolean */ public function fastdfs_tracker_make_all_connections() { return fastdfs_tracker_make_all_connections(); } /** * 得到小組統計信息 * 返回值:array */ public function fastdfs_tracker_list_groups($group_name = '', $tracker_server = array()) { if ($group_name) { return fastdfs_tracker_list_groups($group_name); } if ($group_name && $tracker_server) { return fastdfs_tracker_list_groups($group_name, $tracker_server); } return fastdfs_tracker_list_groups(); } /** * 獲取一個tracker server鏈接 * 返回值:array */ public function fastdfs_tracker_get_connection() { return fastdfs_tracker_get_connection(); } /** * 狀態測試 * 返回值:boolean */ public function fastdfs_active_test($obj) { return fastdfs_active_test($obj); } /** * 鏈接服務器 * 返回值:array */ public function fastdfs_connect_server($ip_addr = '', $port = 0) { if ($ip_addr && $port) { return fastdfs_connect_server($ip_addr, $port); } return array(); } /** * 斷開服務器連接 * 返回值:boolean */ public function fastdfs_disconnect_server($server) { return fastdfs_disconnect_server($server); } /** * 獲取上傳服務器的信息列表 * 返回值:array */ public function fastdfs_tracker_query_storage_store_list($group_name = '', $tracker_server = array()) { if ($group_name) { return fastdfs_tracker_query_storage_store_list($group_name); } if ($group_name && $tracker_server) { return fastdfs_tracker_query_storage_store_list($group_name, $tracker_server); } return fastdfs_tracker_query_storage_store_list(); } /** * 獲取storage信息 * 返回值:array */ public function fastdfs_tracker_query_storage_store($group_name = '', $tracker_server = array()) { if ($group_name) { return fastdfs_tracker_query_storage_store($group_name); } if ($group_name && $tracker_server) { return fastdfs_tracker_query_storage_store($group_name, $tracker_server); } return fastdfs_tracker_query_storage_store(); } /** * 上傳本地文件到服務器 * 返回值:array */ public function fastdfs_storage_upload_by_filename($local_filename = '', $file_ext_name = null, $meta_list = array(), $group_name = null, $tracker_server, $storage_server) { return fastdfs_storage_upload_by_filename($local_filename, $file_ext_name, $meta_list, $group_name, $tracker_server, $storage_server); } /** * 上傳本地文件到服務器2 * 返回值:array */ public function fastdfs_storage_upload_by_filename1($local_filename, $file_ext_name = null, $meta_list = array(), $group_name = null, $tracker, $storage) { return fastdfs_storage_upload_by_filename1($local_filename, $file_ext_name, $meta_list, $group_name, $tracker, $storage); } /** * 上傳本地文件到服務器(filebuff) * 返回值:array */ public function fastdfs_storage_upload_by_filebuff($content, $file_ext_name = null, $meta_list = array()) { return fastdfs_storage_upload_by_filebuff($content, $file_ext_name, $meta_list); } /** * 以主從文件的形式上傳文件。 * 參數:$local_filename:要上傳的從文件;$group:主文件所訴組;$master_filename:主文件的fastdfs路徑;$prefix_name:附加名稱。 * 返回值:array */ public function fastdfs_storage_upload_slave_by_filename($local_filename, $group_name, $master_filename, $prefix_name) { return fastdfs_storage_upload_slave_by_filename($local_filename, $group_name, $master_filename, $prefix_name); } /** * 以主從文件的形式上傳文件2。 * 參數:$local_filename:要上傳的從文件;$master_file_id;$prefix_name:附加名稱。 * 返回值:array */ public function fastdfs_storage_upload_slave_by_filename1($local_filename, $master_file_id, $prefix_name) { return fastdfs_storage_upload_slave_by_filename1($local_filename, $master_file_id, $prefix_name); } /** * 經過文件名獲得文件詳細信息 * 返回值:array */ public function fastdfs_get_file_info($group_name, $remote_filename) { if ($group_name && $remote_filename) { return fastdfs_get_file_info($group_name, $remote_filename); } return array(); } /** * 經過文件名獲得文件詳細信息 * 返回值:int */ public function fastdfs_storage_file_exist($group_name, $remote_filename) { if ($group_name && $remote_filename) { return fastdfs_storage_file_exist($group_name, $remote_filename); } return 0; } /** * 刪除storaged 文件詳細信息 * 返回值:boolean */ public function fastdfs_storage_delete_file($group_name, $remote_filename) { if ($group_name && $remote_filename) { return fastdfs_storage_delete_file($group_name, $remote_filename); } return false; } /** * 刪除storaged 文件詳細信息 * 返回值:boolean */ public function fastdfs_storage_delete_file1($slave_file_id) { if ($slave_file_id) { return fastdfs_storage_delete_file1($slave_file_id); } return false; } /** * 由主文件名產生從文件名 * 返回值:string */ public function fastdfs_gen_slave_filename($master_filename, $prefix_name) { if ($master_filename && $prefix_name) { return fastdfs_gen_slave_filename($master_filename, $prefix_name); } return false; } /** * 獲取token * 返回值:string */ public function fastdfs_http_gen_token($remote_filename, $ts) { if ($remote_filename && $ts) { return fastdfs_http_gen_token($remote_filename, $ts); } return false; } /** * 獲取文件內容 * 返回值:string */ public function fastdfs_storage_download_file_to_buff($group_name, $filename) { if ($group_name && $filename) { return fastdfs_storage_download_file_to_buff($group_name, $filename); } return false; } /** * 獲取文件內容to文件 * 返回值:string */ public function fastdfs_storage_download_file_to_file($group_name, $filename, $local_filename) { if ($group_name && $filename && $local_filename) { return fastdfs_storage_download_file_to_file($group_name, $filename, $local_filename); } return false; } public function fastdfs_storage_set_metadata($group_name, $filename, $meta_list,$flag) { return fastdfs_storage_set_metadata($group_name, $filename, $meta_list,$flag); } public function fastdfs_storage_upload_slave_by_filebuff($content, $group_name,$master_filename,$prefix_name,$file_ext_name) { return fastdfs_storage_upload_slave_by_filebuff($content, $group_name,$master_filename,$prefix_name,$file_ext_name); } }
上傳示例:
upload_test.php
<html> <head> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <input name="file_name" type="file"> <input name="ttt" type="text"> <input type="submit"> </form> </body> </html>
upload.php
<?php /** * Created by PhpStorm. * User: tjj * Date: 17-4-12 * Time: 上午11:28 */ include_once("./fdfs.php"); if ($_FILES["file_name"]["error"] == 0) { $f = new fdsf(); //tracker server $tracker = $f->fastdfs_tracker_get_connection(); /*var_dump($tracker); $f->fastdfs_active_test($tracker); var_dump($f->fastdfs_get_last_error_info()); exit;*/ if (!$f->fastdfs_active_test($tracker)) { echo "fastdfs_active_test errno: " . $f->fastdfs_get_last_error_no() . ", error info: " . $f->fastdfs_get_last_error_info(); } $server = fastdfs_connect_server($tracker['ip_addr'], $tracker['port']); //storage server $storage = $f->fastdfs_tracker_query_storage_store(); if (!$storage) { echo "fastdfs_tracker_query_storage_store errno: " . $f->fastdfs_get_last_error_no() . ", error info: " . $f->fastdfs_get_last_error_info(); exit(1); } $server = $f->fastdfs_connect_server($storage['ip_addr'], $storage['port']); if (!$server) { echo "fastdfs_connect_server errno: " . $f->fastdfs_get_last_error_no() . ", error info: " . $f->fastdfs_get_last_error_info(); exit(1); } if (!$f->fastdfs_active_test($server)) { echo "fastdfs_active_test errno: " . $f->fastdfs_get_last_error_no() . ", error info: " . $f->fastdfs_get_last_error_info(); exit(1); } $storage['sock'] = $server['sock']; $file_info = $f->fastdfs_storage_upload_by_filebuff(file_get_contents($_FILES["file_name"]["tmp_name"]), "jpg"); if ($file_info) { $group_name = $file_info['group_name']; $remote_filename = $file_info['filename']; file_put_contents('./upload.txt', "group_name={$group_name}|remote_filename={$remote_filename}\n", FILE_APPEND); }; var_dump($file_info); }
下載示例download.php
<?php /** * Created by PhpStorm. * User: tjj * Date: 17-4-12 * Time: 下午12:00 */ include_once("fdfs.php"); $f = new fdsf(); //tracker server $tracker = $f->fastdfs_tracker_get_connection(); if (!$f->fastdfs_active_test($tracker)) { echo "fastdfs_active_test errno: " . $f->fastdfs_get_last_error_no() . ", error info: " . $f->fastdfs_get_last_error_info(); } $server = fastdfs_connect_server($tracker['ip_addr'], $tracker['port']); //storage server $storage = $f->fastdfs_tracker_query_storage_store(); if (!$storage) { echo "fastdfs_tracker_query_storage_store errno: " . $f->fastdfs_get_last_error_no() . ", error info: " . $f->fastdfs_get_last_error_info(); exit(1); } $server = $f->fastdfs_connect_server($storage['ip_addr'], $storage['port']); if (!$server) { echo "fastdfs_connect_server errno: " . $f->fastdfs_get_last_error_no() . ", error info: " . $f->fastdfs_get_last_error_info(); exit(1); } if (!$f->fastdfs_active_test($server)) { echo "fastdfs_active_test errno: " . $f->fastdfs_get_last_error_no() . ", error info: " . $f->fastdfs_get_last_error_info(); exit(1); } $storage['sock'] = $server['sock']; $local_filename = 'test.jpg'; echo 'storage_download_file_to_file result: ' . $f->fastdfs_storage_download_file_to_file("group1","M00/00/00/oYYBAFj5sbWAAJLFAAAFtidO_ME83.jpg" , $local_filename) . "\n";
PS:其實,咱們能夠在測試的時候發現,在web訪問時,nginx並無輪詢選擇tracker,而是根據本身自己所在的服務選擇本身的tracker,可是儲存器storager是有實現了輪詢。因此當192.168.50.232上tracker的掛掉,也就192.168.50.232上的nginx上訪問的用戶會出現通訊失敗的錯誤。tracker結合nginx的輪詢,咱們下回分解