Commons pool2 配置详解及其在Jedis中的使用

本人博客同步发表,排版更佳

Commons pool2配置参数(以Jedis连接为例)

  • MaxTotal: 最大连接数(空闲+使用中)
  • MaxIdle: 最大空闲连接数
  • MinIdle: 最小空闲连接数
  • MaxWaitMillis: 借出连接时最大的等待时间,超时抛错
  • TimeBetweenEvictionRunsMillis: 后台检测线程周期
  • MinEvictableIdleTimeMillis:硬闲置时间,连接多久没有使用设置为闲置,检测线程直接剔除闲置,为保持最小空闲数,被剔除后可能重新生成
  • SoftMinEvictableIdleTimeMillis:软闲置时间,连接多久没有使用设置为闲置,当空闲连接 > MinIdle,才执行剔除闲置,否则维持最小空闲数,即使闲置了也不会剔除,设置MinEvictableIdleTimeMillis后,本参数无效
  • testOnBorrow:在borrow一个连接时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
  • testOnReturn:在连接return给pool时,是否提前进行validate操作;
  • testWhileIdle:如果为true,表示后台线程对idle连接进行扫描,如果validate失败,此连接会被从pool中剔除;只有在timeBetweenEvictionRunsMillis大于0时才有意义;

JedisPool验证参数配置

用以下代码验证配置参数的作用,其中Ctg开头的是我封装了的,可以使用JedisPool相关类代替

public static void main(String[] args) {
        BasicConfigurator.configure();

        List hostAndPortList = new ArrayList(); //地址列表
        HostAndPort host1 = new HostAndPort("132.122.232.225",9081); 
        hostAndPortList.add(host1);

        GenericObjectPoolConfig poolConfig = new JedisPoolConfig(); //线程池配置
        poolConfig.setMaxIdle(2); //最大空闲连接数
        poolConfig.setMaxTotal(3); // 最大连接数(空闲+使用中)
        poolConfig.setMinIdle(1); //保持的最小空闲连接数
        poolConfig.setMaxWaitMillis(2000); //借出连接时最大的等待时间
        poolConfig.setTimeBetweenEvictionRunsMillis(1000); //1秒运行一次检测线程
        //硬闲置、软闲置配置
        //硬闲置  3秒没有占用设置为闲置, 检测线程直接剔除闲置,保持的最小空闲数,会被剔除且重新生成
        poolConfig.setMinEvictableIdleTimeMillis(3000);
        //软闲置  3秒没有占用设置为闲置, 当空闲连接>最小空闲数,才执行剔除闲置连接,否则维持最小空闲数,即使闲置了也不会剔除
        poolConfig.setSoftMinEvictableIdleTimeMillis(3000);

        CtgJedisPoolConfig config = new CtgJedisPoolConfig(hostAndPortList);
        config.setDatabase(4970) //分组对应的桶位
                .setPassword("disk#Test1234") // “用户#密码”
                .setPoolConfig(poolConfig) //线程池配置
                .setPeriod(1000)  //后台监控执行周期,毫秒
                .setMonitorTimeout(100);  //后台监控ping命令超时时间,毫秒

        CtgJedisPool pool = new CtgJedisPool(config); //创建连接池
        ProxyJedis jedis_1 = null;
        ProxyJedis jedis_2 = null;
        ProxyJedis jedis_3 = null;

        //最大连接为3,以下三次获取能成功
        try {
            logger.info("=================== get jedis_1 =========================");
            jedis_1 = pool.getResource();
        } catch (CtgJedisPoolException e) {
            logger.error("jedis_1 : Get resource error");
        }

        try {
            logger.info("=================== get jedis_2 =========================");
            jedis_2 = pool.getResource();
        } catch (CtgJedisPoolException e) {
            logger.error("jedis_2 : Get resource error");
        }

        try {
            logger.info("=================== get jedis_3 =========================");
            jedis_3 = pool.getResource();
        } catch (CtgJedisPoolException e) {
            logger.error("jedis_3 : Get resource error");
        }

        //第四次获取连接,失败。获取连接超时时间为2秒,重试一次,正确结果应该为4秒
        ProxyJedis jedis_4 = null;
        long startTime = 0L;
        try {
            logger.info("=================== get jedis_4 =========================");
            startTime = System.currentTimeMillis();
            jedis_4 = pool.getResource();
            logger.info("Get resource cost time : " + (System.currentTimeMillis() - startTime));
        } catch (CtgJedisPoolException e) {
            logger.info("Get resource cost time : " + (System.currentTimeMillis() - startTime));
            logger.error("jedis_4 : Get resource error");
        }

        // 归还一个连接,空闲连接数:1 活跃连接数:2,
        // 等待后台执行剔除空闲连接,当前空闲连接数 = 需要保持最小空闲连接数,不会进行剔除,
        // 硬闲置情况下达到限制时间,会被剔除
        try{
            logger.info("=================== close jedis_1 =========================");
            jedis_1.close();  //归还连接至连接池
        } catch (Throwable e) {

        }

        try {
            logger.info("============== Sleep 10 ===============");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 继续归还一个连接,空闲连接数:2 活跃连接数:1,
        // 等待后台执行剔除空闲连接,当前空闲连接数 > 需要保持最小空闲连接数,进行剔除
        try{
            logger.info("=================== close jedis_2 =========================");
            jedis_2.close();  //归还连接至连接池
        } catch (Throwable e) {

        }

        try {
            logger.info("============== Sleep 9 ===============");
            Thread.sleep(9000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        logger.info("-------------------------------pool close-----------------------------------");
        pool.close(); //关闭连接池
    }

poolConfig.setMinEvictableIdleTimeMillis(3000);
硬闲置配置输出结果

# 前三次获取成功
=================== get jedis_1 =========================
- makeJedis-- 132.122.232.225:9081
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 0 NumIdle: 0 NumWaiters: 0}
- =================== get jedis_2 =========================
- makeJedis-- 132.122.232.225:9081
- =================== get jedis_3 =========================
- makeJedis-- 132.122.232.225:9081

# 达到最大连接数,第四次获取失败,失败时间为4,与预期相同
# NumWaiters = 1
- =================== get jedis_4 =========================
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 3 NumIdle: 0 NumWaiters: 1}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 3 NumIdle: 0 NumWaiters: 1}
- Get jedis from ableNodes error at 132.122.232.225:9081 :Could not get a resource from the pool
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 3 NumIdle: 0 NumWaiters: 1}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 3 NumIdle: 0 NumWaiters: 1}
- Get jedis from ableNodes error at 132.122.232.225:9081 :Could not get a resource from the pool
- Get resource cost time : 4003
- jedis_4 : Get resource error

