文章目录
一、Health Indicator 概述
Health Indicator 是 Spring Boot Actuator 模块的核心组件,用于监控应用的健康状态。它通过 /actuator/health
端点暴露系统关键组件的运行状态(如数据库连接、HTTP 服务、磁盘空间等),帮助开发者快速定位故障。
二、默认 Health Indicator 类型
Spring Boot 内置了多种健康指示器,无需额外配置即可自动生效:
默认指示器 | 监控目标 | 健康状态检查条件 |
---|---|---|
DiskSpaceHealthIndicator | 磁盘空间 | 剩余空间是否低于阈值(默认 10%) |
DbHealthIndicator | 数据库连接 | 是否能成功获取数据库连接 |
HttpHealthIndicator | HTTP 服务端口 | 是否能通过发送 HTTP 请求验证端口可达性 |
JmsHealthIndicator | JMS 队列(如 ActiveMQ) | 是否能连接到消息队列服务 |
RedisHealthIndicator | Redis 数据库 | 是否能执行 Redis 命令 |
三、自定义 Health Indicator
1. 实现步骤
(1) 添加 Actuator 依赖
在 pom.xml
或 build.gradle
中引入 Spring Boot Actuator:
<!-- Maven -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(2) 创建自定义 Health Indicator 类
实现 HealthIndicator
接口,并编写业务逻辑:
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class CustomServiceHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// 模拟服务健康检查:假设检查某个 API 是否可用
boolean isServiceUp = checkExternalApi();
if (isServiceUp) {
return Health.up().withDetail("ServiceStatus", "UP").build();
} else {
return Health.down()
.withDetail("Error", "External API is unreachable")
.withStatus("ERROR")
.build();
}
}
private boolean checkExternalApi() {
// 实际业务逻辑:调用外部 API 或检查服务状态
try {
// 示例:模拟 HTTP 请求
new java.net.URL("https://api.example.com/health").openConnection();
return true;
} catch (Exception e) {
return false;
}
}
}
(3) 配置 Actuator 端点
在 application.yml
中启用 /health
端点:
management:
endpoints:
web:
exposure:
include: "health"
(4) 访问健康状态
启动应用后,访问 http://localhost:8080/actuator/health
,自定义指示器的状态将显示在 components
下:
{
"status": "UP",
"components": {
"diskSpace": { /* 默认磁盘状态 */ },
"db": { /* 默认数据库状态 */ },
"http": { /* 默认 HTTP 状态 */ },
"customService": { # 自定义指示器
"status": "UP",
"details": {
"ServiceStatus": "UP",
"Error": null
}
}
}
}
四、高级自定义场景
1. 组合多个健康检查
通过实现 CompositeHealthIndicator
将多个指示器合并:
@Component
public class CompositeHealthIndicator implements HealthIndicator {
private final List<HealthIndicator> indicators;
public CompositeHealthIndicator(List<HealthIndicator> indicators) {
this.indicators = indicators;
}
@Override
public Health health() {
Health aggregatedHealth = Health.up().build();
for (HealthIndicator indicator : indicators) {
Health componentHealth = indicator.health();
if (componentHealth.getStatus() == DOWN) {
aggregatedHealth = Health.down()
.withDetail("Component", indicator.getClass().getSimpleName())
.build();
}
}
return aggregatedHealth;
}
}
2. 动态健康检查(定时任务)
使用 @Scheduled
定时更新健康状态:
@Component
public class DynamicHealthIndicator implements HealthIndicator {
private volatile boolean isServiceUp = true;
@Scheduled(fixedRate = 60000) // 每分钟检查一次
public void checkService() {
isServiceUp = checkExternalApi(); // 更新状态
}
@Override
public Health health() {
return isServiceUp ? Health.up().build() : Health.down().build();
}
}
3. 自定义健康状态码
通过 Health
构建器指定 HTTP 状态码:
return Health.status(HttpStatus.TOO_MANY_REQUESTS) // 自定义状态码 429
.withDetail("Message", "Rate limit exceeded")
.build();
五、典型应用场景
- 微服务监控:检查下游服务(如 MySQL、Redis、Kafka)是否可用。
- API 可用性:验证第三方 API 或内部 RESTful 接口的响应状态。
- 资源预警:监控磁盘使用率、内存占用量超过阈值时触发告警。
- 业务逻辑健康:检查关键业务线程是否存活(如定时任务、消息队列消费)。
六、总结
• 默认指示器:快速监控基础资源状态。
• 自定义指示器:扩展监控范围至业务逻辑或第三方服务。
• 组合与定时检查:实现复杂健康策略和高频监控。
• 集成监控系统:将 Actuator 数据接入 Prometheus、Grafana 或 ELK 日志系统。
通过灵活配置和扩展,Spring Boot 的 Health Indicator 能满足从简单运维到复杂分布式系统的健康监控需求。