FastDFS介紹
1、簡介
FastDFS 是一個開源的高性能分布式檔案系統(DFS), 它的主要功能包括:檔案存盤,檔案同步和檔案訪問,以及高容量和負載平衡,主要解決了海量資料存盤問題,特別適合以中小檔案(建議范圍:4KB < file_size <500MB)為載體的在線服務,
FastDFS 系統有三個角色:跟蹤服務器(Tracker Server)、存盤服務器(Storage Server)和客戶端(Client),
Tracker Server:跟蹤服務器,主要做調度作業,起到均衡的作用;負責管理所有的 storage server和 group,每個 storage 在啟動后會連接 Tracker,告知自己所屬 group 等資訊,并保持周期性心跳,
Storage Server:存盤服務器,主要提供容量和備份服務;以 group 為單位,每個 group 內可以有多臺 storage server,資料互為備份,
Client:客戶端,上傳下載資料的服務器,也就是我們自己的專案所部署在的服務器,

2、FastDFS的存盤策略
為了支持大容量,存盤節點(服務器)采用了分卷(或分組)的組織方式,存盤系統由一個或多個卷組成,卷與卷之間的檔案是相互獨立的,所有卷的檔案容量累加就是整個存盤系統中的檔案容量,一個卷可以由一臺或多臺存盤服務器組成,一個卷下的存盤服務器中的檔案都是相同的,卷中的多臺存盤服務器起到了冗余備份和負載均衡的作用,
在卷中增加服務器時,同步已有的檔案由系統自動完成,同步完成后,系統自動將新增服務器切換到線上提供服務,當存盤空間不足或即將耗盡時,可以動態添加卷,只需要增加一臺或多臺服務器,并將它們配置為一個新的卷,這樣就擴大了存盤系統的容量,
3、FastDFS的上傳程序
FastDFS向使用者提供基本檔案訪問介面,比如upload、download、append、delete等,以客戶端庫的方式提供給用戶使用,
Storage Server會定期的向Tracker Server發送自己的存盤資訊,當Tracker Server Cluster中的Tracker Server不止一個時,各個Tracker之間的關系是對等的,所以客戶端上傳時可以選擇任意一個Tracker,
當Tracker收到客戶端上傳檔案的請求時,會為該檔案分配一個可以存盤檔案的group,當選定了group后就要決定給客戶端分配group中的哪一個storage server,當分配好storage server后,客戶端向storage發送寫檔案請求,storage將會為檔案分配一個資料存盤目錄,然后為檔案分配一個fileid,最后根據以上的資訊生成檔案名存盤檔案,

4、FastDFS的檔案同步
寫檔案時,客戶端將檔案寫至group內一個storage server即認為寫檔案成功,storage server寫完檔案后,會由后臺執行緒將檔案同步至同group內其他的storage server,
每個storage寫檔案后,同時會寫一份binlog,binlog里不包含檔案資料,只包含檔案名等元資訊,這份binlog用于后臺同步,storage會記錄向group內其他storage同步的進度,以便重啟后能接上次的進度繼續同步;進度以時間戳的方式進行記錄,所以最好能保證集群內所有server的時鐘保持同步,
storage的同步進度會作為元資料的一部分匯報到tracker上,tracke在選擇讀storage的時候會以同步進度作為參考,
5、FastDFS的檔案下載
客戶端uploadfile成功后,會拿到一個storage生成的檔案名,接下來客戶端根據這個檔案名即可訪問到該檔案,

