spring boot的健康检查HealthIndicators实战

 更新时间:2021年10月19日 12:13:09   作者:南北雪树  
这篇文章主要介绍了spring boot的健康检查HealthIndicators实战,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot 健康检查HealthIndicators

想提供自定义健康信息,你可以注册实现了HealthIndicator接口的Spring beans。

你需要提供一个health()方法的实现,并返回一个Health响应。

Health响应需要包含一个status和可选的用于展示的详情。

import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealth implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // perform some specific health check
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
} r
eturn Health.up().build();
}
}

除了Spring Boot预定义的Status类型,Health也可以返回一个代表新的系统状态的自定义Status。

在这种情况下,需要提供一个HealthAggregator接口的自定义实现,或使用management.health.status.order属性配置默认的实现。

例如,假设一个新的,代码为FATAL的Status被用于你的一个HealthIndicator实现中。 为了配置严重程度, 你需要将下面的配

置添加到application属性文件中:

management.health.status.order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP

如果使用HTTP访问health端点, 你可能想要注册自定义的status, 并使用HealthMvcEndpoint进行映射。 例如, 你可以将

FATAL映射为HttpStatus.SERVICE_UNAVAILABLE。

springboot health indicator原理及其使用

作用

sping boot health 可以通过暴露的接口来提供系统及其系统组件是否可用。默认通过/health来访问。返回结果如下:

{
  "status": "UP",
  "discoveryComposite": {
    "description": "Spring Cloud Eureka Discovery Client",
    "status": "UP",
    "discoveryClient": {
      "description": "Spring Cloud Eureka Discovery Client",
      "status": "UP",
      "services": [
        "..."
      ]
    },
    "eureka": {
      "description": "Remote status from Eureka server",
      "status": "UP",
      "applications": {
        "AOMS-MOBILE": 1,
        "DATA-EXCHANGE": 1,
        "CLOUD-GATEWAY": 2,
        "AOMS": 1,
        "AOMS-AIIS": 0,
        "AOMS-EUREKA": 2
      }
    }
  },
  "diskSpace": {
    "status": "UP",
    "total": 313759301632,
    "free": 291947081728,
    "threshold": 10485760
  },
  "refreshScope": {
    "status": "UP"
  },
  "hystrix": {
    "status": "UP"
  }
}

状态说明:

  • UNKNOWN:未知状态,映射HTTP状态码为503
  • UP:正常,映射HTTP状态码为200
  • DOWN:失败,映射HTTP状态码为503
  • OUT_OF_SERVICE:不能对外提供服务,但是服务正常。映射HTTP状态码为200

注意:UNKNOWN,DOWN,OUT_OF_SERVICE在为微服务环境下会导致注册中心中的实例也为down状态,请根据具体的业务来正确使用状态值。

自动配置的Health Indicator

自动配置的HealthIndicator主要有以下内容:

Key Name Description
cassandra CassandraDriverHealthIndicator Checks that a Cassandra database is up.
couchbase CouchbaseHealthIndicator Checks that a Couchbase cluster is up.
datasource DataSourceHealthIndicator Checks that a connection to DataSource can be obtained.
diskspace DiskSpaceHealthIndicator Checks for low disk space.
elasticsearch ElasticsearchRestHealthIndicator Checks that an Elasticsearch cluster is up.
hazelcast HazelcastHealthIndicator Checks that a Hazelcast server is up.
influxdb InfluxDbHealthIndicator Checks that an InfluxDB server is up.
jms JmsHealthIndicator Checks that a JMS broker is up.
ldap LdapHealthIndicator Checks that an LDAP server is up.
mail MailHealthIndicator Checks that a mail server is up.
mongo MongoHealthIndicator Checks that a Mongo database is up.
neo4j Neo4jHealthIndicator Checks that a Neo4j database is up.
ping PingHealthIndicator Always responds with UP.
rabbit RabbitHealthIndicator Checks that a Rabbit server is up.
redis RedisHealthIndicator Checks that a Redis server is up.
solr SolrHealthIndicator Checks that a Solr server is up.

分组

可以通过一个别名来启用一组指标的访问。配置的格式如下:

management.endpoint.health.group.<name>
//demo:
management.endpoint.health.group.mysys.include=db,redis,mail
management.endpoint.health.group.custom.exclude=rabbit

如何管理Health Indicator

开启

可以通过management.health.key.enabled来启用key对应的indicator。例如:

management.health.db.enabled=true

关闭

management.health.db.enabled=false

RedisHealthIndicator源码解析

下面,通过RedisHealthIndicator源码来为什么可以这么写。

代码结构

自动配置的health indicator有HealthIndicator和HealthIndicatorAutoConfiguration两部分组成。

HealthIndicator所在包在org.springframework.boot.actuate,

HealthIndicatorAutoConfiguration所在包在org.springframework.boot.actuate.autoconfigure下

//RedisHealthIndicator.java
public class RedisHealthIndicator extends AbstractHealthIndicator {
	static final String VERSION = "version";
	static final String REDIS_VERSION = "redis_version";
	private final RedisConnectionFactory redisConnectionFactory;
	public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
		super("Redis health check failed");
		Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
		this.redisConnectionFactory = connectionFactory;
	}
	@Override
	protected void doHealthCheck(Health.Builder builder) throws Exception {
		RedisConnection connection = RedisConnectionUtils
				.getConnection(this.redisConnectionFactory);
		try {
			if (connection instanceof RedisClusterConnection) {
				ClusterInfo clusterInfo = ((RedisClusterConnection) connection)
						.clusterGetClusterInfo();
				builder.up().withDetail("cluster_size", clusterInfo.getClusterSize())
						.withDetail("slots_up", clusterInfo.getSlotsOk())
						.withDetail("slots_fail", clusterInfo.getSlotsFail());
			}
			else {
				Properties info = connection.info();
				builder.up().withDetail(VERSION, info.getProperty(REDIS_VERSION));
			}
		}
		finally {
			RedisConnectionUtils.releaseConnection(connection,
					this.redisConnectionFactory);
		}
	}
}

