https://mp.weixin.qq.com/s/NB9KVhtWj1UaepqHKZ4gKQredis
今天在博客的交流区收到一条不错的问题,拿出来给你们分享一下。具体问题以下:
由于项目里面用到了redis集群,但并非用spring boot的配置方式,启动后项目健康检查总是检查redis的时候状态为down,致使注册到eureka后项目状态也是down。spring
"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" }
问下能不能设置spring boot不检查 redis的健康状态。微信
问题原帖:http://qa.didispace.com/?/question/7
欢迎你们来此交流ide
如提问者所述,因为在Spring Boot项目中引用了Redis模块,因此Spring Boot Actuator会对其进行健康检查,正常状况下不会出现问题,可是因为采用了其余配置方式,致使redis的链接检查没有经过。这样就会致使了Consul或Eureka的HealthCheck认为该服务是DOWN状态。ui
那么redis的健康检查是如何实现的呢?咱们不妨来看看健康检查的自动化配置中针对redis的配置源码:this
@Configuration @ConditionalOnBean(RedisConnectionFactory.class) @ConditionalOnEnabledHealthIndicator("redis") public static class RedisHealthIndicatorConfiguration extends CompositeHealthIndicatorConfiguration<RedisHealthIndicator, RedisConnectionFactory> { @Autowired private Map<String, RedisConnectionFactory> redisConnectionFactories; @Bean @ConditionalOnMissingBean(name = "redisHealthIndicator") public HealthIndicator redisHealthIndicator() { return createHealthIndicator(this.redisConnectionFactories); } }
以上内容取自:
org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration类。spa
从自动化配置中咱们能够看到,会自动加载一个针对redis的HealthIndicator实现,而且该Bean命名为redisHealthIndicator。code
经过上面的分析,咱们已经知道了是哪一个Bean致使了服务实例的健康检查不经过,那么如何解决该问题的方法也就立刻能想到了:咱们只须要再实现一个redis的HealthIndicator
实现来替代原先默认的检查逻辑。好比:blog
@Component public class RedisHealthIndicator implements HealthIndicator { @Override public Health health() { return Health.up().build(); } }
上面经过实现HealthIndicator
接口中的health方法,直接返回了up状态。经过@Component
注解,让Spring Boot扫描到该类就能自动的进行加载,并覆盖原来的redis健康检查实现。固然,这里的实现并很差,由于它只是为了让健康检查能够经过,可是并无作真正的健康检查。如提问者所说,采用了其余配置访问,那么正确的作法就是在health
方法中实现针对其余配置的内容进行健康检查。接口
注意:这里隐含了一个实现命名的问题,因为默认的bean名称会使用redisHealthIndicator
,因此这里的定义能够替换默认的实现,由于它的名字与@ConditionalOnMissingBean(name = "redisHealthIndicator")
中的命名一致。可是若是您自定义的实现类并不是叫RedisHealthIndicator
,它的默认名称与自动化配置的名称是不匹配的,那么就不会替换,这个时候须要在@Component
注解中指定该Bean的名称为redisHealthIndicator
。
版权声明
本文采用 CC BY 3.0 CN协议 进行许可。 可自由转载、引用,但需署名做者且注明文章出处。如转载至微信公众号,请在文末添加做者公众号二维码。
长按指纹
一键关注