跟upload file一樣,在downloadfile時客戶端可以選擇任意tracker server,tracker發送download請求給某個tracker,必須帶上檔案名資訊,tracke從檔案名中決議出檔案的group、大小、創建時間等資訊,然后為該請求選擇一個storage用來服務讀請求,
安裝
預備
操作環境:CentOS7 X64,以下操作都是單機環境,
預裝必要的軟體包:
# 編譯原始碼需要gcc gcc-c++ perl,軟體包解壓需要unzip
yum -y install gcc gcc-c++ perl unzip
提前下載好需要的軟體包(統一下載在/usr/local/src目錄),
# 下載libfastcommon環境包: wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
# 下載FastDFS包: wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
#下載nginx原始碼包: wget -c https://nginx.org/download/nginx-1.12.1.tar.gz
#下載 fastdfs-nginx-module插件包: wget https://github.com/happyfish100/fastdfs-nginx-module/archive/5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip
1、安裝 libfastcommon
libfastcommon是從 FastDFS 和 FastDHT 中提取出來的公共 C 函式庫,基礎環境,安裝即可 ,
a) 解壓并進入
# tar -zxvf V1.0.7.tar.gz
# cd libfastcommon-1.0.7
b) 編譯、安裝
# ./make.sh
# ./make.sh install
c) libfastcommon.so 默認安裝到了/usr/lib64/libfastcommon.so,但是后續我們安裝的FastDFS主程式設定的lib目錄是/usr/local/lib,因此需要創建軟鏈接,
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
2、下載安裝FastDFS
a) 解壓并進入
# tar -zxvf V5.05.tar.gz
# cd fastdfs-5.05
b) 編譯、安裝
# ./make.sh
# ./make.sh install
c) 安裝后的相應檔案與目錄如下:
1、fastdfs的服務腳本:
/etc/init.d/fdfs_storaged
/etc/init.d/fdfs_tracker
2、fastdfs組態檔(這三個是樣例配置,后續我們在此基礎上更改) :
/etc/fdfs/client.conf.sample
/etc/fdfs/storage.conf.sample
/etc/fdfs/tracker.conf.sample
3、命令工具在 /usr/bin/ 目錄下:
fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file
stop.sh
restart.sh
d) FastDFS 服務腳本設定的 bin 目錄是 /usr/local/bin, 但實際命令安裝在 /usr/bin/ 下,我們需要建立 /usr/bin 到 /usr/local/bin 的軟鏈接,
ln -s /usr/bin/fdfs_trackerd /usr/local/bin
ln -s /usr/bin/fdfs_storaged /usr/local/bin
ln -s /usr/bin/stop.sh /usr/local/bin
ln -s /usr/bin/restart.sh /usr/local/bin
3、配置FastDFS跟蹤器(Tracker)
a) 進入 /etc/fdfs,復制 FastDFS 跟蹤器樣例組態檔 tracker.conf.sample,并重命名為 tracker.conf,
# cd /etc/fdfs
# cp tracker.conf.sample tracker.conf
# vim tracker.conf
b) 編輯tracker.conf ,標紅的需要修改下,其它的默認即可,
# 組態檔是否不生效,false 為生效
disabled=false
# 提供服務的埠
port=22122
# Tracker 資料和日志目錄地址(根目錄必須存在,子目錄會自動創建)
base_path=/home/fastdfs/tracker
# HTTP 服務埠
http.server_port=80
c) 創建tracker基礎資料目錄,即base_path對應的目錄
# mkdir -p /home/fastdfs/tracker
d) 啟動Tracker
初次成功啟動,會在/home/fastdfs/tracker (配置的base_path)下創建 data、logs 兩個目錄,
啟動tracker
# service fdfs_trackerd start
查看 FastDFS Tracker 是否已成功啟動 ,22122埠正在被監聽,則算是Tracker服務安裝成功,
# netstat -unltp|grep fdfs

