redis cluster 集群连接

原创
2017/12/07 10:23
阅读数 213

redis cluster 集群连接帮助类:


import java.util.HashSet;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

/**
 * 
 * 
 * @desc 操作redis 的client
 * @author Wallker.Gao
 * @date 2017年3月23日下午7:36:37
 * 
 */

@Service("jedisCluster")
public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean{
	
	private JedisCluster jedisCluster;
	
	@Value("${redis.cluster.address}")
	private String addresses;
	
	@Value("${redis.cluster.maxWaitMillis}")
	private String maxWaitMillis;
	
	@Value("${redis.cluster.maxTotal}")
	private int maxTotal;
	
	@Value("${redis.cluster.minIdle}")
	private int minIdle;
	
	@Value("${redis.cluster.maxIdle}")
	private int maxIdle;
	
	@Value("${redis.cluster.timeout}")
	private int timeout;
	
	@Value("${redis.cluster.soTimeout}")
	private int soTimeout;
	
	@Value("${redis.cluster.password}")
	private String password;
	
	@Value("${redis.cluster.maxRedirections}")
	private int maxRedirections;
	
	@Override
	public JedisCluster getObject() throws Exception {
		
		return jedisCluster;
	}

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

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

	
	public JedisClusterFactory(){
		/*Set<HostAndPort> hostSet = new HashSet<>();
		String [] addressArray = addresses.split(",");
		for(String address : addressArray){
			String host = address.substring(0, address.indexOf(":"));
			String port = address.substring(address.indexOf(":"));
			HostAndPort hosts = new HostAndPort(host, Integer.parseInt(port));
			hostSet.add(hosts);
		}
		GenericObjectPoolConfig  genericPoolConfig = new GenericObjectPoolConfig();
		genericPoolConfig.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
		genericPoolConfig.setMaxTotal(maxTotal);
		genericPoolConfig.setMinIdle(minIdle);
		genericPoolConfig.setMaxIdle(maxIdle);
		jedisCluster = new JedisCluster(hostSet, timeout, soTimeout, maxRedirections, password, genericPoolConfig);*/
		
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Set<HostAndPort> hostSet = new HashSet<>();
		String [] addressArray = addresses.split(",");
		for(String address : addressArray){
			String host = address.substring(0, address.indexOf(":"));
			String port = address.substring(address.indexOf(":")+1);
			HostAndPort hosts = new HostAndPort(host, Integer.parseInt(port));
			hostSet.add(hosts);
		}
		GenericObjectPoolConfig  genericPoolConfig = new GenericObjectPoolConfig();
		genericPoolConfig.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
		genericPoolConfig.setMaxTotal(maxTotal);
		genericPoolConfig.setMinIdle(minIdle);
		genericPoolConfig.setMaxIdle(maxIdle);
		jedisCluster = new JedisCluster(hostSet, timeout, soTimeout, maxRedirections, password, genericPoolConfig);
	}
	
}
                             

参数:

redis.cluster.address=192.168.1.33:6371,192.168.1.34:6373,192.168.1.35:6375,192.168.1.33:6374,192.168.1.34:6376,192.168.1.35:6372
redis.cluster.maxWaitMillis=-1
redis.cluster.maxTotal=1000
redis.cluster.minIdle=8
redis.cluster.maxIdle=100
redis.cluster.timeout=1000
redis.cluster.soTimeout=800
redis.cluster.password=chiwen@1234567890DZZG
redis.cluster.maxRedirections=6
redis.cluster.database=1

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部