java连接redis集群

package redis;

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

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

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

public class ClusterDemo {
	private static JedisCluster jedisCluster=null;
	private static Set hostAndPorts=null;
	
	public static  Set getHostAndPort(String hostAndPort){
		Set hap = new HashSet();
		String[] hosts = hostAndPort.split(",");
		String[] hs = null;
		for(String host:hosts){
			hs=host.split(":");
			hap.add(new HostAndPort(hs[0], Integer.parseInt(hs[1])));
		}
		return hap;
	}
	
	public static JedisCluster getJedisCluster(){
		GenericObjectPoolConfig gopc = new GenericObjectPoolConfig();
		gopc.setMaxTotal(32);
		gopc.setMaxIdle(4);
		gopc.setMaxWaitMillis(6000);
		hostAndPorts = getHostAndPort("192.168.10.250:6379");
	    jedisCluster = new JedisCluster(hostAndPorts, 2000, 2000, 3,"redis123456",gopc);
		return jedisCluster;
	}
	
	public static void main(String[] args) {
		jedisCluster = getJedisCluster();
		System.out.println(jedisCluster.get("name"));
	}
}

你可能感兴趣的:(redis)