我有一个使用Jedis的Redis集群配置的spring启动项目。该配置文件如下:Spring Boot执行程序:为什么Redis群集的运行状况不正确?
application.yml文件:
RedisClusterConfig.java文件:
@Configuration
公共类RedisClusterConfig {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${redis.cluster.host1}") private String HOST1;
@Value("${redis.cluster.port1}") private Integer PORT1;
@Value("${redis.cluster.host2}") private String HOST2;
@Value("${redis.cluster.port2}") private Integer PORT2;
@Value("${redis.cluster.host3}") private String HOST3;
@Value("${redis.cluster.port3}") private Integer PORT3;
@Value("${redis.cluster.host4}") private String HOST4;
@Value("${redis.cluster.port4}") private Integer PORT4;
@Value("${redis.cluster.host5}") private String HOST5;
@Value("${redis.cluster.port5}") private Integer PORT5;
@Value("${redis.cluster.host6}") private String HOST6;
@Value("${redis.cluster.port6}") private Integer PORT6;
@Bean
public JedisCluster jedisCluster() {
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
//Jedis Cluster will attempt to discover cluster nodes automatically
jedisClusterNodes.add(new HostAndPort(HOST1, PORT1));
jedisClusterNodes.add(new HostAndPort(HOST2, PORT2));
jedisClusterNodes.add(new HostAndPort(HOST3, PORT3));
if(StringUtils.isNotBlank(HOST4)){
jedisClusterNodes.add(new HostAndPort(HOST4, PORT4));
}else{
logger.warn("jedis cluster HOST4 not configured.");
}
if(StringUtils.isNotBlank(HOST5)){
jedisClusterNodes.add(new HostAndPort(HOST5, PORT5));
}else{
logger.warn("jedis cluster HOST5 not configured.");
}
if(StringUtils.isNotBlank(HOST6)){
jedisClusterNodes.add(new HostAndPort(HOST6, PORT6));
}else{
logger.warn("jedis cluster HOST6 not configured.");
}
JedisCluster jc = new JedisCluster(jedisClusterNodes, new JedisPoolConfig());
return jc;
}
}
使用此配置,一切工作正常。我可以向/从redis集群写入/读取数据。但是,当我访问http://localhost:64001/health以获得执行器健康状态时,返回结果如下:
{“status”:“DOWN”,“diskSpace”:{“status”:“UP”,“total”:120031539200 ,“free”:33381117952,“threshold”:10485760},“redis”:{“status”:“DOWN”,“error”:“org.springframework.data.redis.RedisConnectionFailureException:无法获得Jedis连接;嵌套异常为redis.clients.jedis.exceptions.JedisConnectionException:无法从池中获取资源“},”mongo“:{”status“:”UP“,”version“:”3.2.3“},”db“:{ “状态”: “UP”, “dataSource1”:{ “地位”: “UP”, “数据库”: “MySQL的”, “你好”:1}, “dataSource2”:{ “地位”: “UP”,”数据库“:”MySQL“,”hello“:1},”dataSource3“:{”status“:”UP“,”database“:”MySQL“,”hello“:1}}}
我们可以看到关于Jedis的错误提示。
BTW,相关的依赖关系的pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
有我可能会错过拿到/健康状况恢复到“UP”任何事情吗?谢谢!
更多信息: 我追溯了启动并发现RedisHealthIndicator通过了host = localhost和port = 6379的错误工厂,这应该是配置的集群主机和端口?
public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
this.redisConnectionFactory = connectionFactory;
}
请分享例外。 'JedisConnectionException:无法从池中获取资源'始终存在一个根本原因,该原因在帖子中不可见。 – mp911de
我无法获得例外详细信息。但是我发现在启动期间,以下代码通过一个错误的工厂,主机是localhost,端口是6379,它应该是集群主机和端口:'\t public RedisHealthIndicator(RedisConnectionFactory connectionFactory){ \t \t Assert.notNull(connectionFactory ,“ConnectionFactory不能为空”); \t \t this.redisConnectionFactory = connectionFactory; \t}' –