配套資料,免費下載
連結:https://pan.baidu.com/s/17n_VVnk0geU_fv3789_6Tg
提取碼:kzvy
複製這段內容後開啟百度網路硬碟手機App,操作更方便哦
FastDFS是一個開源的輕量級分散式檔案系統,為網際網路應用量身定做,簡單、靈活、高效,採用C語言開發,由阿里巴巴開發並開源。
FastDFS對檔案進行管理,功能包括:檔案儲存、檔案同步、檔案存取(檔案上傳、檔案下載、檔案刪除)等,解決了大容量檔案儲存的問題,特別適合以檔案為載體的線上服務,如相簿網站、檔案網站、圖片網站、視訊網站等等。
FastDFS充分考慮了冗餘備份、線性擴容等機制,並注重高可用、高效能等指標,使用FastDFS很容易搭建一套高效能的檔案伺服器叢集提供檔案上傳、下載等服務。
2008年4月專案正式啟動
2008年7月推出V1.00
2010年8月推出V2.00
2011年6月推出V3.00
2012年10月推出V4.00
2013年12月推出V5.00
2019年10月推出V6.00
FastDFS系統架構從第一個版本釋出後一直沒有大的調整,高版本完全相容低版本的資料,可以做到平滑升級,推薦更新升級到最新版本。
FastDFS檔案系統由兩大部分構成,一個是使用者端,一個是伺服器端。
使用者端通常指我們的程式,比如我們的Java程式去連線FastDFS、操作FastDFS,那我們的Java程式就是一個使用者端,FastDFS提供專有API存取,目前提供了C、Java和PHP幾種程式語言的API,用來存取FastDFS檔案系統。
伺服器端由兩個部分構成:一個是跟蹤器(tracker),一個是儲存節點(storage)。
跟蹤器(tracker)主要做排程工作,在記憶體中記錄叢集中儲存節點storage的狀態資訊,是前端Client和後端儲存節點storage的樞紐。因為相關資訊全部在記憶體中,Tracker server的效能非常高,一個較大的叢集(比如上百個group)中有3臺就足夠了。
儲存節點(storage)用於儲存檔案,包括檔案和檔案屬性(meta data)都儲存到儲存伺服器磁碟上,完成檔案管理的所有功能:檔案儲存、檔案同步和提供檔案存取等。
官方網址:點選開啟
[root@caochenlei ~]# yum install -y gcc gcc-c++ perl perl-devel openssl openssl-devel pcre pcre-devel zlib zlib-devel libevent libevent-devel
下載:
[root@caochenlei ~]# wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz
解壓:
[root@caochenlei ~]# tar -zxvf V1.0.43.tar.gz
切換:
[root@caochenlei ~]# cd libfastcommon-1.0.43
編譯:
[root@caochenlei libfastcommon-1.0.43]# ./make.sh
安裝:
[root@caochenlei libfastcommon-1.0.43]# ./make.sh install
[root@caochenlei libfastcommon-1.0.43]# cd ~
下載:
[root@caochenlei ~]# wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz
解壓:
[root@caochenlei ~]# tar -zxvf V6.06.tar.gz
切換:
[root@caochenlei ~]# cd fastdfs-6.06
編譯:
[root@caochenlei fastdfs-6.06]# ./make.sh
安裝:
[root@caochenlei fastdfs-6.06]# ./make.sh install
檢視可執行檔案:
[root@caochenlei fastdfs-6.06]# ll /usr/bin/fdfs*
檢視組態檔:
[root@caochenlei fastdfs-6.06]# ll /etc/fdfs/
拷貝其它設定:
[root@caochenlei fastdfs-6.06]# cd conf
[root@caochenlei conf]# cp http.conf /etc/fdfs/
[root@caochenlei conf]# cp mime.types /etc/fdfs/
[root@caochenlei conf]# cd /etc/fdfs/
[root@caochenlei fdfs]# mv client.conf.sample client.conf
[root@caochenlei fdfs]# mv storage.conf.sample storage.conf
[root@caochenlei fdfs]# mv storage_ids.conf.sample storage_ids.conf
[root@caochenlei fdfs]# mv tracker.conf.sample tracker.conf
設定 tracker :
修改tracker.conf的以下幾項設定項:vi tracker.conf
建立相對應的資料夾:
[root@caochenlei fdfs]# mkdir -p /opt/fastdfs/tracker
設定 storage :
修改storage.conf的以下幾項設定項:vi storage.conf
建立相對應的資料夾:
[root@caochenlei fdfs]# mkdir -p /opt/fastdfs/storage
[root@caochenlei fdfs]# mkdir -p /opt/fastdfs/storage/files
啟動 tracker :
[root@caochenlei fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf
啟動 storage :
[root@caochenlei fdfs]# fdfs_storaged /etc/fdfs/storage.conf
檢視啟動情況:
[root@caochenlei fdfs]# ps -ef | grep fdfs
檢查監控資訊:
[root@caochenlei fdfs]# fdfs_monitor /etc/fdfs/storage.conf
檢視資料目錄:
[root@caochenlei fdfs]# ls /opt/fastdfs/storage/files/data/
重新啟動 tracker :
[root@caochenlei fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf restart
waiting for pid [32335] exit ...
starting ...
重新啟動 storage :
[root@caochenlei fdfs]# fdfs_storaged /etc/fdfs/storage.conf restart
waiting for pid [32375] exit ...
starting ...
檢視啟動情況:
[root@caochenlei fdfs]# ps -ef | grep fdfs
設定 client :
修改client.conf的以下幾項設定項:vi client.conf
#client儲存資料目錄
#註冊當前儲存節點的跟蹤器地址
建立相對應的資料夾:
[root@caochenlei fdfs]# mkdir -p /opt/fastdfs/client
建立 a.txt :
[root@caochenlei fdfs]# echo "Hello,FastDFS" > a.txt
上傳 a.txt :
格式:fdfs_test /etc/fdfs/client.conf upload 檔案路徑
[root@caochenlei fdfs]# fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/a.txt
This is FastDFS client test program v6.06
Copyright (C) 2008, Happy Fish / YuQing
FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.fastken.com/
for more detail.
[2020-09-05 20:39:01] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=5, 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
tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.239.128, port=23000
group_name=group1, ip_addr=192.168.239.128, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt
source ip address: 192.168.239.128
file timestamp=2020-09-05 20:39:01
file size=14
file crc32=3519913659
example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
source ip address: 192.168.239.128
file timestamp=2020-09-05 20:39:01
file size=14
file crc32=3519913659
example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
注意:group_name、remote_filename
地址格式,舉例如下圖:
檢視上傳後的資料檔案:
[root@caochenlei fdfs]# ll /opt/fastdfs/storage/files/data/00/00/
總用量 16
-rw-r--r--. 1 root root 14 9月 5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
-rw-r--r--. 1 root root 49 9月 5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt-m
-rw-r--r--. 1 root root 14 9月 5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt
-rw-r--r--. 1 root root 49 9月 5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt-m
刪除 a.txt :
格式:fdfs_delete_file /etc/fdfs/client.conf (group_name)/(remote_filename)
[root@caochenlei fdfs]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806.txt
[root@caochenlei fdfs]# ll /opt/fastdfs/storage/files/data/00/00/
總用量 8
-rw-r--r--. 1 root root 14 9月 5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
-rw-r--r--. 1 root root 49 9月 5 20:39 wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt-m
[root@caochenlei fdfs]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKjvgF9ThuWAGUrIAAAADtHNnrs806_big.txt
[root@caochenlei fdfs]# ll /opt/fastdfs/storage/files/data/00/00/
總用量 0
關閉 tracker :
[root@caochenlei fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf stop
waiting for pid [32441] exit ...
pid [32441] exit.
關閉 storage :
[root@caochenlei fdfs]# fdfs_storaged /etc/fdfs/storage.conf stop
waiting for pid [32453] exit ...
pid [32453] exit.
檢視啟動情況:
[root@caochenlei fdfs]# ps -ef | grep fdfs
注意問題:
啟動 tracker :
[root@caochenlei fdfs]# fdfs_trackerd /etc/fdfs/tracker.conf
啟動 storage :
[root@caochenlei fdfs]# fdfs_storaged /etc/fdfs/storage.conf
關閉防火牆:
[root@caochenlei fdfs]# service iptables stop
[root@caochenlei fdfs]# chkconfig iptables off
回退根目錄:
[root@caochenlei fdfs]# cd ~
Nginx依賴:
[root@caochenlei ~]# yum install -y gcc gcc-c++ make libtool wget pcre pcre-devel zlib zlib-devel openssl openssl-devel
Nginx下載:
[root@caochenlei ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
Nginx解壓:
[root@caochenlei ~]# tar -zxvf nginx-1.18.0.tar.gz
fastdfs-nginx-module下載:
[root@caochenlei ~]# wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
fastdfs-nginx-module解壓:
[root@caochenlei ~]# tar -zxvf V1.22.tar.gz
#檢視一下拓展模組所在路徑,後邊會用到這個路徑
[root@caochenlei ~]# cd fastdfs-nginx-module-1.22/src/
[root@caochenlei src]# pwd
/root/fastdfs-nginx-module-1.22/src
#回退到根目錄,方便接下來的一系列安裝
[root@caochenlei src]# cd ~
Nginx及fastdfs-nginx-module安裝:
注意:因為這個模組必須在Nginx的安裝的過程中才能新增,所有我們需要重新安裝一個Nginx,為了進行區分,我們把新安裝的Nginx取名為nginx_fdfs
[root@caochenlei ~]# cd nginx-1.18.0
[root@caochenlei nginx-1.18.0]# ./configure --prefix=/usr/local/nginx_fdfs --add-module=/root/fastdfs-nginx-module-1.22/src
[root@caochenlei nginx-1.18.0]# make && make install
注意:安裝完成後的路徑為:/usr/local/nginx_fdfs
Nginx命令(此處瞭解,先不要敲呢,跳過這步):
FastDFS的Nginx存取設定:
將 /root/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf 拷貝到 /etc/fdfs/ 目錄下,這樣才能正常啟動Nginx
[root@caochenlei nginx-1.18.0]# cp /root/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
修改mod_fastdfs.conf組態檔:
修改mod_fastdfs.conf的以下幾項設定項:vi /etc/fdfs/mod_fastdfs.conf
#mod_fastdfs儲存資料目錄
#註冊當前儲存節點的跟蹤器地址
#路徑中是否包含分組名稱,我們為true
#真正存放檔案的目錄
建立相對應的資料夾:
[root@caochenlei nginx-1.18.0]# mkdir -p /opt/fastdfs/nginx_mod
設定Nginx的拓展模組請求轉發:
[root@caochenlei nginx-1.18.0]# vi /usr/local/nginx_fdfs/conf/nginx.conf
location ~ /group[1-9]/M0[0-9] {
ngx_fastdfs_module;
}
注意:ngx_fastdfs_module; #這個指令不是Nginx本身提供的,是擴充套件模組提供的,根據這個指令找到FastDFS提供的Nginx模組組態檔,然後找到Tracker,最終找到Stroager
啟動帶有Fastdfs模組的Nginx:
[root@caochenlei nginx-1.18.0]# /usr/local/nginx_fdfs/sbin/nginx -c /usr/local/nginx_fdfs/conf/nginx.conf
ngx_http_fastdfs_set pid=35500
上傳一個檔案進行測試驗證:
[root@caochenlei fdfs]# fdfs_test /etc/fdfs/client.conf upload /etc/fdfs/a.txt
This is FastDFS client test program v6.06
Copyright (C) 2008, Happy Fish / YuQing
FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.fastken.com/
for more detail.
[2020-09-05 21:41:43] DEBUG - base_path=/opt/fastdfs/client, connect_timeout=5, 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
tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.239.128, port=23000
group_name=group1, ip_addr=192.168.239.128, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
source ip address: 192.168.239.128
file timestamp=2020-09-05 21:41:43
file size=14
file crc32=3519913659
example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395_big.txt
source ip address: 192.168.239.128
file timestamp=2020-09-05 21:41:43
file size=14
file crc32=3519913659
example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395_big.txt
在虛擬機器器外部瀏覽器存取上傳的檔案:
注意:直接輸入example file url: http://192.168.239.128/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
將第二章的單範例版本複製7個(關閉FastDFS服務,再關閉系統,退出虛擬機器器),然後依次啟動,選擇我已複製虛擬機器器,這樣就會為每一個虛擬機器器重新分配一塊網路卡,這樣每天虛擬機器器就可以正常上網了。如果你要是有精力,你也可以按照第二章的方法,重新安裝這幾臺機器,但是這裡為了省事,直接複製了。
學習這章電腦的環境:
記憶體至少8G+
硬碟至少255G+
關閉服務及系統命令:
[root@caochenlei ~]# /usr/local/nginx_fdfs/sbin/nginx -s quit
[root@caochenlei ~]# fdfs_trackerd /etc/fdfs/tracker.conf stop
[root@caochenlei ~]# fdfs_storaged /etc/fdfs/storage.conf stop
[root@caochenlei ~]# poweroff
複製完的效果如下圖:
依次開啟啟動虛擬機器器:
注意:啟動後使用ifconfig檢視當前虛擬機器器的IP地址
CentOS 6 64 bit - 副本 (1):192.168.239.136
CentOS 6 64 bit - 副本 (2):192.168.239.137
CentOS 6 64 bit - 副本 (3):192.168.239.138
CentOS 6 64 bit - 副本 (4):192.168.239.139
CentOS 6 64 bit - 副本 (5):192.168.239.140
CentOS 6 64 bit - 副本 (6):192.168.239.141
CentOS 6 64 bit - 副本 (7):192.168.239.142
使用連線工具進行連線:
其實tracker已經在單範例版本的時候就設定好了,跳過就行了。
每兩個storage server為一組,共兩個組(group1 和 group2),一個組內有兩個storage server。
修改group1的storage1的storage.conf:vi /etc/fdfs/storage.conf
修改group1的storage2的storage.conf:vi /etc/fdfs/storage.conf
修改group2的storage3的storage.conf:vi /etc/fdfs/storage.conf
修改group2的storage4的storage.conf:vi /etc/fdfs/storage.conf
修改group1的nginx1的mod_fastdfs.conf:vi /etc/fdfs/mod_fastdfs.conf
tracker_server = 192.168.239.136:22122
tracker_server = 192.168.239.137:22122
group_name = group1
group_count = 2
在末尾增加2個組的具體資訊:
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs/storage/files
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs/storage/files
修改group1的nginx2的mod_fastdfs.conf:vi /etc/fdfs/mod_fastdfs.conf
tracker_server = 192.168.239.136:22122
tracker_server = 192.168.239.137:22122
group_name = group1
group_count = 2
在末尾增加2個組的具體資訊:
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs/storage/files
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs/storage/files
修改group2的nginx3的mod_fastdfs.conf:vi /etc/fdfs/mod_fastdfs.conf
tracker_server = 192.168.239.136:22122
tracker_server = 192.168.239.137:22122
group_name = group2
group_count = 2
在末尾增加2個組的具體資訊:
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs/storage/files
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs/storage/files
修改group2的nginx4的mod_fastdfs.conf:vi /etc/fdfs/mod_fastdfs.conf
tracker_server = 192.168.239.136:22122
tracker_server = 192.168.239.137:22122
group_name = group2
group_count = 2
在末尾增加2個組的具體資訊:
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs/storage/files
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/opt/fastdfs/storage/files
Nginx刪除:
注意:之前已經下載了nginx安裝包並解壓安裝了,我們先刪除以前安裝的,然後再安裝
[root@caochenlei ~]# rm -rf /usr/local/nginx_fdfs
注意:之前已經下載了nginx安裝包並解壓安裝了,我們先刪除以前解壓的,然後再解壓
Nginx解壓:
[root@caochenlei ~]# rm -rf nginx-1.18.0
[root@caochenlei ~]# tar -zxvf nginx-1.18.0.tar.gz
Nginx安裝:
[root@caochenlei ~]# cd nginx-1.18.0
[root@caochenlei nginx-1.18.0]# ./configure
[root@caochenlei nginx-1.18.0]# make && make install
注意:安裝完成後的路徑為:/usr/local/nginx
Nginx設定:
[root@caochenlei nginx-1.18.0]# vi /usr/local/nginx/conf/nginx.conf
#設定負載均衡
upstream fastdfs_storage_server {
server 192.168.239.138:80;
server 192.168.239.139:80;
server 192.168.239.140:80;
server 192.168.239.141:80;
}
#設定請求路徑
location ~ /group[1-9]/M0[0-9] {
proxy_pass http://fastdfs_storage_server;
}
Nginx命令:
啟動兩個tracker:
[root@caochenlei nginx-1.18.0]# fdfs_trackerd /etc/fdfs/tracker.conf
啟動四個storage:
[root@caochenlei ~]# fdfs_storaged /etc/fdfs/storage.conf
啟動四個storage上的nginx:
[root@caochenlei ~]# /usr/local/nginx_fdfs/sbin/nginx -c /usr/local/nginx_fdfs/conf/nginx.conf
啟動兩個tracker上的nginx:
[root@caochenlei nginx-1.18.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
開啟瀏覽器,輸入以下幾個地址進行測試:
注意:wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt這個檔案是我們之前測試的檔案,已經上傳上去了
兩個tracker上的存取:
http://192.168.239.136/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
http://192.168.239.137/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
四個storage上的存取:
http://192.168.239.138/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
http://192.168.239.139/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
http://192.168.239.140/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
http://192.168.239.141/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt
我們需要使用一臺單獨的nginx為兩臺tracker進行負載均衡,以此達到統一存取路徑。這回設定的是192.168.239.142這臺機器
Nginx刪除:
注意:之前已經下載了nginx安裝包並解壓安裝了,我們先刪除以前安裝的,然後再安裝
[root@caochenlei ~]# rm -rf /usr/local/nginx_fdfs
注意:之前已經下載了nginx安裝包並解壓安裝了,我們先刪除以前解壓的,然後再解壓
Nginx解壓:
[root@caochenlei ~]# rm -rf nginx-1.18.0
[root@caochenlei ~]# tar -zxvf nginx-1.18.0.tar.gz
Nginx安裝:
[root@caochenlei ~]# cd nginx-1.18.0
[root@caochenlei nginx-1.18.0]# ./configure
[root@caochenlei nginx-1.18.0]# make && make install
注意:安裝完成後的路徑為:/usr/local/nginx
Nginx設定:
[root@caochenlei nginx-1.18.0]# vi /usr/local/nginx/conf/nginx.conf
#設定負載均衡
upstream fastdfs_tracker_server {
server 192.168.239.136:80;
server 192.168.239.137:80;
}
#設定請求路徑
location ~ /group[1-9]/M0[0-9] {
proxy_pass http://fastdfs_tracker_server;
}
Nginx命令:
Nginx啟動:
[root@caochenlei nginx-1.18.0]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@caochenlei nginx-1.18.0]# ps -ef | grep nginx
root 6309 1 0 11:20 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 6310 6309 0 11:20 ? 00:00:00 nginx: worker process
root 6312 3016 0 11:20 pts/1 00:00:00 grep nginx
Nginx測試:
開啟瀏覽器,輸入http://192.168.239.142/group1/M00/00/00/wKjvgF9TlZeAZpeTAAAADtHNnrs395.txt,如果看到內容證明環境已經搭建完畢!
請您參考:學習Nginx這一篇就夠了中的高可用叢集如何搭建的。
原理說明:準備兩臺Nginx,一主一備,每一臺上安裝一個keepalived,由keepalived負責監測當前機器上的nginx是否可用(其實是呼叫一個shell指令碼不停的檢視程序資訊),同時也會在兩個keepalived之間進行心跳監測,如果主節點宕機,則從節點自動跟上,存取是通過虛擬IP進行存取的。
注意:我們進行JavaAPI測試採用的是單範例版本,所以要將之前的單範例版本啟動並把所有服務開啟,這一步請參考第二章。還有一個問題需要注意,虛擬機器器的IP地址可能變化,如果一旦發生變化,那麼之前設定的FastDFS就肯定不會啟動成功,雖然我們會設定了,但是我們為了偷懶,臨時把IP地址修改為之前的那個IP地址,命令如下(命令生效,請重新啟動一個視窗連線):
ifconfig eth0 192.168.239.128 netmask 255.255.255.0
工程名稱:FastDFSDemo
專案依賴:
單元測試:Junit 4
測試包名:com.caochenlei.fastdfs.demo
組態檔:在src中建立 fdfs_client.conf
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.239.128:22122
connection_pool.enabled = true
connection_pool.max_count_per_entry = 500
connection_pool.max_idle_time = 3600
connection_pool.max_wait_time_in_ms = 1000
FastDFSDemo.java(全路徑:/FastDFSDemo/src/com/caochenlei/fastdfs/demo/FastDFSDemo.java)
/**
* 上傳檔案
*/
@Test
public void fileUpload() {
try {
// 1.載入組態檔,預設去classpath下載入
ClientGlobal.init("fdfs_client.conf");
// 2.建立TrackerClient物件
TrackerClient trackerClient = new TrackerClient();
// 3.建立TrackerServer物件
TrackerServer trackerServer = trackerClient.getTrackerServer();
// 4.建立StorageServler物件
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
// 5.建立StorageClient物件,這個物件完成對檔案的操作
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 6.上傳檔案(第一個引數:本地檔案路徑、第二個引數:上傳檔案的字尾、第三個引數:檔案資訊)
String[] uploadArray = storageClient.upload_file("a.txt", "txt", null);
for (String str : uploadArray) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
執行結果:
group1
M00/00/00/wKjvgF9UbIGAV0ioAAAAHjXTgjs092.txt
FastDFSDemo.java(全路徑:/FastDFSDemo/src/com/caochenlei/fastdfs/demo/FastDFSDemo.java)
/**
* 下載檔案
*/
@Test
public void fileDownload() {
try {
// 1.載入組態檔,預設去classpath下載入
ClientGlobal.init("fdfs_client.conf");
// 2.建立TrackerClient物件
TrackerClient trackerClient = new TrackerClient();
// 3.建立TrackerServer物件
TrackerServer trackerServer = trackerClient.getTrackerServer();
// 4.建立StorageServler物件
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
// 5.建立StorageClient物件,這個物件完成對檔案的操作
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 6.下載檔案(返回0表示成功,其它均表示失敗)
int num = storageClient.download_file("group1", "M00/00/00/wKjvgF9UbIGAV0ioAAAAHjXTgjs092.txt", "b.txt");
System.out.println(num);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
執行結果:
0
FastDFSDemo.java(全路徑:/FastDFSDemo/src/com/caochenlei/fastdfs/demo/FastDFSDemo.java)
/**
* 刪除檔案
*/
@Test
public void fileDelete() {
try {
// 1.載入組態檔,預設去classpath下載入
ClientGlobal.init("fdfs_client.conf");
// 2.建立TrackerClient物件
TrackerClient trackerClient = new TrackerClient();
// 3.建立TrackerServer物件
TrackerServer trackerServer = trackerClient.getTrackerServer();
// 4.建立StorageServler物件
StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
// 5.建立StorageClient物件,這個物件完成對檔案的操作
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 6.刪除檔案(返回0表示成功,其它均表示失敗)
int num = storageClient.delete_file("group1", "M00/00/00/wKjvgF9UbIGAV0ioAAAAHjXTgjs092.txt");
System.out.println(num);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
執行結果:
0