使用Spring Boot Actuator查看应用健康情况

Spring Boot Actuator 提供了强大的监控和管理功能,其中健康检查(Health)是最常用的功能之一。通过相关配置,可以全面监控 Spring Boot 应用的健康状况,并根据需要进行自定义扩展。

1. 添加依赖

首先在 pom.xml 中添加 Actuator 依赖:

XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

或者在 build.gradle 中:

bash 复制代码
implementation 'org.springframework.boot:spring-boot-starter-actuator'

2. 基本配置

application.ymlapplication.properties 中进行基本配置:

application.yml

bash 复制代码
management:
  endpoints:
    web:
      exposure:
        include: health,info,metrics  # 暴露需要的端点
  endpoint:
    health:
      show-details: always  # 显示详细健康信息

application.properties

bash 复制代码
management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=always

3. 访问健康端点

启动应用后,可以通过以下 URL 访问健康信息:

  • 基础健康信息 : http://localhost:8080/actuator/health
  • 详细健康信息 : http://localhost:8080/actuator/health(当配置了 show-details: always 时)

响应示例

简单响应

bash 复制代码
{
  "status": "UP"
}

详细响应

bash 复制代码
{
  "status": "UP",
  "components": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 500068036608,
        "free": 239096578048,
        "threshold": 10485760,
        "exists": true
      }
    },
    "ping": {
      "status": "UP"
    },
    "db": {
      "status": "UP",
      "details": {
        "database": "MySQL",
        "validationQuery": "isValid()"
      }
    }
  }
}

健康状态级别

Spring Boot 定义了以下健康状态:

  • UP: 组件正常运行
  • DOWN: 组件不可用
  • OUT_OF_SERVICE: 组件暂时不可用
  • UNKNOWN: 状态未知

整体应用状态由最差的组件状态决定。

常用健康检查端点

端点 说明
/actuator/health 应用健康状况
/actuator/info 应用基本信息
/actuator/metrics 应用指标
/actuator/env 环境变量
/actuator/loggers 日志配置
/actuator/beans 查看应用的beans

4. 自定义健康指示器

创建自定义的健康检查逻辑:

java 复制代码
@Component
public class CustomHealthIndicator implements HealthIndicator {
    
    @Override
    public Health health() {
        // 执行自定义健康检查逻辑
        boolean isHealthy = checkCustomService();
        
        if (isHealthy) {
            return Health.up()
                .withDetail("customService", "Available")
                .withDetail("version", "1.0.0")
                .build();
        } else {
            return Health.down()
                .withDetail("customService", "Unavailable")
                .withDetail("error", "Connection timeout")
                .build();
        }
    }
    
    private boolean checkCustomService() {
        // 实现具体的健康检查逻辑
        return true;
    }
}

5. 常见内置健康指示器

  • DiskSpaceHealthIndicator: 检查磁盘空间
  • DataSourceHealthIndicator: 检查数据库连接
  • RedisHealthIndicator: 检查 Redis 连接
  • RabbitHealthIndicator: 检查 RabbitMQ 连接
  • MongoHealthIndicator: 检查 MongoDB 连接

6. 高级配置选项

配置健康端点路径

bash 复制代码
# 修改基础路径
management.endpoints.web.base-path=/monitor  

# 自定义健康端点路径
management.endpoint.health.path=/health-check

权限控制

bash 复制代码
# 仅在认证后显示详细信息
management.endpoint.health.show-details=when_authorized

分组健康检查

bash 复制代码
management.endpoint.health.group.custom.include=db,diskSpace
management.endpoint.health.group.custom.show-details=always

访问分组健康检查:http://localhost:8080/actuator/health/custom

7. 生产环境最佳实践

bash 复制代码
# 生产环境配置示例

# 仅暴露必要的端点
management.endpoints.web.exposure.include=health,info

# 仅授权用户可见详细信息
management.endpoint.health.show-details=when_authorized

# 启用 Kubernetes 探针支持
management.endpoint.health.probes.enabled=true

# 禁用环境信息暴露
management.info.env.enabled=false

8. Kubernetes 探针集成

Actuator 还提供了专门的探针端点:

  • Liveness Probe : http://localhost:8080/actuator/health/liveness
  • Readiness Probe : http://localhost:8080/actuator/health/readiness

配置示例:

bash 复制代码
# 启用 Kubernetes 探针支持
management.endpoint.health.probes.enabled=true

# 仅授权用户可见详细信息
management.endpoint.health.show-details=when_authorized
相关推荐
萧瑟余晖5 小时前
JDK 26 新特性详解
java·开发语言
马优晨5 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
维天说8 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
Zane19949 小时前
并发 vs 并行:别再傻傻分不清了,一文讲透 Java 并发编程的第一课
java·后端
噢,我明白了9 小时前
java中Excel的导入和导出(EasyExcel)
java·开发语言·excel
吠品9 小时前
Zabbix Web界面误报Server未运行的排查与解决
java·服务器·数据库
啊湘9 小时前
天气查询API接口 按月Token鉴权 实时天气 物联网可用 文档齐全
java·后端·struts
Java内核笔记10 小时前
告别十亿美元的错误 : Spring Boot 4 空安全 (JSpecify) 实战
java·spring boot·后端
糖果店的幽灵10 小时前
langgraph的 MessagesState 解读
java·开发语言·人工智能·windows·langgraph
极光代码工作室11 小时前
基于SpringBoot的在线博客系统
java·springboot·web开发·后端开发