Redis集群错误:Caused by: redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster
使用spring-data-redis练习连接redis集群时出现以下错误信息
Exception in thread "main" org.springframework.data.redis.RedisConnectionFailureException: No reachable node in cluster; nested exception is redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:65)
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:42)
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:44)
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:42)
at org.springframework.data.redis.connection.jedis.JedisClusterConnection.convertJedisAccessException(JedisClusterConnection.java:777)
at org.springframework.data.redis.connection.jedis.JedisClusterStringCommands.convertJedisAccessException(JedisClusterStringCommands.java:525)
at org.springframework.data.redis.connection.jedis.JedisClusterStringCommands.set(JedisClusterStringCommands.java:118)
at org.springframework.data.redis.connection.DefaultedRedisConnection.set(DefaultedRedisConnection.java:281)
at org.springframework.data.redis.core.DefaultValueOperations$3.inRedis(DefaultValueOperations.java:240)
at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:60)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:228)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:188)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:96)
at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:236)
at com.etoak.ClusterTest.main(ClusterTest.java:15)
Caused by: redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster
at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnection(JedisSlotBasedConnectionHandler.java:69)
at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:86)
at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:102)
at redis.clients.jedis.JedisClusterCommand.runBinary(JedisClusterCommand.java:49)
at redis.clients.jedis.BinaryJedisCluster.set(BinaryJedisCluster.java:105)
at org.springframework.data.redis.connection.jedis.JedisClusterStringCommands.set(JedisClusterStringCommands.java:116)
... 8 more
采用的是java 配置文件的方式来配置redis,配置文件如下
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
@Configuration
public class ClusterConfig {
@Bean
public JedisPoolConfig poolConfig(){
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(50);
poolConfig.setMaxIdle(50);
poolConfig.setMinIdle(1);
return poolConfig;
}
@Bean
public RedisClusterConfiguration clusterConfiguration(){
RedisClusterConfiguration config = new RedisClusterConfiguration();
Set<RedisNode> nodes = new HashSet<>();
for(int port = 6001;port <= 6006;port++){
nodes.add(new RedisNode("192.168.60,128",port));
}
config.setClusterNodes(nodes);
return config;
}
@Bean
public JedisConnectionFactory connectionFactory(){
return new JedisConnectionFactory(this.clusterConfiguration(),this.poolConfig());
}
@Bean("stringRedisTemplate")
public StringRedisTemplate stringRedisTemplate(){
return new StringRedisTemplate(this.connectionFactory());
}
@Bean("redisTemplate")
public RedisTemplate redisTemplate(){
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(this.connectionFactory());
return template;
}
}
经过排查是在设置RedisClusterConfiguration
设置主机是IP地址写错了
把 192.168.60.128 写成了 192.168.60,128。
修改clusterConfiguration()
方法后,代码成功运行
@Bean
public RedisClusterConfiguration clusterConfiguration(){
RedisClusterConfiguration config = new RedisClusterConfiguration();
Set<RedisNode> nodes = new HashSet<>();
for(int port = 6001;port <= 6006;port++){
nodes.add(new RedisNode("192.168.60.128",port));
}
config.setClusterNodes(nodes);
return config;
}