项目启动报错:Redis health check failed
最近是重新开发整个项目,在上线测试的时候发现这个问题。
项目环境:SpringBoot2.x+Consul+Redission+Maven
报错的信息如下:
o.s.b.a.redis.RedisHealthIndicator - Redis health check failed
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
项目中引用了健康检查
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
通过spring-boot-actuator-autoconfigure:2.1.13.RELEASE的jar包找到了源码
源代码如下:
@Configuration
@ConditionalOnClass(RedisConnectionFactory.class)
@ConditionalOnBean(RedisConnectionFactory.class)
@ConditionalOnEnabledHealthIndicator("redis")
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
@AutoConfigureAfter({ RedisAutoConfiguration.class, RedisReactiveHealthIndicatorAutoConfiguration.class })
public class RedisHealthIndicatorAutoConfiguration
extends CompositeHealthIndicatorConfiguration<RedisHealthIndicator, RedisConnectionFactory> {
private final Map<String, RedisConnectionFactory> redisConnectionFactories;
public RedisHealthIndicatorAutoConfiguration(Map<String, RedisConnectionFactory> redisConnectionFactories) {
this.redisConnectionFactories = redisConnectionFactories;
}
@Bean
@ConditionalOnMissingBean(name = "redisHealthIndicator")
public HealthIndicator redisHealthIndicator() {
return createHealthIndicator(this.redisConnectionFactories);
}
}
解决方法1:
通过@ConditionalOnEnabledHealthIndicator可以知道解决办法,在配置文件中禁用redis检查
management:
health:
redis: ---禁用redis检查
# defaults: ---也可以禁用,不推荐
enabled: false
解决方法2:
查看源码发现RedisHealthIndicator继承AbstractHealthIndicator,AbstractHealthIndicator实现了HealthIndicator接口。我们可以通过自定义配置来解决。
@Configuration
public class RedisHealthIndicator implements HealthIndicator{
@Override
public Health health() {
return Health.up().build();
}
}
注意:我们必须要将原有的RedisHealthIndicator类覆盖,所以class名为RedisHealthIndicator。如果类想另起名则需指定bean名为redisHealthIndicator,否则还是会出现错误信息。
最后说下自己遇到的原因吧!属实有点尴尬。
自己项目上线遇到的问题则是怪自己粗心。只在redisson的配置文件中配置了redis连接,忘记在application.yml配置redis的连接信息。