//创建集群节点集合
Set nodes=new HashSet<>();
nodes.add(new HostAndPort("192.168.193.129", 7001));
nodes.add(new HostAndPort("192.168.193.129", 7002));
nodes.add(new HostAndPort("192.168.193.129", 7003));
nodes.add(new HostAndPort("192.168.193.129", 7004));
nodes.add(new HostAndPort("192.168.193.129", 7005));
nodes.add(new HostAndPort("192.168.193.129", 7006));
//创建集群对象
JedisCluster cluster=new JedisCluster(nodes);
cluster.set("cluster1", "Hello World!");
System.out.println(cluster.get("cluster1"));
cluster.close();
环境:redis.3.0+CentOS-6.4
运行上述程序后报错:redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?(大概意思是 连接数太多重定向了吗?!)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:34)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:67)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:83)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:67)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:83)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:67)
at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:29)
at redis.clients.jedis.JedisCluster.set(JedisCluster.java:70)
at com.lm.test1.JedisTest.testJedisCluster(JedisTest.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
解决方案:
第一种:在创建cluster的时候,带上超时时间和最大重定向数。
JedisCluster cluster=new JedisCluster(nodes,3000,1000);
第二种:查看 cluster nodes 信息
get cluster1
-> Redirected to slot [8483] located at 127.0.0.1:7002
"Hello World!"
连接超时,配置集群详细ip