Spring Boot actuator: 为什么Redis集群的Health状态不正确?
Spring Boot actuator: Why is the Health status of Redis cluster not correct?
我有一个 spring 引导项目,其中使用 Jedis 配置了 redis 集群。配置文件如下:
application.yml 文件:
RedisClusterConfig.java 文件:
@Configuration
public class 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;
}
}
使用此配置,一切正常。我可以write/read数据to/fromredis集群。但是当我访问http://localhost:64001/health获取执行器健康状态时,它returns结果如下:
{"status":"DOWN","diskSpace":{"status":"UP","total":120031539200,"free" :33381117952,"threshold":10485760},"redis":{"status":"DOWN","error":"org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool"},"mongo": {"status":"UP","version":"3.2.3"},"db":{"status":"UP","dataSource1" :{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"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>
为了让 /health 状态恢复到 "UP" 有什么我可能会错过的吗?谢谢!
更多信息:
跟踪启动,发现RedisHealthIndicator传递了一个错误的factory,host=localhost,port=6379,应该是配置的集群主机和端口?
public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
this.redisConnectionFactory = connectionFactory;
}
最后,我通过重新配置redis解决了问题,如下所示:
spring:
redis:
cluster:
nodes:
- 192.168.0.17:6390
- 192.168.0.17:6391
- 192.168.0.17:6392
- 192.168.0.9:6390
- 192.168.0.9:6391
- 192.168.0.9:6392
我有一个 spring 引导项目,其中使用 Jedis 配置了 redis 集群。配置文件如下:
application.yml 文件:
RedisClusterConfig.java 文件:
@Configuration
public class 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;
}
}
使用此配置,一切正常。我可以write/read数据to/fromredis集群。但是当我访问http://localhost:64001/health获取执行器健康状态时,它returns结果如下:
{"status":"DOWN","diskSpace":{"status":"UP","total":120031539200,"free" :33381117952,"threshold":10485760},"redis":{"status":"DOWN","error":"org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool"},"mongo": {"status":"UP","version":"3.2.3"},"db":{"status":"UP","dataSource1" :{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"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>
为了让 /health 状态恢复到 "UP" 有什么我可能会错过的吗?谢谢!
更多信息: 跟踪启动,发现RedisHealthIndicator传递了一个错误的factory,host=localhost,port=6379,应该是配置的集群主机和端口?
public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
this.redisConnectionFactory = connectionFactory;
}
最后,我通过重新配置redis解决了问题,如下所示:
spring:
redis:
cluster:
nodes:
- 192.168.0.17:6390
- 192.168.0.17:6391
- 192.168.0.17:6392
- 192.168.0.9:6390
- 192.168.0.9:6391
- 192.168.0.9:6392