代码如下
private static JedisCluster jedisCluster = null;
static{
Set nodes = new HashSet<>();
nodes.add(new HostAndPort("114.112.69.204", 7001));
nodes.add(new HostAndPort("114.112.69.204", 7002));
nodes.add(new HostAndPort("114.112.69.204", 7003));
nodes.add(new HostAndPort("114.112.69.204", 7004));
nodes.add(new HostAndPort("114.112.69.204", 7005));
nodes.add(new HostAndPort("114.112.69.204", 7006));
// 1、创建一个JedisCluster对象,构造方法,需要指定一个Set对象,set中包含HostAndPort对象。每个HostAndPort就是一个节点的地址。
jedisCluster = new JedisCluster(nodes);
/*JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(1000);
config.setMaxIdle(300);
config.setMaxWait(10000);
config.setTestOnBorrow(false);
jedisPool =new JedisPool(config, "114.112.69.204", 6379);*/
}
@RequestMapping("/index.html")
@ResponseBody
//加入redis缓存
public Map index(HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String, Object> result = new HashMap<String, Object>();
ObjectMapper objectMapper = new ObjectMapper();
IndexResult indexResult = null;
String indexJson = jedisCluster.hget("indexResult_KEY","indexResult666");
if (StringUtils.isNotBlank(indexJson)) {
try {//取出redis中的数据
indexResult = objectMapper.readValue(indexJson, IndexResult.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (jedisCluster != null) {
jedisCluster.close();
}
}
在正常请求时会报错!!!!主要错误如下
严重: Servlet.service() for servlet [goutrip_api] in context with path [] threw exception [Request processing failed; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: no reachable node in cluster] with root cause
redis.clients.jedis.exceptions.JedisConnectionException: no reachable node in cluster
at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnection(JedisSlotBasedConnectionHandler.java:54)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:113)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:131)
at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:30)
at redis.clients.jedis.JedisCluster.hget(JedisCluster.java:361)
at com.goutrip.controller.index.IndexController.index(IndexController.java:95)
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)
而使用命令行取出数据是没有问题的
![图片说明](https://img-ask.csdn.net/upload/201603/25/1458879946_317708.png)
要崩溃了啊!!!!快来救救我!!!!!!!!