主要实现doHealthCheck方法来实现具体的判断逻辑。注意,操作完成后在finally中释放资源。

在父类AbstractHealthIndicator中,对doHealthCheck进行了try catch,如果出现异常,则返回Down状态。

 //AbstractHealthIndicator.java
 @Override
 public final Health health() {
  Health.Builder builder = new Health.Builder();
  try {
   doHealthCheck(builder);
  }
  catch (Exception ex) {
   if (this.logger.isWarnEnabled()) {
    String message = this.healthCheckFailedMessage.apply(ex);
    this.logger.warn(StringUtils.hasText(message) ? message : DEFAULT_MESSAGE,
      ex);
   }
   builder.down(ex);
  }
  return builder.build();
 }

RedisHealthIndicatorAutoConfiguration完成RedisHealthIndicator的自动配置

@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);
 }
}

重点说明ConditionalOnEnabledHealthIndicator:如果management.health..enabled为true,则生效。

CompositeHealthIndicatorConfiguration 中会通过HealthIndicatorRegistry注册创建的HealthIndicator

自定义Indicator

@Component
@ConditionalOnProperty(name="spring.dfs.http.send-url")
@Slf4j
public class DfsHealthIndicator implements HealthIndicator {
    @Value("${spring.dfs.http.send-url}")
    private String dsfSendUrl;
    @Override
    public Health health() {
        log.debug("正在检查dfs配置项...");
        log.debug("dfs 请求地址:{}",dsfSendUrl);
        Health.Builder up = Health.up().withDetail("url", dsfSendUrl);
        try {
            HttpUtils.telnet(StringUtils.getIpFromUrl(dsfSendUrl),StringUtils.getPortFromUrl(dsfSendUrl));
            return up.build();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("DFS配置项错误或网络超时");
            return up.withException(e).build();
        }
    }
}

返回值:

{
    "dfs": {
    "status": "UP",
    "url": "10.254.131.197:8088",
    "error": "java.net.ConnectException: Connection refused (Connection refused)"
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 解析Tars-Java客户端源码

    解析Tars-Java客户端源码

    Tars是基于名字服务使用Tars协议的高性能RPC开发框架,同时配套一体化的服务治理平台,帮助个人或者企业快速的以微服务的方式构建自己稳定可靠的分布式应用
    2021-06-06
  • SpringBoot对Druid配置SQL监控功能失效问题及解决方法

    SpringBoot对Druid配置SQL监控功能失效问题及解决方法

    这篇文章主要介绍了SpringBoot对Druid配置SQL监控功能失效问题的解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05
  • Java之maven打完jar包之后将jar包放到指定位置汇总

    Java之maven打完jar包之后将jar包放到指定位置汇总

    这篇文章主要介绍了Java之maven打完jar包之后将jar包放到指定位置汇总,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • java 键盘输入的多种实现方法

    java 键盘输入的多种实现方法

    java不像C中拥有scanf这样功能强大的函数,大多是通过定义输入输出流对象。常用的类有BufferedReader,Scanner。
    2013-03-03
  • java设计模式--桥接模式详解

    java设计模式--桥接模式详解

    这篇文章主要为大家详细介绍了java设计模式之桥接模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能给你带来帮助
    2021-07-07
  • Java基础夯实之线程问题全面解析

    Java基础夯实之线程问题全面解析

    操作系统支持多个应用程序并发执行,每个应用程序至少对应一个进程 。进程是资源分配的最小单位,而线程是CPU调度的最小单位。本文将带大家全面解析线程相关问题,感兴趣的可以了解一下
    2022-11-11
  • 关于JSON.toJSONString()和Gson.toJson()方法的比较

    关于JSON.toJSONString()和Gson.toJson()方法的比较

    本文介绍了两种将Java对象转换为JSON字符串的方法:阿里的`JSON.toJSONString()`和谷歌的`Gson.toJson()`,通过一个示例,展示了当使用继承关系且子类覆盖父类字段时,`Gson`会报错,而`JSON`可以正常运行,作者建议在处理JSON相关操作时使用阿里的`JSON`类
    2024-11-11
  • 一小时迅速入门Mybatis之增删查改篇

    一小时迅速入门Mybatis之增删查改篇

    这篇文章主要介绍了迅速入门Mybatis之增删查改篇,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • SpringBoot集成JPA持久层框架,简化数据库操作

    SpringBoot集成JPA持久层框架,简化数据库操作

    JPA(Java Persistence API)意即Java持久化API,是Sun官方在JDK5.0后提出的Java持久化规范。主要是为了简化持久层开发以及整合ORM技术,结束Hibernate、TopLink、JDO等ORM框架各自为营的局面。JPA是在吸收现有ORM框架的基础上发展而来,易于使用,伸缩性强。
    2021-06-06
  • Java微服务开发之Swagger详解

    Java微服务开发之Swagger详解

    Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步
    2021-10-10

最新评论