java操作redis集群连接池

redis3.0.6集群使用连接池,需要jedis-2.8.0.jar和commons-pool2-2.0.jar。 JedisCluster包含了所有节点的连接池,建议JedisCluster使用单例。JedisCluster每次操作完成后,不需要管理连接池的借还,它在内部已经完成。JedisCluster不用执行close()操作,它会将所有的JedisPool执行destory操作。

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
public class ClusterPoolUtil {
private static JedisCluster jedisCluster;
private static String redisConfigFile = “redis.properties”;
private static String hostAndPorts = null;
public static JedisCluster getJedisCluster(){
Properties props = new Properties();
try{
//加载连接池配置文件
props.load(RedisPoolUtil.class.getClassLoader().getResourceAsStream(redisConfigFile));
hostAndPorts = props.getProperty(“redis.ips.ports”);
if(jedisCluster==null){
int connectTimeOut = 10000;//连接超时
int soTimeOut =5000;//读写超时
int maxAttemts =10;//重试次数
Set jedisClusterNode = new HashSet();
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(Integer.valueOf(props.getProperty(“redis.maxActive”)));
poolConfig.setMaxIdle(Integer.valueOf(props.getProperty(“redis.maxIdle”)));
poolConfig.setMaxWaitMillis(Long.valueOf(props.getProperty(“redis.maxWaitMillis”)));
poolConfig.setTestOnBorrow(Boolean.valueOf(props.getProperty(“redis.testOnBorrow”)));
poolConfig.setTestOnReturn(Boolean.valueOf(props.getProperty(“redis.testOnReturn”)));
String[] hosts = hostAndPorts.split("\|\|");
for(String hostport:hosts){
String[] ipport = hostport.split("?;
String ip = ipport[0];
int port = Integer.parseInt(ipport[1]);
jedisClusterNode.add(new HostAndPort(ip, port));
}
if (jedisCluster == null) {
jedisCluster = new JedisCluster(jedisClusterNode,connectTimeOut,soTimeOut,maxAttemts, poolConfig);
}

        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return jedisCluster;
}

public static void set(String key,String value){
    jedisCluster = getJedisCluster();
    jedisCluster.set(key, value);
}

public static String get(String key){
    jedisCluster = getJedisCluster();
    String value = jedisCluster.get(key);
    return value;
}

public static void main(String[] args) {
    set("key1", "value1");
    System.out.println(get("key1"));
}

}

redis.properties

#自己定义的参数
redis.ips.ports=10.10.15.191:7000||10.10.15.191:7001||10.10.15.191:7002||10.10.15.191:7003||10.10.15.191:7004||10.10.15.191:7005||10.10.15.191:7006||10.10.15.191:7007

##连接池最大连接数,默认是8。
redis.maxActive=80

##连接池中最大空闲的连接数
redis.maxIdle=10
##连接池中最小空闲的连接数
redis.minIdle=10
#空闲连接的检测周期(单位为毫秒),默认-1 表示不做检测
redis.timeBetweenEvictionRunsMillis=60000
#做空闲连接检测时,每次的采样数,默认3
redis.numTestsPerEvictionRun=10

#当连接池用尽后,调用者是否要等待,此参数和maxWaitMillis对应的,只有当此参数为true时,maxWaitMills才会生效。
redis.blockWhenExhausted=false
#当连接池用尽后,调用者的最大等待时间(单位为毫秒),默认值为-1 表示永不超时,一直等待,一般不建议使用默认值。
redis.maxWaitMillis = 1000

##向连接池借用连接时是否做连接有效性检测(ping),无效连接会被移除,每次借出多执行一次ping命令,默认false。
redis.testOnBorrow=true
##向连接池归还连接时是否做连接有效性检测(ping),无效连接会被移除,每次借出多执行一次ping命令,默认false。
redis.testOnReturn=true
#向连接池借用连接时是否做连接空闲检测,空闲超时的连接会被移除,默认false。
redis.testWhileIdle=true

##当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能,单位毫秒
redis.timeout=3000

#连接的最小空闲时间,达到此值后空闲连接将被移除
redis.minEvictableIdleTimeMills=30000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值