java jedis cluster_redis cluster java client jedisCluster spring集成方法

该博客主要介绍了如何在Java中配置并使用JedisCluster来连接Redis集群。内容包括设置连接地址、超时时间、最大重定向次数以及通用对象池配置,通过FactoryBean和InitializingBean接口实现初始化操作。
摘要由CSDN通过智能技术生成

packagecom.www.core.utils;

importjava.util.HashSet;

importjava.util.Properties;

importjava.util.Set;

importjava.util.regex.Pattern;

importorg.apache.commons.pool2.impl.GenericObjectPoolConfig;

importorg.springframework.beans.factory.FactoryBean;

importorg.springframework.beans.factory.InitializingBean;

importorg.springframework.core.io.Resource;

importredis.clients.jedis.HostAndPort;

importredis.clients.jedis.JedisCluster;

public classJedisClusterFactoryimplementsFactoryBean, InitializingBean {

privateString address;

privateJedisCluster jedisCluster;

privateInteger timeout;

privateInteger maxRedirections;

privateGenericObjectPoolConfig genericObjectPoolConfig;

privatePattern p = Pattern.compile("^.+[:]\\d{1,5}\\s*$");

@Override

publicJedisCluster getObject()throwsException {

returnjedisCluster;

}

@Override

publicClass<?extendsJedisCluster> getObjectType() {

return(this.jedisCluster !=null?this.jedisCluster.getClass() : JedisCluster.class);

}

@Override

public booleanisSingleton() {

return true;

}

privateSet parseHostAndPort()throwsException {

try{

String[] addressArr=address.trim().split(",");

Set haps =newHashSet();

for(String addressStr:addressArr){

String[] ipAndPort = addressStr.trim().split(":");

HostAndPort hap =newHostAndPort(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1].trim()));

haps.add(hap);

}

returnhaps;

}catch(IllegalArgumentException ex) {

throwex;

}catch(Exception ex) {

throw newException("解析 jedis 配置文件失败", ex);

}

}

@Override

public voidafterPropertiesSet()throwsException {

Set haps =this.parseHostAndPort();

jedisCluster =newJedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);

}

public voidsetTimeout(inttimeout) {

this.timeout = timeout;

}

public voidsetMaxRedirections(intmaxRedirections) {

this.maxRedirections = maxRedirections;

}

/*** @Param String address to set*/

public voidsetAddress(String address) {

this.address = address;

}

public voidsetGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {

this.genericObjectPoolConfig = genericObjectPoolConfig;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值