關閉Tracker命令:
# service fdfs_trackerd stop
a) tracker server 目錄及檔案結構
Tracker服務啟動成功后,會在base_path下創建data、logs兩個目錄,目錄結構如下:
${base_path}
|__data
| |__storage_groups.dat:存盤分組資訊
| |__storage_servers.dat:存盤服務器串列
|__logs
| |__trackerd.log: tracker server 日志檔案
4、配置 FastDFS 存盤 (Storage)
a) 進入 /etc/fdfs 目錄,復制 FastDFS 存盤器樣例組態檔 storage.conf.sample,并重命名為 storage.conf
# cd /etc/fdfs
# cp storage.conf.sample storage.conf
# vim storage.conf
b) 編輯storage.conf
標紅的需要修改,其它的默認即可,
# 組態檔是否不生效,false 為生效
disabled=false
# 指定此 storage server 所在 組(卷)
group_name=group1
# storage server 服務埠
port=23000
# 心跳間隔時間,單位為秒 (這里是指主動向 tracker server 發送心跳)
heart_beat_interval=30
# Storage 資料和日志目錄地址(根目錄必須存在,子目錄會自動生成)
base_path=/home/fastdfs/storage
# 存放檔案時 storage server 支持多個路徑,這里配置存放檔案的基路徑數目,通常只配一個目錄,
store_path_count=1
# 逐一配置 store_path_count 個路徑,索引號基于 0,
# 如果不配置 store_path0,那它就和 base_path 對應的路徑一樣,此處直接注釋掉,
#store_path0=/home/fastdfs/file
# FastDFS 存盤檔案時,采用了兩級目錄,這里配置存放檔案的目錄個數,
# 如果本引數只為 N(如: 256),那么 storage server 在初次運行時,會在 store_path 下自動創建 N * N 個存放檔案的子目錄,
subdir_count_per_path=256
# tracker_server 的串列 ,會主動連接 tracker_server
# 有多個 tracker server 時,每個 tracker server 寫一行
tracker_server=192.168.244.8:22122
# 允許系統同步的時間段 (默認是全天) ,一般用于避免高峰同步產生一些問題而設定,
sync_start_time=00:00
sync_end_time=23:59
# 訪問埠
http.server_port=80
c) 創建Storage基礎資料目錄,對應base_path目錄
# mkdir -p /home/fastdfs/storage
d) 啟動 Storage
啟動Storage前確保Tracker是啟動的,初次啟動成功,會在 /home/fastdfs/storage 目錄下創建 data、 logs 兩個目錄,
啟動
# service fdfs_storaged start
查看 Storage 是否成功啟動,23000 埠正在被監聽,就算 Storage 啟動成功,
# netstat -unltp|grep fdfs

關閉Storage命令:
# service fdfs_storaged stop
查看Storage和Tracker是否在通信:
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf

a) Storage 目錄
同 Tracker一樣,Storage 啟動成功后,在base_path 下創建了data、logs目錄,記錄著 Storage Server 的資訊,
在 store_path0 目錄下,創建了N*N個子目錄:

5、檔案上傳測驗
a) 修改 Tracker 服務器中的客戶端組態檔
# cd /etc/fdfs
# cp client.conf.sample client.conf
# vim client.conf
修改如下配置即可,其它默認,
# Client 的資料和日志目錄
base_path=/home/fastdfs/client
# Tracker埠
tracker_server=192.168.244.8:22122
#tracker服務器的埠
http.tracker_server_port=8081
b) 上傳測驗
在linux內部執行如下命令上傳一個檔案(比如:client.conf.sample)
# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf client.conf.sample
上傳成功后回傳檔案ID號:group1/M00/00/00/wKgz6lnduTeAMdrcAAEoRmXZPp870.sample

回傳的檔案ID由group、存盤目錄、兩級子目錄、fileid、檔案后綴名(由客戶端指定,主要用于區分檔案型別)拼接而成,

安裝Nginx
上面將檔案上傳成功了,但我們無法訪問下載,因此安裝Nginx作為服務器以支持Http方式訪問檔案,同時,后面安裝FastDFS的Nginx模塊也需要Nginx環境,
Nginx只需要安裝到StorageServer所在的服務器即可,用于訪問檔案,
1、安裝nginx所需環境
a) PCRE pcre-devel 安裝
# yum install -y pcre pcre-devel
b) zlib 安裝
# yum install -y zlib zlib-devel
c) OpenSSL 安裝
# yum install -y openssl openssl-devel
2、安裝Nginx
a) 進入/usr/local/src目錄,解壓nginx原始碼包并進入
# tar -zxvf nginx-1.12.1.tar.gz
# cd nginx-1.12.1
b) 使用默認配置
# ./configure
c) 編譯、安裝
# make
# make install
d) 啟動nginx
# cd /usr/local/nginx/sbin/
# ./nginx
其它命令
# ./nginx -s stop
# ./nginx -s quit
# ./nginx -s reload
e) 查看nginx的版本及模塊
/usr/local/nginx/sbin/nginx -V

