Jedis操作Redis集群出现的问题

叶云轩 2016-09-21 02:22:08
搭好了Redis集群,但是java操作时出现异常
redis.clients.jedis.exceptions.JedisConnectionException: no reachable node in cluster
at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnection(JedisSlotBasedConnectionHandler.java:53)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:50)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:68)
at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:29)
at redis.clients.jedis.JedisCluster.set(JedisCluster.java:75)
at org.yzw.util.TokenUtil.saveToken(TokenUtil.java:130)
at org.yzw.service.impl.TokenImpl.createToken(TokenImpl.java:31)
at org.yzw.test.testLogin.testLogins(testLogin.java:29)
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.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
JedisUtil类代码如下:
package org.yzw.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;

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

/**
* 用于连接redis的工具类
* @author 叶云轩
* @date 2016-9-20:下午3:18:31
* @email ycountjavaXuan@outlook.com
* @description
*/
public class JedisUtil {
private static String ipAddress;

private static List<String> ports = new ArrayList<String>();

private static Properties pro = new Properties();

static{
InputStream is = null ;
try {
String path = System.getProperty("user.dir");
is =new FileInputStream(path+"\\config\\db.properties");
pro.load(is);
ipAddress = pro.getProperty("redis.url");
for (int i = 0; i <= 5; i++) {
ports.add(pro.getProperty("redis.port"+i));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


private final static ThreadLocal<JedisCluster> tl = new ThreadLocal<JedisCluster>();

/**
* 连接Redis集群
* @return 集群连接
*/
public static JedisCluster connRedis(){
JedisCluster cluster = tl.get();
if(cluster == null){
HashSet<HostAndPort> nodes = new HashSet<HostAndPort>();
for (int i = 0 ; i<ports.size();i++) {
nodes.add(new HostAndPort(ipAddress, Integer.parseInt(ports.get(i))));
System.out.println(i+"+++"+ipAddress+"-"+ports.get(i));
}
cluster = new JedisCluster(nodes);
tl.set(cluster);
}
return cluster;
}


/**
* 关闭连接
* @param cluster
*/
public static void close(JedisCluster cluster){
if(cluster!=null){
tl.remove();
cluster.close();
}
}
}
redis连接配置文件db.properties如下:
redis.url=192.168.142.130
redis.port0=7000
redis.port1=7001
redis.port2=7002
redis.port3=7003
redis.port4=7004
redis.port5=7005
Linux中集群运行情况如下:
该怎么解决
...全文
3068 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xc_xiaochou 2017-11-06
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
不要调用close方法,会关闭所有连接
我按照这个方法,成功解决了问题,虽然看起来比较荒唐,但是谢谢.http://www.cnblogs.com/zhangshiwen/p/5808820.html 这里放上一个解说地址
追梦的晓米 2017-05-01
  • 打赏
  • 举报
回复
我今天也遇到的类似的问题,博客最后怎么解决的啊
叶云轩 2016-09-23
  • 打赏
  • 举报
回复
层主,好像不是close()的原因。我在另一台虚拟机中重新搭建了一下redis集群环境,然后用这段代码去测试,没有问题。可能就是我第一次搭建的时候没有搭建好的原因吧~不过还是谢谢你哦~
  • 打赏
  • 举报
回复
不要调用close方法,会关闭所有连接

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