您的位置:首页 > 编程语言 > Java开发

spring框架中由FactoryBean获取JedisCluster实例

2017-05-14 00:50 441 查看
spring配置文件:

<bean id="jedisCluster" class="com.pingan.ela.structure.ex.JedisClusterFactory">
<property name="addressConfig" value="classpath:context-ela-static.properties"/>
<property name="addressKeyPrefix" value="redis.cluster.address"/>
<property name="timeout" value="${redis.cluster.timeout}"/>
<property name="maxRedirections" value="${redis.cluster.maxRedirections}"/>
<property name="genericObjectPoolConfig" ref="genericObjectPoolConfig"/>
</bean>


JedisClusterFactory.java文件


import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;
import com.pingan.ela.structure.util.PropertiesUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.IPMaps;
import redis.clients.jedis.JedisCluster;

import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Created by YANHAO123 on 2016/3/1.
*/
public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {

private Resource addressConfig;
private String addressKeyPrefix ;
private JedisCluster jedisCluster;
private Integer timeout;
private Integer maxRedirections;
private GenericObjectPoolConfig genericObjectPoolConfig;

@Override
public JedisCluster getObject() throws Exception {
return jedisCluster;
}

@Override
public Class<? extends JedisCluster> getObjectType() {
return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
}

@Override
public boolean isSingleton() {
return true;
}

private Set<HostAndPort> parseHostAndPort() throws IOException {
Properties prop = new Properties();
prop.load(addressConfig.getInputStream());
// 增加非空判断,忽略值为空的配置
return Maps.filterKeys(prop, k -> ((String) k).startsWith(addressKeyPrefix)).values().stream().map(v -> (String) v).filter(StringUtils::isNotBlank).map(v -> v.split(":")).map(s -> new HostAndPort(s[0], Ints.tryParse(s[1]), s[2])).collect(Collectors.toSet());
}

@Override
public void afterPropertiesSet() throws Exception {
Set<HostAndPort> haps = this.parseHostAndPort();
jedisCluster = new JedisCluster(haps, timeout, maxRedirections, genericObjectPoolConfig);

}
public void setAddressConfig(Resource addressConfig) {
this.addressConfig = addressConfig;
}

public void setTimeout(int timeout) {
this.timeout = timeout;
}
public void setMaxRedirections(int maxRedirections) {
this.maxRedirections = maxRedirections;
}

public void setAddressKeyPrefix(String addressKeyPrefix) {
this.addressKeyPrefix = addressKeyPrefix;
}

public void setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
String[] oldIP = PropertiesUtils.getInstance().getProperty("redis.cluster.setIPMap.IP").split("[|]");
String[] newIP = PropertiesUtils.getInstance().getProperty("redis.cluster.setIPMap.natIP").split("[|]");
for(int i=0;i<oldIP.length;i++){
IPMaps.setIPMap(oldIP[i], newIP[i]);
}
this.genericObjectPoolConfig = genericObjectPoolConfig;
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: