【SpringBoot】定制⾃⼰的 Health Indicator

本文介绍了Spring Boot的Health Indicator功能,用于检查应用运行状态。Health Indicator有四种状态:DOWN、OUT_OF_SERVICE、UP、UNKNOWN,通过HealthIndicatorRegistry收集信息。用户可以自定义Health Indicator,实现HealthIndicator接口,返回自定义的Health状态。此外,文章还探讨了Health Information的使用,包括自动配置的HealthIndicators和如何展示详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot ⾃带的 Health Indicator

⽬的
  • 检查应⽤程序的运⾏状态
状态
  • DOWN - 503
  • OUT_OF_SERVICE - 503
  • UP - 200
  • UNKNOWN - 200
机制
  • 通过 HealthIndicatorRegistry 收集信息
  • HealthIndicator 实现具体检查逻辑
配置项
  • management.health.defaults.enabled=true|false
  • management.health…enabled=true
  • management.endpoint.health.show-details=never | whenauthorized | alway

在这里插入图片描述

⾃定义 Health Indicator

⽅法

  • 实现 HealthIndicator 接⼝
  • 根据⾃定义检查逻辑返回对应 Health 状态
    • Health 中包含状态和详细描述信息

Health Information

官网地址:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html#actuator.endpoints.health

可以使用运行状况信息(health information)检查正在运行的应用程序的状态。它经常被监控软件用于在生产系统崩溃时发出警报。health endpoint公开的信息取决于management.endpoint.health.show-detailsmanagement.endpoint.health.show-components属性,可配置为以下值之一:

Name Description
never 从不显示详细信息。
when-authorized 详细信息只显示给授权用户。可使用management.endpoint.health.roles配置授权角色。
always 详细信息将显示给所有用户。

缺省值为never。当用户处于一个或多个endpoints的角色中时,就认为用户已获得授权。如果端点没有配置角色(默认),则所有经过身份验证的用户都被认为是经过授权的。可以使用management.endpoint.health.roles属性配置角色。

如果已保护了应用程序并希望使用always,则安全配置必须允许已验证和未验证的用户访问运行状况端点。

运行状况信息从HealthContributorRegistry(默认情况下,ApplicationContext中定义的所有HealthContributor实例)的内容中收集。Spring Boot包括许多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值