非常感谢 http://blog.csdn.net/Mr_Smile2014/article/details/52441824
FastDFS 技术学习,更多知识请访问https://www.itkc8.com
公司使用fastdfs文件系统来存储文件和图片,为了避免每个系统都直接通过客户端直接访问fastdfs文件系统,所以我们做了一个
dubbo服务。要实现图片上传、下载的服务或工程直接调用dubbo接口就可以了。为了提高性能对fastdfs文件系统TrackerServer写了个
连接池,同时写了空闲连接是否可用的监测类。
下面对连接池的具体实现作介绍。
连接池类
实现配置文件的读取、初始化各项参数、创建默认的TrackerServer数、空闲连接获取、释放繁忙连接及删除不可用的连接;
[java] view plain copy
- package com.xxxx.fastdfs.pool;
- import java.io.IOException;
- import java.util.UUID;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.TimeUnit;
- import org.csource.fastdfs.ClientGlobal;
- import org.csource.fastdfs.TrackerClient;
- import org.csource.fastdfs.TrackerServer;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.xxxx.common.AppException;
- import com.xxxx.fastdfs.exception.ERRORS;
- /**
- *
- * @ClassName: ConnectionPool
- * @Description: fastdfs连接池
- * @author mr_smile2014 [email protected]
- * @date 2015年10月26日 下午3:36:02
- *
- */
- public class ConnectionPool {
- private static final Logger LOGGER = LoggerFactory
- .getLogger(ConnectionPool.class);
- /** 空闲的连接池 */
- private LinkedBlockingQueue
idleConnectionPool = null; - /** 连接池默认最小连接数 */
- private long minPoolSize = 10;
- /** 连接池默认最大连接数 */
- private long maxPoolSize = 30;
- /** 当前创建的连接数 */
- private volatile long nowPoolSize = 0;
- /** 默认等待时间(单位:秒) */
- private long waitTimes = 200;
- /** fastdfs客户端创建连接默认1次 */
- private static final int COUNT = 1;
- /**
- * 默认构造方法
- */
- public ConnectionPool(long minPoolSize, long maxPoolSize, long waitTimes) {
- String logId = UUID.randomUUID().toString();
- LOGGER.info("[线程池构造方法(ConnectionPool)][" + logId
- + "][默认参数:minPoolSize=" + minPoolSize + ",maxPoolSize="
- + maxPoolSize + ",waitTimes=" + waitTimes + "]");
- this.minPoolSize = minPoolSize;
- this.maxPoolSize = maxPoolSize;
- this.waitTimes = waitTimes;
- /** 初始化连接池 */
- poolInit(logId);
- /** 注册心跳 */
- HeartBeat beat = new HeartBeat(this);
- beat.beat();
- }
- /**
- *
- * @Description: 连接池初始化 (在加载当前ConnectionPool时执行) 1).加载配置文件 2).空闲连接池初始化;
- * 3).创建最小连接数的连接,并放入到空闲连接池;
- *
- */
- private void poolInit(String logId) {
- try {
- /** 加载配置文件 */
- initClientGlobal();
- /** 初始化空闲连接池 */
- idleConnectionPool = new LinkedBlockingQueue
(); - /** 往线程池中添加默认大小的线程 */
- for (int i = 0; i < minPoolSize; i++) {
- createTrackerServer(logId, COUNT);
- }
- } catch (Exception e) {
- LOGGER.error("[FASTDFS初始化(init)--异常][" + logId + "][异常:{}]", e);
- }
- }
- /**
- *
- * @Description: 创建TrackerServer,并放入空闲连接池
- *
- */
- public void createTrackerServer(String logId, int flag) {
- LOGGER.info("[创建TrackerServer(createTrackerServer)][" + logId + "]");
- TrackerServer trackerServer = null;
- try {
- TrackerClient trackerClient = new TrackerClient();
- trackerServer = trackerClient.getConnection();
- while (trackerServer == null && flag < 5) {
- LOGGER.info("[创建TrackerServer(createTrackerServer)][" + logId
- + "][第" + flag + "次重建]");
- flag++;
- initClientGlobal();
- trackerServer = trackerClient.getConnection();
- }
- org.csource.fastdfs.ProtoCommon.activeTest(trackerServer
- .getSocket());
- idleConnectionPool.add(trackerServer);
- /** 同一时间只允许一个线程对nowPoolSize操作 **/
- synchronized (this) {
- nowPoolSize++;
- }
- } catch (Exception e) {
- LOGGER.error("[创建TrackerServer(createTrackerServer)][" + logId
- + "][异常:{}]", e);
- } finally {
- if (trackerServer != null) {
- try {
- trackerServer.close();
- } catch (Exception e) {
- LOGGER.error("[创建TrackerServer(createTrackerServer)--关闭trackerServer异常]["
- + logId + "][异常:{}]", e);
- }
- }
- }
- }
- /**
- *
- * @Description: 获取空闲连接 1).在空闲池(idleConnectionPool)中弹出一个连接;
- * 2).把该连接放入忙碌池(busyConnectionPool)中; 3).返回 connection
- * 4).如果没有idle connection, 等待 wait_time秒, and check again
- *
- * @throws AppException
- *
- */
- public TrackerServer checkout(String logId) throws AppException {
- LOGGER.info("[获取空闲连接(checkout)][" + logId + "]");
- TrackerServer trackerServer = idleConnectionPool.poll();
- if (trackerServer == null) {
- if (nowPoolSize < maxPoolSize) {
- createTrackerServer(logId, COUNT);
- try {
- trackerServer = idleConnectionPool.poll(waitTimes,
- TimeUnit.SECONDS);
- } catch (Exception e) {
- LOGGER.error("[获取空闲连接(checkout)-error][" + logId
- + "][error:获取连接超时:{}]",e);
- throw ERRORS.WAIT_IDLECONNECTION_TIMEOUT.ERROR();
- }
- }
- if (trackerServer == null) {
- LOGGER.error("[获取空闲连接(checkout)-error][" + logId
- + "][error:获取连接超时(" + waitTimes + "s)]");
- throw ERRORS.WAIT_IDLECONNECTION_TIMEOUT.ERROR();
- }
- }
- LOGGER.info("[获取空闲连接(checkout)][" + logId + "][获取空闲连接成功]");
- return trackerServer;
- }
- /**
- *
- * @Description: 释放繁忙连接 1.如果空闲池的连接小于最小连接值,就把当前连接放入idleConnectionPool;
- * 2.如果空闲池的连接等于或大于最小连接值,就把当前释放连接丢弃;
- *
- * @param client1
- * 需释放的连接对象
- *
- */
- public void checkin(TrackerServer trackerServer, String logId) {
- LOGGER.info("[释放当前连接(checkin)][" + logId + "][prams:" + trackerServer
- + "] ");
- if (trackerServer != null) {
- if (idleConnectionPool.size() < minPoolSize) {
- idleConnectionPool.add(trackerServer);
- } else {
- synchronized (this) {
- if (nowPoolSize != 0) {
- nowPoolSize--;
- }
- }
- }
- }
- }
- /**
- *
- * @Description: 删除不可用的连接,并把当前连接数减一(调用过程中trackerServer报异常,调用一般在finally中)
- * @param trackerServer
- *
- */
- public void drop(TrackerServer trackerServer, String logId) {
- LOGGER.info("[删除不可用连接方法(drop)][" + logId + "][parms:" + trackerServer
- + "] ");
- if (trackerServer != null) {
- try {
- synchronized (this) {
- if (nowPoolSize != 0) {
- nowPoolSize--;
- }
- }
- trackerServer.close();
- } catch (IOException e) {
- LOGGER.info("[删除不可用连接方法(drop)--关闭trackerServer异常][" + logId
- + "][异常:{}]", e);
- }
- }
- }
- private void initClientGlobal() throws Exception {
- ClientGlobal.init("fastdfs_client.conf");
- }
- public LinkedBlockingQueue
getIdleConnectionPool() { - return idleConnectionPool;
- }
- public long getMinPoolSize() {
- return minPoolSize;
- }
- public void setMinPoolSize(long minPoolSize) {
- if (minPoolSize != 0) {
- this.minPoolSize = minPoolSize;
- }
- }
- public long getMaxPoolSize() {
- return maxPoolSize;
- }
- public void setMaxPoolSize(long maxPoolSize) {
- if (maxPoolSize != 0) {
- this.maxPoolSize = maxPoolSize;
- }
- }
- public long getWaitTimes() {
- return waitTimes;
- }
- public void setWaitTimes(int waitTimes) {
- if (waitTimes != 0) {
- this.waitTimes = waitTimes;
- }
- }
- }
连接池定时检测器
用于定时检测连接池空闲队列中的连接是否可用,如果不可用将从连接池中移除。
[java] view plain copy
- package com.xxxx.fastdfs.pool;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.UUID;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.TimeUnit;
- import org.csource.fastdfs.TrackerServer;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- *
- * @ClassName: HeartBeat
- * @Description: 连接池定时器设置
- * @author mr_smile2014 [email protected]
- * @date 2015年1月14日 下午2:21:29
- *
- */
- public class HeartBeat {
- private static final Logger LOGGER = LoggerFactory
- .getLogger(HeartBeat.class);
- /** fastdfs连接池 */
- private ConnectionPool pool = null;
- /** 小时毫秒数 */
- public static int ahour = 1000 * 60 * 60 * 1;
- /** 等待时间 */
- public static int waitTimes = 200;
- public HeartBeat(ConnectionPool pool) {
- this.pool = pool;
- }
- /**
- *
- * @Description: 定时执行任务,检测当前的空闲连接是否可用,如果不可用将从连接池中移除
- *
- */
- public void beat() {
- LOGGER.info("[心跳任务方法(beat)]");
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- String logId = UUID.randomUUID().toString();
- LOGGER.info("[心跳任务方法(beat)]["
- + logId
- + "][Description:对idleConnectionPool中的TrackerServer进行监测]");
- LinkedBlockingQueue
idleConnectionPool = pool - .getIdleConnectionPool();
- TrackerServer ts = null;
- for (int i = 0; i < idleConnectionPool.size(); i++) {
- try {
- ts = idleConnectionPool.poll(waitTimes,
- TimeUnit.SECONDS);
- if (ts != null) {
- org.csource.fastdfs.ProtoCommon.activeTest(ts
- .getSocket());
- idleConnectionPool.add(ts);
- } else {
- /** 代表已经没有空闲长连接 */
- break;
- }
- } catch (Exception e) {
- /** 发生异常,要删除,进行重建 */
- LOGGER.error("[心跳任务方法(beat)][" + logId
- + "][异常:当前连接已不可用将进行重新获取连接]");
- pool.drop(ts, logId);
- }
- }
- }
- };
- Timer timer = new Timer();
- timer.schedule(task, ahour, ahour);
- }
- }
连接池调用
[java] view plain copy
- /** 获取fastdfs服务器连接 */
- trackerServer = connectionPool.checkout(logId);
- StorageServer storageServer = null;
- StorageClient1 client1 = new StorageClient1(trackerServer,
- storageServer);
- /** 以文件字节的方式上传 */
- ..........................
- /** 上传完毕及时释放连接 */
- connectionPool.checkin(trackerServer, logId);
连接池的使用最好是再写一个工具类,在工具类当中进行连接池的初始化及上传、下载、删除功能的实现。把工具类配置到spring配置文件中,
生成对应的bean,其它用到的地方直接调用该bean的对应方法就可以了。工具类的实现将在下一篇文章中分享。
FastDFS 技术学习,更多知识请访问https://www.itkc8.com