commons-pool2的使用,创建JedisCluster池
Posted IT_Holmes
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了commons-pool2的使用,创建JedisCluster池相关的知识,希望对你有一定的参考价值。
1. common-pool2是干什么的?
apache commons-pool是apache基金会的一个开源对象池组件,我们常用的数据库连接池dpcp和redis的java客户端jedis都使用commons-pool来管理连接
common-pool2.jar包主要就是池化技术,如果我们多个连接想要达到数据池的那种效果。它就是最好的选择。
2. commons-pool2的使用
开始就是导包commons-pool2的包和Jedis的包。
因为,这里我搭建出了集群效果,操作集群的是JedisCluster对象。如果是普通redis服务,只需要将JedisCluster换成Jedis对象就可以了。
这里主要是连接redis集群的效果:
下面的几种对象要知道:
- PooledObject 池化后的对象
- ObjectPool 对象池
- PooledObjectFactory 池对象工厂
- GenericObjectPool
package com.itholmes.utils;
import java.util.Date;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
//池化技术:BasePooledObjectFactory。BasePooledObjectFactory是common-pool2的一个类。
public class JedisClusterPoolFactory extends BasePooledObjectFactory<JedisCluster>
private HostAndPort hostAndPort = new HostAndPort("127.0.0.1", 6379);
public JedisClusterPoolFactory()
public JedisClusterPoolFactory(HostAndPort hostAndPort)
this.hostAndPort = hostAndPort;
//对象创建
@Override
public JedisCluster create() throws Exception
System.out.println("创建:"+new Date());
JedisCluster cluster = new JedisCluster(hostAndPort);
return cluster;
//wrap包装
@Override
public PooledObject<JedisCluster> wrap(JedisCluster obj)
System.out.println("包装:" + new Date());
PooledObject<JedisCluster> pooledObject = new DefaultPooledObject<JedisCluster>(obj);
return pooledObject;
//activate激活
@Override
public void activateObject(PooledObject<JedisCluster> pooledObject) throws Exception
System.out.println("激活:" + new Date());
super.activateObject(pooledObject);
//对象销毁
@Override
public void destroyObject(PooledObject<JedisCluster> pooledObject) throws Exception
System.out.println("销毁:" + new Date());
JedisCluster cluster = pooledObject.getObject();
cluster.close();
super.destroyObject(pooledObject);
package com.itholmes.utils;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisCluster;
public class JedisClusterUtils
//创建GenericObjectPoolConfig对象配置
static GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
//获得genericObjectPool对象,连接池对象
static GenericObjectPool<JedisCluster> objectPool = new GenericObjectPool<JedisCluster>(new JedisClusterPoolFactory(),genericObjectPoolConfig);
static
//最大闲置数
genericObjectPoolConfig.setMaxIdle(10);
//最小闲置数
genericObjectPoolConfig.setMinIdle(5);
//最大连接数
genericObjectPoolConfig.setMaxTotal(100);
public static JedisCluster getJedisCluster()
//JedisCluster对象使用该对象来连接redis集群
JedisCluster cluster = null;
try
//通过连接池生成jedisCluster对象
cluster = objectPool.borrowObject();
catch (Exception e)
// TODO Auto-generated catch block
e.printStackTrace();
return cluster;
/*
* String类型方法封装:(再往下就封装的jedisCluster操作redis集群的命令。)
*/
public static Long setnx_Clus(String key,String value)
JedisCluster cluster = getJedisCluster();
Long setnx = cluster.setnx(key, value);
cluster.close();
return setnx;
public static String get_Clus(String key)
JedisCluster cluster = getJedisCluster();
String string = cluster.get(key);
cluster.close();
return string;
public static String setex_Clus(String key,String value,int seconds)
JedisCluster cluster = getJedisCluster();
String setex = cluster.setex(key, seconds, value);
cluster.close();
return setex;
public static Long ttl_Clus(String key)
JedisCluster cluster = getJedisCluster();
Long ttl = cluster.ttl(key);
cluster.close();
return ttl;
public static Long incrBy_Clus(String key,Long increment)
JedisCluster cluster = getJedisCluster();
Long by = cluster.incrBy(key, increment);
cluster.close();
return by;
//因为是集群的缘故,必须要用组的方式来传值
public static String mset_Clus(Map<String, String> map)
String keysvalues = "";
Set set = map.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext())
Object next = iterator.next();
Map.Entry entry = (Map.Entry)next;
keysvalues += entry.getKey()+","+entry.getValue()+",";
keysvalues = keysvalues.substring(0, keysvalues.length()-1);
JedisCluster cluster = getJedisCluster();
String mset = cluster.mset(keysvalues);
cluster.close();
return mset;
public static List<String> mget_Clus(List<String> list)
String keys = "";
for(String l:list)
keys+=l+",";
keys = keys.substring(0, keys.length()-1);
JedisCluster cluster = getJedisCluster();
List<String> list2 = cluster.mget(keys);
cluster.close();
return list2;
/*
* Hash类型方法封装:
*/
public static String hget_Clus(String key,String field)
JedisCluster cluster = getJedisCluster();
String hget = cluster.hget(key, field);
cluster.close();
return hget;
public static Long hset_Clus(String key,String field,String value)
JedisCluster cluster = getJedisCluster();
Long hset = cluster.hset(key,field,value);
cluster.close();
return hset;
public static Long hdel_Clus(String key,String field)
JedisCluster cluster = getJedisCluster();
Long hdel = cluster.hdel(key, field);
cluster.close();
return hdel;
public static Long hincrby_Clus(String key , String field , Long value)
JedisCluster cluster = getJedisCluster();
Long hincrBy = cluster.hincrBy(key, field, value);
cluster.close();
return hincrBy;
public static Set<String> hkeys_Clus(String key)
JedisCluster cluster = getJedisCluster();
Set<String> set = cluster.hkeys(key);
cluster.close();
return set;
public static List<String> hvals_Clus(String key)
JedisCluster cluster = getJedisCluster();
List<String> list = cluster.hvals(key);
cluster.close();
return list;
/*
* List类型方法封装:
*/
public static Long lpush_Clus(String key,String string)
JedisCluster cluster = getJedisCluster();
Long lpush = cluster.lpush(key, string);
cluster.close();
return lpush;
public static String lindex_Clus(String key,int index)
JedisCluster cluster = getJedisCluster();
String lindex = cluster.lindex(key, index);
cluster.close();
return lindex;
public static String lpop_Clus(String key)
JedisCluster cluster = getJedisCluster();
String lpop = cluster.lpop(key);
cluster.close();
return lpop;
public static String lset_Clus(String key,int index,String value)
JedisCluster cluster = getJedisCluster();
String lset = cluster.lset(key, index, value);
cluster.close();
return lset;
/*
* Set类型方法封装:
*/
public static Long sadd_Clus(String key,String member)
JedisCluster cluster = getJedisCluster();
Long sadd = cluster.sadd(key, member);
cluster.close();
return sadd;
public static Set<String> smembers_Clus(String key)
JedisCluster cluster = getJedisCluster();
Set<String> set = cluster.smembers(key);
cluster.close();
return set;
public static Boolean sismember_Clus(String key,String member)
JedisCluster cluster = getJedisCluster();
Boolean sismember = cluster.sismember(key, member);
cluster.close();
return sismember;
public static Long srem_Clus(String key,String member)
JedisCluster cluster = getJedisCluster();
Long srem = cluster.srem(key, member);
cluster.close();
return srem;
/*
* sorted set类型方法封装:
*/
public static Long zadd_Cluster(String key,double score,String member)
JedisCluster cluster = getJedisCluster();
Long zadd = cluster.zadd(key, score,member);
cluster.close();
return zadd;
public static Set<String> zrange_Clus(String key,int start,int stop)
JedisCluster cluster = getJedisCluster();
Set<String> set = cluster.zrange(key, start, stop);
cluster.close();
return set;
public static Double zincrby_Clus(String key,int increment,String member)
JedisCluster cluster = getJedisCluster();
Double zincrby = cluster.zincrby(key, increment, member);
cluster.close();
return zincrby;
public static Long zrem_Clus(String key,String members)
JedisCluster cluster = getJedisCluster();
Long zrem = cluster.zrem(key, members);
cluster.close();
return zrem;
public static Double zscore_Clus(String key,String member)
JedisCluster cluster = getJedisCluster();
Double zscore = cluster.zscore(key, member);
cluster.close();
return zscore;
public static Long zcount_Clus(String key,Long min,Long max)
JedisCluster cluster = getJedisCluster();
Long zcount = cluster.zcount(key, min, max);
cluster.close();
return zcount;
public static Long zcard_Clus(String key)
JedisCluster cluster = getJedisCluster();
Long zcard 以上是关于commons-pool2的使用,创建JedisCluster池的主要内容,如果未能解决你的问题,请参考以下文章
redis连接池 jedis-2.9.0.jar+commons-pool2-2.4.2.jar
commons-pool2中GenericObjectPoolConfig的maxTotalmaxIdleminIdle属性理解
commons-pool2中GenericObjectPoolConfig的maxTotalmaxIdleminIdle属性理解