FastDFS介紹:
FastDFS是一個開源的輕量級分布式文件系統(tǒng),它對文件進(jìn)行管理蜜自,功能包括:文件存儲菩貌、文件同步、文件訪問(文件上傳袁辈、文件下載)等菜谣,解決了大容量存儲和負(fù)載均衡的問題。
特別適合以文件為載體的在線服務(wù)晚缩,如相冊網(wǎng)站尾膊、視頻網(wǎng)站等等。FastDFS為互聯(lián)網(wǎng)量身定制荞彼,充分考慮了冗余備份冈敛、負(fù)載均衡、線性擴(kuò)容等機(jī)制鸣皂,并注重高可用抓谴、高性能等指標(biāo)暮蹂,使用FastDFS很容易搭建一套高性能的文件服務(wù)器集群提供文件上傳、下載等服務(wù)癌压。
FastDFS服務(wù)端有兩個角色:跟蹤器(tracker)和存儲節(jié)點(diǎn)(storage)仰泻。跟蹤器主要做調(diào)度工作,在訪問上起負(fù)載均衡的作用滩届。
FastDFS中的文件標(biāo)識分為兩個部分:卷名和文件名集侯,二者缺一不可。
FastDFS主要內(nèi)容:
tracker:追蹤者服務(wù)器帜消,主要用于協(xié)調(diào)調(diào)度棠枉,可以起到負(fù)載均衡的作用,記錄storage的相關(guān)狀態(tài)信息泡挺。
storage:存儲服務(wù)器辈讶,用于保存文件以及文件的元數(shù)據(jù)信息。
group:組娄猫,同組節(jié)點(diǎn)提供冗余備份贱除,不同組用于擴(kuò)容。
mata data:文件的元數(shù)據(jù)信息稚新,比如長寬信息勘伺,圖片后綴,視頻的幀數(shù)等褂删。
FastDFS安裝:
第一步:基礎(chǔ)環(huán)境準(zhǔn)備
yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget git
第二步:主體安裝飞醉,主要安裝以下內(nèi)容
libfatscommon:FastDFS分離出的一些公用函數(shù)包
FastDFS:FastDFS本體
fastdfs-nginx-module:FastDFS和nginx的關(guān)聯(lián)模塊
nginx:發(fā)布訪問服務(wù)
安裝libfatscommon:
unzip libfastcommon-master.zip
cd libfastcommon-master/
./make.sh
./make.sh install
安裝FastDFS:
unzip fastdfs-master.zip
cd fastdfs-master/
./make.sh
./make.sh install
安裝fastdfs-nginx-module:
unzip fastdfs-nginx-module-master.zip
cp fastdfs-nginx-module-master/src/mod_fastdfs.conf /etc/fdfs
安裝nginx,設(shè)置安裝目錄屯阀,添加FastDFS和nginx關(guān)聯(lián)模塊缅帘,并且添加ssl模塊方便后期配置HTTPS域名訪問:
tar -zxvf nginx-1.22.0.tar.gz
cd nginx-1.22.0/
./configure --prefix=/usr/local/nginx --add-module=/data/fastdfs-nginx-module-master/src --with-http_ssl_module
make
make install
到此,所有的安裝已經(jīng)結(jié)束难衰,但是配置文件還沒配置弯囊,所以所有的服務(wù)都不能啟動岂津,不然就會報(bào)錯吕晌。
FastDFS配置:
配置:1個Tracker+3個Storage熄诡,分別用來存放圖片、視頻鳄虱、其他文件弟塞。
數(shù)據(jù)文件存放目錄:/data/fileServer/,在該文件夾下創(chuàng)建以下目錄:
cd /data/fileServer/
mkdir client storage tracker
cd storage
mkdir image video file
配置文件放置目錄:/etc/fdfs/
配置tracker.conf拙已,主要配置如下决记,其他配置使用默認(rèn)配置即可:
# tracker server 服務(wù)端口
port=22122
# 存放數(shù)據(jù)和日志的基礎(chǔ)路徑
base_path=/data/fileServer/tracker
# 文件上傳選擇組的策略
# 0: 輪詢
# 1: 指定組
# 2: 均衡,選擇空間空閑最大的組
store_lookup=2
# 當(dāng)store_lookup為1時倍踪,必須指定組的名字系宫,該組名字必須時真實(shí)存在的
# 如果store_lookup如果是0或2索昂,則此參數(shù)無效
store_group=image
# tracker server的HTTP端口
http.server_port=8080
配置storage_image.conf、storage_video.conf和stroage_file.conf扩借,主要配置如下椒惨,其他配置使用默認(rèn)配置即可:
# 組名稱
group_name=image
# 組服務(wù)端口
port=23000
# 該組存放數(shù)據(jù)和日志的路徑
base_path=/data/fileServer/storage/image
store_path0=/data/fileServer/storage/image
# 結(jié)合nginx的一個對外服務(wù)端口號,與nginx監(jiān)聽端口保持一致
http.server_port=8888
# 組名稱
group_name=video
# 組服務(wù)端口
port=23001
# 該組存放數(shù)據(jù)和日志的路徑
base_path=/data/fileServer/storage/video
store_path0=/data/fileServer/storage/video
# 結(jié)合nginx的一個對外服務(wù)端口號潮罪,與nginx監(jiān)聽端口保持一致
http.server_port=8888
# 組名稱
group_name=file
# 組服務(wù)端口
port=23002
# 該組存放數(shù)據(jù)和日志的路徑
base_path=/data/fileServer/storage/file
store_path0=/data/fileServer/storage/file
# 結(jié)合nginx的一個對外服務(wù)端口號框产,與nginx監(jiān)聽端口保持一致
http.server_port=8888
配置client.conf,主要配置如下错洁,其他配置使用默認(rèn)配置即可:
# 存儲日志文件的路徑
base_path=/data/fileServer/client
# 這里建議使用公網(wǎng)IP,方便從互聯(lián)網(wǎng)上傳文件
tracker_server=49.232.105.121:22122
# tracker服務(wù)器的 http端口號戒突,必須等于tracker.conf中的http.server_port值
http.tracker_server_port=8080
到這里屯碴,F(xiàn)astDFS文件服務(wù)系統(tǒng)已經(jīng)配置成功了,此時可以啟動服務(wù)膊存,在服務(wù)器上面操作上傳文件了导而。
啟動服務(wù):
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage_image.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage_video.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage_file.conf
上傳文件,根據(jù)配置文件的配置隔崎,上傳組策略采用均衡(load balance)方式:
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /data/des.png
文件上傳成功返回信息如下:
file/M00/00/00/rBUAD2NkfdmAFH4aAAygMuNF7cs093.png
圖片可以上傳了今艺,但是如果通過瀏覽器查看,這個時候就需要配置插件mod_fastdfs和nginx爵卒。
配置mod_fastdfs.conf虚缎,主要配置如下,其他配置使用默認(rèn)配置即可:
# tracker server的ip和端口
tracker_server=49.232.105.121:22122
# 是否在訪問地址中加入組信息钓株,比如${group_name}/M00/00/00/xxx
url_have_group_name = true
# 分組數(shù)量实牡,必須與下方group settings中g(shù)roup組實(shí)際數(shù)一致,不然瀏覽器查看時會報(bào)404
# 如果下方group settings中沒有設(shè)置group組轴合,則為使用單一組创坞,設(shè)置為0
group_count = 3
# 組設(shè)置,主要包括組名字受葛、端口题涨、存儲數(shù)量、存儲地址总滩,與storage配置文件中的信息保持一致
[group1]
group_name=image
storage_server_port=23000
store_path_count=1
store_path0=/data/fileServer/storage/image
[group2]
group_name=video
storage_server_port=23001
store_path_count=1
store_path0=/data/fileServer/storage/video
[group3]
group_name=file
storage_server_port=23002
store_path_count=1
store_path0=/data/fileServer/storage/file
配置nginx纲堵,添加以下端口監(jiān)聽:
#文件服務(wù)器
server {
listen 8888;
server_name 49.232.105.121;
location ~/image|video|file/ {
ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
另有兩個配置文件,http.conf和mime.types咳秉,使用默認(rèn)配置即可婉支,無需修改。
啟動nginx:
cd /usr/local/nginx/sbin
./nginx
通過瀏覽器查看剛才上次的圖片:
http://49.232.105.121:8888/file/M00/00/00/rBUAD2NkfdmAFH4aAAygMuNF7cs093.png
Java客戶端操作FastDFS的使用:
第一步:配置依賴
<dependency>
<groupId>cn.bestwu</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
</dependency>
第二步:配置fdfsClient_config.properties
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
http.tracker_http_port = 8080
tracker_server =49.232.105.121:22122
第三步:測試用例
package cn.tongmap.client;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import javax.imageio.ImageIO;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class FastDFSClient {
private static final String CONF_FILENAME = new FastDFSClient().getClass().getClassLoader().getResource("fdfsClient_config.properties").getPath();
static {
try {
// 加載配置文件
ClientGlobal.init(CONF_FILENAME);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
}
/**
* 上傳圖片到指定的group
* @param uploadFilePath
* @param width
* @param height
* @param groupName
* @return
* @throws Exception
*/
public static String[] uploadFileSpecifyGroup(String uploadFilePath, int width, int height, String groupName) throws Exception {
String[] results = null;
String fileExtName = "";
if (uploadFilePath.contains(".")) {
fileExtName = uploadFilePath.substring(uploadFilePath.lastIndexOf(".") + 1);
} else {
return results;
}
//建立連接
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
FileInputStream fileInputStream = new FileInputStream(new File(uploadFilePath));
byte[] file_buff = null;
if(fileInputStream != null){
int len = fileInputStream.available();
file_buff = new byte[len];
fileInputStream.read(file_buff);
}
//設(shè)置元信息
NameValuePair[] metas = new NameValuePair[3];
metas[0] = new NameValuePair("width", "" + width);
metas[1] = new NameValuePair("heigth", "" + height);
metas[2] = new NameValuePair("author", "bss");
results = storageClient.upload_file(groupName, file_buff, fileExtName, metas);
System.out.println(Arrays.toString(results));
trackerServer.close();
fileInputStream.close();
return results;
}
/**
* 上傳圖片澜建,load balance
* @param uploadFilePath
* @param width
* @param height
* @return
* @throws Exception
*/
public static String[] uploadFileBalance(String uploadFilePath, int width, int height) throws Exception {
String[] results = null;
String fileExtName = "";
if (uploadFilePath.contains(".")) {
fileExtName = uploadFilePath.substring(uploadFilePath.lastIndexOf(".") + 1);
} else {
return results;
}
//建立連接
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
FileInputStream fileInputStream = new FileInputStream(new File(uploadFilePath));
byte[] file_buff = null;
if(fileInputStream != null){
int len = fileInputStream.available();
file_buff = new byte[len];
fileInputStream.read(file_buff);
}
//設(shè)置元信息
NameValuePair[] metas = new NameValuePair[3];
metas[0] = new NameValuePair("width", "" + width);
metas[1] = new NameValuePair("heigth", "" + height);
metas[2] = new NameValuePair("author", "bss");
results = storageClient.upload_file(uploadFilePath, fileExtName, metas);
System.out.println(Arrays.toString(results));
trackerServer.close();
fileInputStream.close();
return results;
}
public static void main(String[] args) throws Exception{
String image = "D://Perfect Match.jpg";
BufferedInputStream bufInputStream = new BufferedInputStream(new FileInputStream(new File(image)));
BufferedImage bi = ImageIO.read(bufInputStream);
int width = bi.getWidth();
int height = bi.getHeight();
uploadFileSpecifyGroup(image, width, height, "image");
uploadFileBalance(image, width, height);
}
}
#FastDFS安裝包向挖,各個配置文件的配置蝌以,以及Java客戶端操作FastDFS的使用。
https://download.csdn.net/download/kzhzhang/86901476