訪問檔案
簡單的測驗訪問檔案
① 修改nginx.conf
# vim /usr/local/nginx/conf/nginx.conf
添加如下行,將 /group1/M00 映射到 /home/fastdfs/storage/data
location /group1/M00 {
alias /home/fastdfs/storage/data;
}
# 重啟nginx
# /usr/local/nginx/sbin/nginx -s reload

② 在瀏覽器訪問之前上傳的檔案、成功,
http://192.168.244.8/group1/M00/00/00/wKj0CF0cTBKAYwBiAABdrZgsqUU227.jpg
4、配置 Nginx的fastdfs 模塊
① fastdfs-nginx-module 模塊說明
FastDFS 通過 Tracker 服務器,將檔案放在 Storage 服務器存盤, 但是同組存盤服務器之間需要進行檔案復制, 有同步延遲的問題,
假設 Tracker 服務器將檔案上傳到了 192.168.244.8,上傳成功后檔案 ID已經回傳給客戶端,
此時 FastDFS 存盤集群機制會將這個檔案同步到同組存盤 192.168.244.9,在檔案還沒有復制完成的情況下,客戶端如果用這個檔案 ID 在 192.168. 244.9 上取檔案,就會出現檔案無法訪問的錯誤,
而 fastdfs-nginx-module 可以重定向檔案鏈接到源服務器取檔案,避免客戶端由于復制延遲導致的檔案無法訪問錯誤,
②解壓 fastdfs-nginx-module
# 解壓
# unzip 5e5f3566bbfa57418b5506aaefbe107a42c9fcb1.zip
# 重命名
# mv fastdfs-nginx-module-5e5f3566bbfa57418b5506aaefbe107a42c9fcb1 fastdfs-nginx-module
③ 配置Nginx
在nginx中添加模塊
# 先停掉nginx服務
# /usr/local/nginx/sbin/nginx -s stop
進入nginx解壓包目錄
# cd /usr/local/src/nginx-1.12.1/
# 添加模塊
# ./configure --add-module=../fastdfs-nginx-module/src
重新編譯、安裝
# make && make install
④ 查看Nginx的版本模塊
# /usr/local/nginx/sbin/nginx -V
有下面這個就說明添加模塊成功

⑤ 復制 fastdfs-nginx-module 原始碼中的組態檔到/etc/fdfs 目錄, 并修改
# cd /usr/local/src/fastdfs-nginx-module/src
# cp mod_fastdfs.conf /etc/fdfs/
修改如下配置,其它默認
# 連接超時時間
connect_timeout=10
# Tracker Server
tracker_server=192.168.244.8:22122
# StorageServer 默認埠
storage_server_port=23000
# 如果檔案ID的uri中包含/group**,則要設定為true
url_have_group_name = true
# Storage 配置的store_path0路徑,必須和storage.conf中的一致,統一注釋掉
#store_path0= /home/yuqing/fastdfs
⑥ 復制 FastDFS 的部分組態檔到/etc/fdfs 目錄
# cd /usr/local/src/fastdfs-5.05/conf/
# cp anti-steal.jpg http.conf mime.types /etc/fdfs/
⑦ nginx,修改nginx.conf
# vi /usr/local/nginx/conf/nginx.conf
修改配置,其它的默認
在80埠下添加fastdfs-nginx模塊
location ~/group0/M00 {
ngx_fastdfs_module;
}

注意:
listen 80 埠值是要與 /etc/fdfs/storage.conf 中的 http.server_port=80 (前面改成80了)相對應,
⑧啟動nginx
# /usr/local/nginx/sbin/nginx
列印處如下就算配置成功

