您好,登錄后才能下訂單哦!
本文會搭建一個適合低業務訪問業務量的高可用的FastDFS集群環境:兩個Tracker服務,一個storage group中兩個storage服務節點;該方案僅適用于業務訪問量較低的環境下。對于大量業務系統的高并發訪問,為了保證存儲系統正常工作一般的架構思路:安裝多個Tracker服務(至少兩個,根據業務量調整),搭建多個storage group(至少兩個,根據業務量調整),每個storage group中多個storage node(至少兩個,做數據的冗余備份,進行容災機制,而且node必須在不同的機器上)
一、FastDFS簡介
FastDFS是一個開源的輕量級分布式文件系統,它對文件進行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負載均衡的問題。特別適合以文件為載體的在線服務,如相冊網站、視頻網站等等。
FastDFS為互聯網量身定制,充分考慮了冗余備份、負載均衡、線性擴容等機制,并注重高可用、高性能等指標,使用FastDFS很容易搭建一套高性能的文件服務器集群提供文件上傳、下載等服務。
FastDFS服務端有兩個角色:跟蹤器(tracker)和存儲節點(storage)。跟蹤器主要做調度工作,在訪問上起負載均衡的作用。
存儲節點存儲文件,完成文件管理的所有功能:就是這樣的存儲、同步和提供存取接口,FastDFS同時對文件的metadata進行管理。所謂文件的meta data就是文件的相關屬性,以鍵值對(key value)方式表示,如:width=1024,其中的key為width,value為1024。文件metadata是文件屬性列表,可以包含多個鍵值對。
跟蹤器和存儲節點都可以由一臺或多臺服務器構成。跟蹤器和存儲節點中的服務器均可以隨時增加或下線而不會影響線上服務。其中跟蹤器中的所有服務器都是對等的,可以根據服務器的壓力情況隨時增加或減少。
為了支持大容量,存儲節點(服務器)采用了分卷(或分組)的組織方式。存儲系統由一個或多個卷組成,卷與卷之間的文件是相互獨立的,所有卷的文件容量累加就是整個存儲系統中的文件容量。一個卷可以由一臺或多臺存儲服務器組成,一個卷下的存儲服務器中的文件都是相同的,卷中的多臺存儲服務器起到了冗余備份和負載均衡的作用。
在卷中增加服務器時,同步已有的文件由系統自動完成,同步完成后,系統自動將新增服務器切換到線上提供服務。
當存儲空間不足或即將耗盡時,可以動態添加卷。只需要增加一臺或多臺服務器,并將它們配置為一個新的卷,這樣就擴大了存儲系統的容量。
FastDFS中的文件標識分為兩個部分:卷名和文件名,二者缺一不可。
———簡介摘自百度百科
二、FastDFS原理介紹
1.文件上傳
FastDFS以客戶端庫的方式提供基本的文件訪問接口如upload、download、append、delete等,Storage 服務會定時的向Tracker服務發送自己的存儲信息。當Tracker 服務集群中的Tracker 服務是多個時,各個Tracker服務之間的關系是對等的,因此客戶端上傳時會任意選擇一個Trackre服務。當Tracker服務收到客戶端上傳文件請求時,會為該文件分配一個可以存儲文件的group,當選定了group后就要決定給客戶端分配group中的哪一個storage服務。當分配好storage 服務后,客戶端向storage發送寫文件請求,storage將會為文件分配一個數據存儲目錄。然后為文件分配一個文件ID標示,然后根據以上的信息生成文件名存儲文件。
2.文件同步
上傳文件后,客戶端將文件寫到group內的一個storage 服務即為上傳文件成功,storage服務寫完文件后,會由后臺線程將文件同步至同group內的其他的storage 服務節點上。?每個storage服務寫文件后,會同時寫一份binlog,binlog里不包含文件數據,只包含文件名等元信息,這份binlog用于后臺同步,storage會記錄向group內其他storage同步的進度,以便重啟后能接上次的進度繼續同步;進度以時間戳的方式進行記錄,所以最好能保證集群內的所有server的始終保持同步。最后Storage服務的同步進度會作為元數據的一部分匯報到tracker服務上,tracker服務在選擇讀storage的時候會以同步進度作為參考指標。
3.下載文件
當下載文件時,客戶端先詢問tracker服務下載文件的storage,參數為文件標識(卷名和文件名);然后tracker向客戶端返回一臺可用的storage;最后客戶端直接和storage通訊完成文件下載。
三、部署環境準備
1.環境說明
操作系統CentOS7.6
fastdfs版本:6.01
nginx版本:1.16.1
keepalived版本:2.0.19
2.系統依賴
?gcc?gcc-c++ perl pcre?pcre-devel?zlib?zlib-devel?openssl?openssl-devel libnl libnl-devel
3.軟件環境
libevent下載地址:http://libevent.org/
nginx下載地址:http://nginx.org/en/download.html
keepalived下載地址:https://www.keepalived.org/software/
fastdfs下載地址:https://github.com/happyfish200/fastdfs/releases
libfasttcommon下載地址:https://github.com/happyfish200/libfastcommon/releases
fastdfs-nginx-module下載地址:https://github.com/happyfish200/fastdfs-nginx-module/releases
4.機器及網絡環境規劃
Fdfs?Server?VIP:?????????192.168.100.110Tracker?Server1:?????????192.168.100.111Tracker?Server2:?????????192.168.100.112Storage?Group1?Node1:????192.168.100.111Storage?Group1?Node2:????192.168.100.112
5.防火墻設置
關閉系統防火墻:sudo systemctl stop firewalld && systemctl disable firewalld
四、Keepalived服務安裝配置
1.下載Keepalived源碼包
官網地址:https://www.keepalived.org/
下載地址:https://www.keepalived.org/software/keepalived-2.0.19.tar.gz
2.上傳并解壓Keepalived源碼包
tar -zxvf keepalived-2.0.19.tar.gz
3.編譯Keepalived準備
進入解壓目錄:cd keepalived-2.0.19
執行編譯準備:./configure --prefix=/work/keepalived
注意:一定要有gcc和openssl編譯相關的依賴
4.編譯安裝Keepalived
make && make install
5.安裝配置Keepalived
keepalived啟動時會從/etc/keepalived/中相關的目錄下查找keepalived.conf配置文件,因此將keepalived安裝錄/usr/local/keepalived/etc/keepalived.conf 拷貝到/etc/keepalived/中。
mkdir /etc/keepalived/
cp /work/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
cp /work/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
6.設置Keepalived開機啟動項
systemctl enable keepalived
然后就能使用systemctl start/stop/status keepalived管理keepalived了
7.配置Keepalived服務
192.168.100.111機器上配置:vi /etc/keepalived/keepalived.conf
vrrp_script?check_nginx?{ ???????interval?3 ???????script?"/work/script/check_nginx.sh"} ?vrrp_instance?fdfs_server?{ ???????state?MASTER ???????interface?enp0s3 ???????virtual_router_id?110 ???????priority?100 ???????advert_int?3 ???????authentication?{ ????????????auth_type?PASS ????????????auth_pass?password ???????} ???????virtual_ipaddress?{ ????????????192.168.100.110 ???????} ???????track_script?{ ????????????check_nginx ???????}}
192.168.100.112機器上配置:vi /etc/keepalived/keepalived.conf
vrrp_script?check_nginx?{ ???????interval?3 ???????script?"/work/script/check_nginx.sh"} ?vrrp_instance?fdfs_server?{ ???????state?BACKUP ???????interface?enp0s3 ???????virtual_router_id?110 ???????priority?90 ???????advert_int?3 ???????authentication?{ ????????????auth_type?PASS ????????????auth_pass?password ???????} ???????virtual_ipaddress?{ ????????????192.168.100.110 ???????} ???????track_script?{ ????????????check_nginx ???????}}
8.編寫nginx服務檢測腳本
vi /work/script/check_nginx.sh
#!/bin/bashactive_status=`netstat?-lntp|grep?nginx|wc?-l`if?[?$active_status?-gt?0?];?then ????exit?0else ????exit?1fi
?然后給腳本賦予執行權限:chmod +x /work/script/check_nginx.sh
9.修改內核參數
vi /etc/sysctl.conf
增加如下內容:
net.ipv4.ip_nonlocal_bind?=?1??#允許忽視VIP的存在net.ipv4.ip_forward?=?1??#允許轉發
sysctl --system 使配置生效
五、安裝FastDFS依賴庫
1.安裝libevent依賴
解壓libevent源碼包:tar -zxvf libevent-2.1.11-stable.tar.gz
進入源碼目錄:cd libevent-2.1.11-stable
編譯安裝前配置:./configure?
編譯安裝:make && make install
默認安裝位置:/usr/local/lib
2.安裝libfasttcommon依賴
解壓libfasttcommon源碼包:tar -zxvf libfastcommon-1.0.41.tar.gz
進入源碼目錄:cd libfastcommon-1.0.41
編譯安裝:./make.sh && ./make.sh install
默認安裝位置:/usr/lib64
六、安裝部署Tracker服務和Storage服務
1.安裝fastdfs服務
解壓fastdfs源碼包:tar -zxvf fastdfs-6.01.tar.gz
進入fastdfs源碼包:cd fastdfs-6.01
編譯安裝:./make.sh && ./make.sh install
2.fastdfs服務目錄信息
安裝完成后服務及腳本拷貝到/usr/bin 目錄,配置文件拷貝到/etc/fdfs目錄,啟動腳本拷貝到/etc/init.d/目錄
3.注冊開機啟動
chkconfig --add fdfs_trackerd
chkconfig fdfs_trackerd on
chkconfig --add fdfs_storaged
chkconfig fdfs_storaged on
4.數據目錄規劃
創建fdfs數據主目錄:mkdir /work/fdfs
創建tracker數據目錄:mkdir /work/fdfs/tracker
創建storage數據目錄:mkdir /work/fdfs/storage
5.配置tracker服務
將/etc/fdfs目錄下的tracker.conf.sample改為tracker.conf:mv tracker.conf.sample ?tracker.conf修改內容如下:
將base_path=/home/yuqing/fastdfs 改為:/work/fdfs/tracker(該目錄為上面定義創建)
啟動Tracker服務:systemctl start fdfs_trackerd
6.配置storage服務
將/etc/fdfs目錄下的storage.conf.sample改為storage.conf:mv storage.conf.sample storage.conf修改內容如下:
base_path=/home/yuqing/fastdfs 改為:base_path=/work/fdfs/storage(該目錄為上面定義創建)
store_path0=/home/yuqing/fastdfs 改為:store_path0=/work/fdfs/storage(該目錄為上面定義創建)
tracker_server=192.168.209.121:22122 改為:tracker_server=192.168.100.111:22122和tracker_server=192.168.100.112:22122
將/etc/fdfs/torage_ids.conf.sample 為storage_ids.conf,內容修改為當前group的storage節點信息:
100001? ? group1? ? 192.168.100.111
100002? ? group1? ? 192.168.100.112
啟動Storage服務:systemctl start fdfs_storaged
七、Nginx服務安裝配置
1.下載Nginx源碼包
官網地址:http://nginx.org/
下載地址:http://nginx.org/en/download.html
2.上傳并解壓Nginx源碼包及fastdfs插件包
tar -zxvf nginx-1.16.1.tar.gz
tar -zxvf fastdfs-nginx-module-1.21.tar.gz
3.編譯Nginx準備
進入解壓目錄:cd nginx-1.16.1
拷貝fastdfs插件包到nginx源碼目錄:mv ../fastdfs-nginx-module-1.21 .
執行編譯準備:./configure --prefix=/work/nginx \--with-stream \--add-module=fastdfs-nginx-module-1.21/src
注意:一定要有gcc和openssl編譯相關的依賴
4.編譯安裝Nginx
make && make install
5.注冊到系統服務
vi /usr/lib/systemd/system/nginx.service
[Unit]Description=nginxDocumentation=http://nginx.org/en/docs/After=network.target[Service]Type=forkingPIDFile=/work/nginx/logs/nginx.pidExecStartPre=/work/nginx/sbin/nginx?-t?-c?/work/nginx/conf/nginx.confExecStart=/work/nginx/sbin/nginx?-c?/work/nginx/conf/nginx.confExecReload=/bin/kill?-s?HUP?$MAINPIDExecStop=/bin/kill?-s?QUIT?$MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target
6.設置Nginx開機啟動項
systemctl enable nginx
然后就能使用systemctl start/stop/status nginx管理nginx了
7.修改Nginx配置
vi /work/nginx/conf/nginx.conf
#user??nobody;worker_processes??1;#error_log??logs/error.log;#error_log??logs/error.log??notice;#error_log??logs/error.log??info;#pid????????logs/nginx.pid;events?{ ????worker_connections??1024;}stream?{ ????upstream?tracker?{ ?????????server?192.168.100.111:22122?weight=1?max_fails=2?fail_timeout=10s; ?????????server?192.168.100.112:22122?weight=1?max_fails=2?fail_timeout=10s; ????} ????server?{ ????????listen???????7777; ????????proxy_timeout?5m; ????????proxy_pass?tracker; ????????proxy_connect_timeout?10s; ????}}http?{ ????include???????mime.types; ????default_type??application/octet-stream; ????sendfile????????on; ????keepalive_timeout??65; ????upstream?storage?{ ?????????server?192.168.100.111:8888?weight=1?max_fails=2?fail_timeout=10s; ?????????server?192.168.100.112:8888?weight=1?max_fails=2?fail_timeout=10s; ????} ????server?{ ????????listen???????80; ????????server_name??localhost; ???????? ????????location?/group1?{ ????????????proxy_pass?????????http://storage; ????????????proxy_set_header???Host?????????????$host; ????????????proxy_set_header???X-Real-IP????????$remote_addr; ????????????proxy_set_header???X-Forwarded-For??$proxy_add_x_forwarded_for; ????????} ???????? ????????error_page???500?502?503?504??/50x.html; ????????location?=?/50x.html?{ ????????????root???html; ????????} ????} ????server?{ ????????listen???????8888; ????????server_name??localhost; ????????location?/?{ ????????????alias?/work/fdfs/storage/data/; ????????????ngx_fastdfs_module; ????????} ????}}
8.配置nginx的fdfs插件
將/etc/fdfs下的http.conf.sample和mime.types.sample重命名為:http.conf和mime.types
將fastdfs-nginx-module-1.21/src下的mod_fastdfs.conf拷貝到/etc/fdfs下
修改mod_fastdfs.conf如下:
連接超時時間: connect_timeout=5
Tracker服務地址:tracker_server=192.168.100.111:22122? 和tracker_server=192.168.100.112:22122
Storage服務端口:storage_server_port=23000
如果文件ID的uri中包含/group**,則要設置為true:url_have_group_name = true
Storage配置的store_path0路徑,必須和storage.conf中的一致:store_path0=/work/fdfs/storage
其他詳細配置如下:
#?connect?timeout?in?seconds#?default?value?is?30sconnect_timeout=5#?network?recv?and?send?timeout?in?seconds#?default?value?is?30snetwork_timeout=10#?the?base?path?to?store?log?filesbase_path=/work/fdfs/storage#?if?load?FastDFS?parameters?from?tracker?server#?since?V1.12#?default?value?is?falseload_fdfs_parameters_from_tracker=true#?storage?sync?file?max?delay?seconds#?same?as?tracker.conf#?valid?only?when?load_fdfs_parameters_from_tracker?is?false#?since?V1.12#?default?value?is?86400?seconds?(one?day)storage_sync_file_max_delay?=?86400#?if?use?storage?ID?instead?of?IP?address#?same?as?tracker.conf#?valid?only?when?load_fdfs_parameters_from_tracker?is?false#?default?value?is?false#?since?V1.13use_storage_id?=?false#?specify?storage?ids?filename,?can?use?relative?or?absolute?path#?same?as?tracker.conf#?valid?only?when?load_fdfs_parameters_from_tracker?is?false#?since?V1.13storage_ids_filename?=?storage_ids.conf#?FastDFS?tracker_server?can?ocur?more?than?once,?and?tracker_server?format?is#??"host:port",?host?can?be?hostname?or?ip?address#?valid?only?when?load_fdfs_parameters_from_tracker?is?truetracker_server=192.168.100.111:22122tracker_server=192.168.100.112:22122#?the?port?of?the?local?storage?server#?the?default?value?is?23000storage_server_port=23000#?the?group?name?of?the?local?storage?servergroup_name=group1#?if?the?url?/?uri?including?the?group?name#?set?to?false?when?uri?like?/M00/00/00/xxx#?set?to?true?when?uri?like?${group_name}/M00/00/00/xxx,?such?as?group1/M00/xxx#?default?value?is?falseurl_have_group_name?=?true#?path(disk?or?mount?point)?count,?default?value?is?1#?must?same?as?storage.confstore_path_count=1#?store_path#,?based?0,?if?store_path0?not?exists,?it's?value?is?base_path#?the?paths?must?be?exist#?must?same?as?storage.confstore_path0=/work/fdfs/storage#?standard?log?level?as?syslog,?case?insensitive,?value?list:###?emerg?for?emergency###?alert###?crit?for?critical###?error###?warn?for?warning###?notice###?info###?debuglog_level=info#?set?the?log?filename,?such?as?/usr/local/apache2/logs/mod_fastdfs.log#?empty?for?output?to?stderr?(apache?and?nginx?error_log?file)log_filename=#?response?mode?when?the?file?not?exist?in?the?local?file?system##?proxy:?get?the?content?from?other?storage?server,?then?send?to?client##?redirect:?redirect?to?the?original?storage?server?(HTTP?Header?is?Location)response_mode=proxy#?the?NIC?alias?prefix,?such?as?eth?in?Linux,?you?can?see?it?by?ifconfig?-a#?multi?aliases?split?by?comma.?empty?value?means?auto?set?by?OS?type#?this?paramter?used?to?get?all?ip?address?of?the?local?host#?default?values?is?emptyif_alias_prefix=#?use?"#include"?directive?to?include?HTTP?config?file#?NOTE:?#include?is?an?include?directive,?do?NOT?remove?the?#?before?include#include?http.conf#?if?support?flv#?default?value?is?false#?since?v1.15flv_support?=?true#?flv?file?extension?name#?default?value?is?flv#?since?v1.15flv_extension?=?flv#?set?the?group?count#?set?to?none?zero?to?support?multi-group?on?this?storage?server#?set?to?0??for?single?group?only#?groups?settings?section?as?[group1],?[group2],?...,?[groupN]#?default?value?is?0#?since?v1.14group_count?=?1#?group?settings?for?group?#1#?since?v1.14#?when?support?multi-group?on?this?storage?server,?uncomment?following?section[group1]group_name=group1storage_server_port=23000store_path_count=1store_path0=/work/fdfs/storage#?group?settings?for?group?#2#?since?v1.14#?when?support?multi-group,?uncomment?following?section?as?neccessary#[group2]#group_name=group2#storage_server_port=23000#store_path_count=1#store_path0=/home/yuqing/fastdfs
八、服務啟動及驗證
分別啟動keepalive、nginx、tracker、storage服務
查看服務是否正常服務:
在任意Storage機器上查看集群狀態:fdfs_monitor /etc/fdfs/storage.conf
九、Java客戶端測試
1.java項目Maven依賴
項目地址:https://github.com/tobato/FastDFS_Client目前客戶端主要依賴于SpringBoot,因此必須引入:<parent> ???<groupId>org.springframework.boot</groupId> ???<artifactId>spring-boot-starter-parent</artifactId> ???<version>2.0.0.RELEASE</version> ???<relativePath/></parent>FastDFS?依賴包:<dependency> ????<groupId>com.github.tobato</groupId> ????<artifactId>fastdfs-client</artifactId> ????<version>1.26.7</version></dependency>將FastDFS引入項目:@Import(FdfsClientConfig.class)在application.yml當中配置Fdfs相關參數:fdfs: ??pool: ????#連接池最大數量 ????max-total:?200 ????#每個tracker地址的最大連接數 ????max-total-per-key:?50 ????#連接耗盡時等待獲取連接的最大毫秒數 ????max-wait-millis:?5000 ??so-timeout:?1500 ??connect-timeout:?600 ??thumb-image: ????width:?150 ????height:?150 ??tracker-list: ????-?192.168.100.110:7777或者fdfs: ??pool: ????#連接池最大數量 ????max-total:?200 ????#每個tracker地址的最大連接數 ????max-total-per-key:?50 ????#連接耗盡時等待獲取連接的最大毫秒數 ????max-wait-millis:?5000 ??so-timeout:?1500 ??connect-timeout:?600 ??thumb-image: ????width:?150 ????height:?150 ??tracker-list: ????-?192.168.100.111:22122 ????-?192.168.100.112:22122使用接口服務對Fdfs服務端進行操作,主要接口包括:TrackerClient?-?TrackerServer接口GenerateStorageClient?-?一般文件存儲接口?(StorageServer接口)FastFileStorageClient?-?為方便項目開發集成的簡單接口(StorageServer接口)AppendFileStorageClient?-?支持文件續傳操作的接口?(StorageServer接口)
2.代碼測試
package?com.maxbill;import?com.github.tobato.fastdfs.FdfsClientConfig;import?com.github.tobato.fastdfs.domain.fdfs.MetaData;import?com.github.tobato.fastdfs.domain.fdfs.StorePath;import?com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;import?com.github.tobato.fastdfs.service.FastFileStorageClient;import?lombok.extern.log4j.Log4j2;import?org.apache.commons.lang3.StringUtils;import?org.springframework.beans.factory.annotation.Autowired;import?org.springframework.context.annotation.Import;import?org.springframework.stereotype.Component;import?javax.annotation.PostConstruct;import?java.io.File;import?java.io.FileInputStream;import?java.text.DateFormat;import?java.text.SimpleDateFormat;import?java.util.*;@Log4j2@Component@Import(FdfsClientConfig.class)public?class?FdfsClientUtil?{????private?static?final?String?BASE_URL?=?"http://192.168.100.110:8888/";????@Autowired ????private?FastFileStorageClient?storageClient;????private?static?FdfsClientUtil?fdfsClientUtil;????@PostConstruct ????public?void?init()?{ ????????fdfsClientUtil?=?this; ????}????/** ?????*?文件上傳 ?????* ?????*?@param?file????文件信息 ?????*?@param?infoMap?文件擴展信息 ?????*?@return?上傳路徑 ?????*/ ????public?static?Map<String,?Object>?uploadFile(File?file,?Map<String,?String>?infoMap)?{????????try?{????????????String?fileName?=?file.getName();????????????String?fileType?=?fileName.substring(fileName.lastIndexOf("\\")?+?1);????????????//String?fileType?=?FilenameUtils.getExtension(file.getName()) ????????????log.info("[fdfs-upload]-start?upload?file?...?"); ????????????log.info("[fdfs-upload]-request?upload?file?name:?{}",?fileName); ????????????log.info("[fdfs-upload]-request?upload?file?info:?{}",?infoMap); ????????????StorePath?path?=?fdfsClientUtil.storageClient.uploadFile(new?FileInputStream(file),?file.length(),?fileType,?getMetaData(infoMap)); ????????????log.info("[fdfs-upload]-upload?success?path:?{}",?path.getFullPath());????????????return?getResultMap(BASE_URL.concat(path.getFullPath()),?null); ????????}?catch?(Exception?e)?{ ????????????log.error("[fdfs-upload]-upload?file?exception?info:?{}",?e.getMessage());????????????return?getResultMap(null,?e.getMessage()); ????????} ????}????/** ?????*?下載文件 ?????* ?????*?@param?filePath?文件路徑標識 ?????*?@return?文件字節 ?????*/ ????public?static?Map<String,?Object>?downloadFile(String?filePath)?{????????try?{ ????????????filePath?=?filePath.replace(BASE_URL,?""); ????????????StorePath?storePath?=?StorePath.parseFromUrl(filePath);????????????String?group?=?storePath.getGroup();????????????String?path?=?storePath.getPath(); ????????????log.info("[fdfs-download]-start?download?file?...?"); ????????????log.info("[fdfs-download]-request?download?file?group:?{}",?group); ????????????log.info("[fdfs-download]-request?download?file?path:?{}",?path); ????????????byte[]?data?=?fdfsClientUtil.storageClient.downloadFile(group,?path,?new?DownloadByteArray()); ????????????log.info("[fdfs-download]-request?download?file?success?...?");????????????return?getResultMap(data,?null); ????????}?catch?(Exception?e)?{ ????????????log.error("[fdfs-download]-download?file?exception?info:?{}",?e.getMessage());????????????return?getResultMap(null,?e.getMessage()); ????????} ????}????/** ?????*?刪除文件 ?????* ?????*?@param?filePath?文件路徑標識 ?????*?@return?操作結果 ?????*/ ????public?static?boolean?deleteFile(String?filePath)?{????????try?{ ????????????filePath?=?filePath.replace(BASE_URL,?""); ????????????StorePath?storePath?=?StorePath.parseFromUrl(filePath);????????????String?group?=?storePath.getGroup();????????????String?path?=?storePath.getPath(); ????????????log.info("[fdfs-delete]-start?delete?file?...?"); ????????????log.info("[fdfs-delete]-request?delete?file?group:?{}",?group); ????????????log.info("[fdfs-delete]-request?delete?file?path:?{}",?path); ????????????fdfsClientUtil.storageClient.deleteFile(storePath.getGroup(),?storePath.getPath()); ????????????log.info("[fdfs-delete]-request?delete?file?success?...?");????????????return?true; ????????}?catch?(Exception?e)?{ ????????????log.error("[fdfs-delete]-delete?file?exception?info:?{}",?e.getMessage());????????????return?false; ????????} ????}????/** ?????*?查看文件元信息 ?????* ?????*?@param?filePath?文件路徑標識 ?????*?@return?文件信息 ?????*/ ????public?static?Map<String,?Object>?getFileInfo(String?filePath)?{????????try?{ ????????????filePath?=?filePath.replace(BASE_URL,?""); ????????????StorePath?storePath?=?StorePath.parseFromUrl(filePath);????????????String?group?=?storePath.getGroup();????????????String?path?=?storePath.getPath(); ????????????log.info("[fdfs-meta]-start?meta?file?...?"); ????????????log.info("[fdfs-meta]-request?meta?file?group:?{}",?group); ????????????log.info("[fdfs-meta]-request?meta?file?path:?{}",?path); ????????????Map<String,?String>?infoMap?=?new?HashMap<>(); ????????????infoMap.put("createPath",?filePath); ????????????Set<MetaData>?metaData?=?fdfsClientUtil.storageClient.getMetadata(storePath.getGroup(),?storePath.getPath()); ????????????log.info("[fdfs-meta]-request?meta?file?success?...?");????????????if?(null?!=?metaData?&&?!metaData.isEmpty())?{ ????????????????metaData.forEach(meta?->?{ ????????????????????infoMap.put(meta.getName(),?meta.getValue()); ????????????????}); ????????????}????????????return?getResultMap(infoMap,?null); ????????}?catch?(Exception?e)?{ ????????????log.error("[fdfs-meta]-meta?file?exception?info:?{}",?e.getMessage());????????????return?getResultMap(null,?e.getMessage()); ????????} ????}????/** ?????*?封裝附件元信息 ?????* ?????*?@param?infoMap?自定義數據 ?????*?@return?附件元信息 ?????*/ ????private?static?Set<MetaData>?getMetaData(Map<String,?String>?infoMap)?{????????if?(null?!=?infoMap?&&?!infoMap.isEmpty())?{ ????????????Set<MetaData>?metaDataSet?=?new?HashSet<>();????????????for?(String?key?:?infoMap.keySet())?{ ????????????????metaDataSet.add(new?MetaData(key,?infoMap.get(key))); ????????????}????????????return?metaDataSet; ????????}?else?{ ????????????DateFormat?df?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss"); ????????????Set<MetaData>?metaDataSet?=?new?HashSet<>(); ????????????metaDataSet.add(new?MetaData("createUser",?"MaxBill")); ????????????metaDataSet.add(new?MetaData("createDate",?df.format(new?Date())));????????????return?metaDataSet; ????????} ????}????/** ?????*?封裝結果信息 ?????* ?????*?@param?data?數據 ?????*?@param?info?信息 ?????*?@return?操作結果 ?????*/ ????private?static?Map<String,?Object>?getResultMap(Object?data,?String?info)?{ ????????Map<String,?Object>?resultMap?=?new?HashMap<>();????????if?(StringUtils.isEmpty(info))?{ ????????????resultMap.put("flag",?true); ????????????resultMap.put("data",?data); ????????????resultMap.put("info",?"success"); ????????}?else?{ ????????????resultMap.put("flag",?false); ????????????resultMap.put("info",?info); ????????????resultMap.put("data",?null); ????????}????????return?resultMap; ????} }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。