# 归还一个连接
- =================== close jedis_1 =========================
- ============== Sleep 10 ===============
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
# NumIdle: 1到达3秒硬闲置时间,剔除,重新 makeJedis
- makeJedis-- 132.122.232.225:9081
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
# NumIdle: 1到达3秒硬闲置时间,剔除,重新 makeJedis
- makeJedis-- 132.122.232.225:9081
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}

# 归还第二个连接
- =================== close jedis_2 =========================
- ============== Sleep 9 ===============
# 未超过最大空闲连接数2,NumIdle+1 = 2
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 2 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 2 NumWaiters: 0}
# 到达3秒硬闲置时间,剔除,NumIdle - 1 = 1 = minIdle,不需要 makeJedis
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
# NumIdle: 1到达3秒硬闲置时间,剔除,重新 makeJedis
- makeJedis-- 132.122.232.225:9081
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
# NumIdle: 1到达3秒硬闲置时间,剔除,重新 makeJedis
- makeJedis-- 132.122.232.225:9081
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- -------------------------------pool close-----------------------------------

poolConfig.setSoftMinEvictableIdleTimeMillis(3000);
软闲置配置输出结果

# 省略前面相同部分
# 归还一个连接
- =================== close jedis_1 =========================
- ============== Sleep 10 ===============
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
# NumIdle: 1到达3秒软闲置时间,但 NumIdle = minIdle 不剔除,没有makeJedis日志输出
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
# NumIdle: 1到达3秒软闲置时间,但 NumIdle = minIdle 不剔除,没有makeJedis日志输出
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 2 NumIdle: 1 NumWaiters: 0}

# 归还第二个连接
- =================== close jedis_2 =========================
- ============== Sleep 9 ===============
# 未超过最大空闲连接数2,NumIdle+1 = 2
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 2 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 2 NumWaiters: 0}
# NumIdle: 2到达3秒软闲置时间,NumIdle > minIdle 剔除,
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
# NumIdle: 1到达3秒软闲置时间,但 NumIdle = minIdle 不剔除,没有makeJedis日志输出
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
# NumIdle: 1到达3秒软闲置时间,但 NumIdle = minIdle 不剔除,没有makeJedis日志输出
- nodePoolMap: {132.122.232.225:9081=132.122.232.225:9081 NumActive: 1 NumIdle: 1 NumWaiters: 0}
- -------------------------------pool close-----------------------------------

从上述日志输出,验证了各参数的作用,其中 软闲置、硬闲置配置的区别,在于剔除闲置连接时,是否需要考虑当前 空闲连接与minIdle的大小关系。

此外,同时配置了软、硬闲置参数以后,输出结果与单独配置硬闲置参数相同,即
设置MinEvictableIdleTimeMillis后,SoftMinEvictableIdleTimeMillis参数无效

你可能感兴趣的:(Commons pool2 配置详解及其在Jedis中的使用)