⑨ 在地址欄訪問,
重新上傳一個圖片,能用瀏覽器打開,即安裝成功,
在springboot集成客戶端流程
引入java客戶端依賴
<!-- fastDFS --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.1-RELEASE</version>
配置 application.yml
#fastDFS
fdfs:
#讀取超時,單位秒
so-timeout: 1500
#連接超時,單位秒
connect-timeout: 600
#縮略圖
thumb-image:
width: 150
height: 150
#tracker服務地址串列
tracker-list:
- localhost:22122
撰寫配置類 配置資訊已經在上面寫齊了所以 基本沒有內容
/**
* FastDFS 配置類
*
* Create by qinji on 2019/10/9
*/
@Configuration
@Import(FdfsClientConfig.class)
/**
* 解決jmx重復注冊bean的問題
*/
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastDFSConfig {
}
封裝成工具類
@Slf4j @Component public class FastDFSUtils { /** 支持的圖片型別 */ private static final String[] SUPPORT_IMAGE_TYPE = { "JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP" }; private static final List<String> SUPPORT_IMAGE_LIST = Arrays.asList(SUPPORT_IMAGE_TYPE); @Autowired private FastFileStorageClient storageClient; @Autowired private ThumbImageConfig thumbImageConfig; /** * 上傳圖片檔案及縮略圖 */ public StorePath uploadImageFile(File file, String fileExtName, Set<MateData> metaDataSet) throws FileNotFoundException { //上傳檔案 return storageClient.uploadImageAndCrtThumbImage(new FileInputStream(file), file.length(), fileExtName, metaDataSet); } /** * 上傳非圖片檔案 */ public StorePath uploadFile(File file, String suffix, Set<MetaData> meta) throws Exception { return storageClient.uploadFile(new FileInputStream(file), file.length(), suffix, null); } /** * 上傳檔案 */ public String uploadFile(MultipartFile file) { if(file == null || file.isEmpty()) { throw new AppException("上傳檔案不存在"); } // 檔案后綴名 String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1); StorePath path; try { if(isSupportImage(suffix)) { // 圖片 // 上傳圖片并且生成縮略圖 path = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), suffix, null); } else { // 非圖片檔案 path = storageClient.uploadFile(file.getInputStream(), file.getSize(), suffix, null); } return path.getPath(); } catch (IOException e) { log.error(e.getMessage()); } return ""; } /** * 檔案下載 */ public byte[] downloadFile(String group, String url, DownloadByteArray array) throws Exception { return storageClient.downloadFile(group, url, array); } /** * 打包下載 */ public void downloadZip(List<UploadFileEntity> fileList, String group, ZipOutputStream zip) throws Exception { DownloadByteArray downloadByteArray = new DownloadByteArray(); for(UploadFileEntity entity : fileList) { // zipEntry例如:工程檔案/測驗檔案夾/xxx.docx zip.putNextEntry(new ZipEntry(entity.getParentFolderPath() + entity.getName())); // 下載檔案 byte[] bytes = downloadFile(group, entity.getUrl(), downloadByteArray); // 寫入zipOutputStream IOUtils.write(bytes, zip); zip.closeEntry(); } } /** * 洗掉檔案 */ public void deleteFile(String group, String path) { storageClient.deleteFile(group, path); } // public void downloadFile(ZipOutputStream zip) throws Exception { // String url = "M00/00/00/wKgA1V2d0KKAEB9FAAJi1MdFAb4930.png"; // String url2 = "M00/00/00/wKgA1V2d0qiAe_nFAADGWkQfxd439.docx"; // //創建流 // //ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // //ZipOutputStream zip = new ZipOutputStream(outputStream); // zip.putNextEntry(new ZipEntry("測驗檔案夾" + File.separator + "測驗圖片下載.png")); // // DownloadByteArray downloadByteArray = new DownloadByteArray(); // byte[] bytes = downloadFile("group1", url, downloadByteArray); // //InputStream inputStream = new ByteArrayInputStream(bytes); // IOUtils.write(bytes, zip); // zip.closeEntry(); // zip.putNextEntry(new ZipEntry("測驗檔案夾" + File.separator + "測驗檔案下載.docx")); // bytes = downloadFile("group1", url2, downloadByteArray); // IOUtils.write(bytes, zip); // zip.closeEntry(); // } /** * 創建元資訊 */ public Set<MateData> createMetaData(MetaData meta) { Set<MateData> metaDataSet = new HashSet<>(); metaDataSet.add(new MateData("author", meta.getAuthor())); metaDataSet.add(new MateData("oldName", meta.getOldName())); return metaDataSet; } /** * 是否是支持的圖片檔案 */ public boolean isSupportImage(String fileExtName) { return SUPPORT_IMAGE_LIST.contains(fileExtName.toUpperCase()); } @Data public class MetaData { private String author; // 原檔案名 private String oldName; } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/2123.html
標籤:其他
上一篇:SystemC中文教程一
