因为项目里面用到了redis集群,但并不是用spring boot的配置方式,启动后项目健康检查老是检查redis的时候状态为down,导致注册到eureka后项目状态也是down。
问下能不能设置spring boot不检查 redis的健康状态

"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"
}

而且,应用的控制台上也会输出如下:

Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:888)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:432)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 95 common frames omitted

原因分析

如提问者所述,由于在Spring Boot项目中引用了Redis模块,所以Spring Boot Actuator会对其进行健康检查,正常情况下不会出现问题,但是由于采用了其他配置方式,导致redis的连接检查没有通过。这样就会导致了Consul或Eureka的HealthCheck认为该服务是DOWN状态。

那么redis的健康检查是如何实现的呢?我们不妨来看看健康检查的自动化配置中针对redis的配置源码:

@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类。从自动化配置中我们可以看到,会自动加载一个针对redis的HealthIndicator实现,并且该Bean命名为redisHealthIndicator

解决方法

通过上面的分析,我们已经知道了是哪个Bean导致了服务实例的健康检查不通过,那么如何解决该问题的方法也就马上能想到了:我们只需要再实现一个redis的HealthIndicator实现来替代原先默认的检查逻辑。比如:

@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

Springboot监控之一:SpringBoot四大神器之Actuator之2--spring boot健康检查对Redis的连接检查的调整的更多相关文章

  1. Springboot监控之一:SpringBoot四大神器之Actuator之3-springBoot的监控和管理--指标说明

    Spring Boot包含很多其他的特性,它们可以帮你监控和管理发布到生产环境的应用.你可以选择使用HTTP端点,JMX或远程shell(SSH或Telnet)来管理和监控应用.审计(Auditing ...

  2. Springboot监控之一:SpringBoot四大神器之Actuator

    介绍 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供 ...

  3. Springboot监控之一:SpringBoot四大神器之Actuator之2--springboot健康检查

    Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring Boot 内置了一些 HealthIndicator. ...

  4. Spring Boot]SpringBoot四大神器之Actuator

    论文转载自博客: https://blog.csdn.net/Dreamhai/article/details/81077903 https://bigjar.github.io/2018/08/19 ...

  5. SpringBoot四大神器之Actuator

    介绍 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供 ...

  6. Springboot监控之一:SpringBoot四大神器之Actuator之2--覆盖修改spring cloud的默认的consul健康检查规则

    微服务网关是socket长连接与支付公司对接,该网关需要提供http接口给内部系统调用,当socket没有建立连接时(网关服务的高可用是haProxy搭建的,有些服务的socket可能未连上支付公司) ...

  7. SpringBoot四大神器之Starter

    SpringBoot的starter主要用来简化依赖用的.本文主要分两部分,一部分是列出一些starter的依赖,另一部分是教你自己写一个starter. 部分starters的依赖 Starter( ...

  8. SpringBoot四大神器之auto-configuration

    SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...

  9. Spring Boot (四): Druid 连接池密码加密与监控

    在上一篇文章<Spring Boot (三): ORM 框架 JPA 与连接池 Hikari> 我们介绍了 JPA 与连接池 Hikari 的整合使用,在国内使用比较多的连接池还有一个是阿 ...

随机推荐

  1. BZOJ1505: [NOI2004]小H的小屋

    BZOJ1505: [NOI2004]小H的小屋 Description 小H发誓要做21世纪最伟大的数学家.他认为,做数学家与做歌星一样,第一步要作好包装,不然本事再大也推不出去. 为此他决定先在自 ...

  2. 我的Android进阶之旅------>Android关于Log的一个简单封装

    android.util.Log类,可以方便地用于在编码调试过程中打印日志.但是在发布后的产品中,如果有太多的日志打印,则会严重地影响性能.对android.util.Log类做一个简单的封装,当产品 ...

  3. MySQL删除相同前缀的表,修改某个库的存储引擎

    MySQL5.0 之后,提供了一个新的数据库information_schema,用来记录MySQL总的元数据信息.元数据指的是 数据的数据. 比如表名.列名.列类型.索引名等表的各种属性名称.这个库 ...

  4. wap网站即手机端网页SEO优化注意事项及方法

    定位和页面设计: 无论是PC端还是移动端,网站 都要考虑清楚消费群体的定位问题.虽然智能手机用户数量非常普及,但是要明白中国的大部分手机用户使用的还是2G网络,一直高 喊的3G.4G手机用户只有大约1 ...

  5. easy_install和pip的安装及使用

    在终端输入命令报错后,在网上找到了这篇博客,用easy_install命令安装pip,问题解决 Fatal error in launcher: Unable to create process us ...

  6. UI组件之Label

    Use Core Data 接口,链接数据库 Portrait 肖像模式 LandScape(Left, Right) 风景模式 1.程序启动后,从main接口进入, main函数会调用UIAppli ...

  7. 326 集合 ,数据类型的补充 ,copy , 编码之间的转换

    一.数据类型补充1,对于元组:如果只有一个元素,并且没有逗号,此元素是什么数据类型,改表达式就是什么数据类型. tu = () tu1 = (,) print(tu,type(tu)) #1 < ...

  8. ansible普通用户su切换问题

    在现网应用中,安全加固后的主机是不允许直接以root用户登陆的,而很多命令又需要root用户来执行,在不改造现网的情况下.希望通过一个普通用户先登陆,再su切到root执行.而且每台主机的普通用户和r ...

  9. MS-SQL charindex的用法

    select * from table_a where charindex('a',id)>0 or charindex('b',id)>0 table_a 表中 id字段中含有" ...

  10. java中的向上转型

    Person 可以表示为一个抽象的东西 就是人.比如说人可以唱歌, 就好比Person类中有一个sing方法.那么这个抽象的类(Person 人)可以具体到两类或者更多类 比如 男人,女人 .Man ...