文章首发于:clawhub.club


简介

分布式文件系统FastDFS非常适合中小型项目,比如说作为图片服务器。
happyfish100大神的github:fastdfs,fastdfs-client-java

简单的对客户端进行了连接池的封装,方便使用。

  • 系统启动,池子管理连接
  • 心跳确认连接是否可靠
  • 构造器模式创建连接池
  • 回调方式使用客户端

核心代码

初始化连接池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Build fast dfs conn pool.
*
* @return the fast dfs conn pool
*/
public FastDFSConnPool build() {
// 初始化空闲连接池
idleConnectionPool = new LinkedBlockingQueue<>(maxPoolSize);
//初始化全局参数
try {
ClientGlobal.init(confFileName);
} catch (IOException | MyException e) {
throw new RuntimeException("init client global exception.", e);
}
// 往线程池中添加默认大小的线程
TrackerServer trackerServer;
for (int i = 0; i < minPoolSize; i++) {
//获取到连接
trackerServer = createTrackerServer();
if (trackerServer != null) {
//放入空闲池
idleConnectionPool.offer(trackerServer);
}
}
// 注册心跳
new HeartBeat(this).beat();

return this;
}

客户端执行请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 执行方式
*
* @param <T> the type parameter
* @param invoke the invoke
* @return the t
*/
public <T> T processFdfs(CallBack<T> invoke) {
TrackerServer trackerServer = null;
T t;
try {
//获取tracker连接
trackerServer = fastDFSConnPool.checkOut();
//获取storage
StorageClient1 storageClient = new StorageClient1(trackerServer, null);
//执行操作
t = invoke.invoke(storageClient);
//释放连接
fastDFSConnPool.checkIn(trackerServer);
return t;
} catch (Exception e) {
//删除链接
fastDFSConnPool.drop(trackerServer);
throw new RuntimeException(e);
}
}

心跳

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 心跳任务
*/
private class HeartBeatTask implements Runnable {

@Override
public void run() {
LinkedBlockingQueue<TrackerServer> idleConnectionPool = fastDFSConnPool.getIdleConnectionPool();
TrackerServer ts = null;
for (int i = 0; i < idleConnectionPool.size(); i++) {
try {
ts = idleConnectionPool.poll(fastDFSConnPool.getWaitTimes(), TimeUnit.SECONDS);
if (ts != null) {
ProtoCommon.activeTest(ts.getSocket());
idleConnectionPool.add(ts);
} else {
//代表已经没有空闲长连接
break;
}
} catch (Exception e) {
//发生异常,要删除,进行重建
logger.error("heart beat conn have dead, and reconnect.", e);
fastDFSConnPool.drop(ts);
}
}

}
}

使用方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//初始化连接池
FastDFSConnPool fastDFSConnPool = new FastDFSConnPool()
.confFileName("./config/fdfs_client.conf")
.maxPoolSize(8)
.minPoolSize(1)
.reConnNum(2)
.waitTimes(2).build();

//使用客户端
FastDFSClient client = new FastDFSClient(fastDFSConnPool);
//上传 ileName 文件全路径 extName 文件扩展名,不包含(.) metas 文件扩展信息
String parts = client.processFdfs(storageClient -> storageClient.upload_file1("fileName", "extName", new NameValuePair[0]));
//下载 fileId: group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
byte[] bytes = client.processFdfs(storageClient -> storageClient.download_file1("fileId"));
//删除 -1失败,0成功
int result = client.processFdfs(storageClient -> storageClient.delete_file1("fileId"));
//获取远程服务器文件资源信息 groupName 文件组名 如:group1 remoteFileName M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg
FileInfo fileInfo = client.processFdfs(storageClient -> storageClient.get_file_info("groupName", "remoteFileName"));

github地址

FastDFS-Pool