JAVA使用Jedis连接redis cluster

最近在使用redis的cluster功能,首先是搭建redis cluster集群,不在多说,具体可参考http://yangchunhe.iteye.com/admin/blogs/2361550

然后就是客户端使用redis cluster集群,由于项目使用的java,随列举一下java的调用实例,本例子使用java开源框架jedis,具体测试代码如下:

public class JedisclusterTest extends TestCase{
	private static final int DEFAULT_TIMEOUT = 2000;
	private static final int DEFAULT_REDIRECTIONS = 5;
	private static final JedisPoolConfig DEFAULT_CONFIG = new JedisPoolConfig();
	
	public static JedisCluster getJc(){
		Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
		
	    jedisClusterNode.add(new HostAndPort("192.168.5.181", 6001));
	    jedisClusterNode.add(new HostAndPort("192.168.5.181", 6002));
	    jedisClusterNode.add(new HostAndPort("192.168.5.181", 6003));
	    jedisClusterNode.add(new HostAndPort("192.168.5.181", 6004));
	    jedisClusterNode.add(new HostAndPort("192.168.5.181", 6005));
	    jedisClusterNode.add(new HostAndPort("192.168.5.181", 6006));
//	    
	    JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT,
	        DEFAULT_REDIRECTIONS, null, DEFAULT_CONFIG);
	    return jc;
	}
	
	public void testcluster(){
		
		JedisCluster jc = getJc();
	    
	    jc.set("testkey4", "testkey hello");
	    //
	    jc.expire("testkey3", 100);
	    System.out.println(jc.get("testkey3"));
	    
	    try {
			jc.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 使用期间最坑的是包错连不上

大致报错信息如下:

Could not get a resource from the pool

多半原因是由于redis集群问题,跟java代码无关

展开阅读全文

没有更多推荐了,返回首页