SpringBoot的actuator组件快速使用
Spring Boot Actuator 组件的主要作用是为 Spring Boot 应用提供生产级监控和管理功能。它让应用能够自我监控和管理,是微服务架构中不可或缺的组件。
1.引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
2.application.xml文件配置
management:endpoint:web:exposure:include: "*"health:show-details: alwayshealth:defaults:enabled: false
上面配置会暴露所有端点,并总是显示健康检查详情,禁用默认的健康检查器(其它端点访问结果为空)。
3.主要端点以及作用
端点 | 路径 | 作用 |
---|---|---|
health | /actuator/health | 应用健康状态 |
info | /actuator/info | 应用基本信息 |
metrics | /actuator/metrics | 应用指标数据 |
env | /actuator/env | 环境配置信息 |
configprops | /actuator/configprops | 配置属性 |
beans | /actuator/beans | Spring Beans 信息 |
mappings | /actuator/mappings | URL 映射信息 |
loggers | /actuator/loggers | 日志配置管理 |
threaddump | /actuator/threaddump | 线程转储 |
heapdump | /actuator/heapdump | 堆内存转储 |
4.访问端点/actuator/health
启动服务之后。
{"status": "UP","details": {"application": {"status": "UP"}}
}
当启用默认健康检查器时
management:endpoint:web:exposure:include: "*"health:show-details: alwayshealth:defaults:enabled: true
显示信息如下:
{"status": "DOWN","details": {"diskSpace": {"status": "UP","details": {"total": 350731890688,"free": 312695111680,"threshold": 10485760}},"db": {"status": "UP","details": {"database": "MySQL","hello": 1}},"refreshScope": {"status": "UP"},"redis": {"status": "DOWN","details": {"error": "org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379"}}}
}
5./actuator(显示所有端点的页面)
{"_links": {"self": {"href": "http://localhost:8080/lams-portal/actuator","templated": false},"health": {"href": "http://localhost:8080/lams-portal/actuator/health","templated": false},"health-component-instance": {"href": "http://localhost:8080/lams-portal/actuator/health/{component}/{instance}","templated": true},"health-component": {"href": "http://localhost:8080/lams-portal/actuator/health/{component}","templated": true},"info": {"href": "http://localhost:8080/lams-portal/actuator/info","templated": false}}
}
6.访问其它端点显示为空(没有配置)