Appearance
SpringBoot Actuator的关键特性是在应用程序里提供众多Web端点,通过它们了解应用程序运行时的内部状况。
深入探索Spring Boot Actuator:应用监控与管理的利器
Spring Boot Actuator 是Spring Boot生态中用于监控和管理应用程序的核心工具。它通过一系列内置的Web端点(Endpoints),让开发者能够实时洞察应用的运行状态、配置信息和性能指标。本文将从关键特性、端点使用、运行时监控到自定义配置,全面解析Actuator的核心功能。
一、Actuator的核心特性
Spring Boot Actuator 提供了约20个端点(Endpoint),涵盖应用配置、Bean管理、健康检查等场景。以下是常用端点的功能概览:
| 端点路径 | 请求方式 | 描述 |
|---|---|---|
/actuator/beans | GET | 查看应用上下文中的所有Bean及其依赖关系 |
/actuator/conditions | GET | 自动配置报告:显示生效和未生效的自动配置类 |
/actuator/env | GET | 获取全部环境属性(环境变量、JVM属性、配置文件属性等) |
/actuator/mappings | GET | 所有URI路径与控制器的映射关系 |
/actuator/metrics | GET | 应用程序的度量指标(如JVM内存、线程状态等) |
/actuator/loggers | GET | 查看日志级别配置 |
/actuator/health | GET | 应用健康状态(如数据库连接、磁盘空间等) |
/actuator/shutdown | POST | 关闭应用(需手动启用) |
提示:访问根端点
/actuator可查看所有已启用的端点列表。
二、关键端点详解
1. 查看配置明细
- /beans端点
展示Spring上下文中的所有Bean及其依赖关系,访问地址:http://localhost:8088/actuator/beans
适用于排查Bean注入冲突或依赖缺失问题。
{
"contexts": {
"application": {
"beans": {
"sqlSessionFactory": {
"aliases": [],
"scope": "singleton",
"type": "org.apache.ibatis.session.defaults.DefaultSqlSessionFactory",
"resource": "class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]",
"dependencies": [
"org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration",
"dataSource"
]
},
"jdbcTemplate": {
"aliases": [],
"scope": "singleton",
"type": "org.springframework.jdbc.core.JdbcTemplate",
"resource": "class path resource [org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.class]",
"dependencies": [
"dataSourceScriptDatabaseInitializer",
"org.springframework.boot.autoconfigure.jdbc.JdbcTemplateConfiguration",
"dataSource",
"spring.jdbc-org.springframework.boot.autoconfigure.jdbc.JdbcProperties"
]
}
}
}
}
}- /conditions端点
生成自动配置报告,区分positiveMatches(生效配置)和negativeMatches(未生效配置)。访问地址:http://localhost:8088/actuator/conditions
帮助开发者理解Spring Boot的自动配置逻辑。
{
"contexts": {
"application": {
"positiveMatches": {
"DruidDataSourceAutoConfigure": [{
"condition": "OnClassCondition",
"message": "@ConditionalOnClass found required class 'com.alibaba.druid.pool.DruidDataSource'"
}]
},
"negativeMatches": {
"RabbitAutoConfiguration": {
"notMatched": [{
"condition": "OnClassCondition",
"message": "@ConditionalOnClass did not find required class 'com.rabbitmq.client.Channel'"
}],
"matched": []
}
}
}
}
}- /env端点
显示所有环境属性,包括系统变量、配置文件属性等。支持按名称查询特定属性:http://localhost:8088/actuator/env/{name}
{
"activeProfiles": [],
"propertySources": [{
"name": "systemProperties",
"properties": {
"java.runtime.name": {
"value": "Java(TM) SE Runtime Environment"
},
"java.vm.name": {
"value": "Java HotSpot(TM) 64-Bit Server VM"
},
"java.runtime.version": {
"value": "1.8.0_91-b14"
}
}
},
{
"name": "Config resource 'class path resource [application.yml]' via location 'optional:classpath:/'",
"properties": {
"server.port": {
"value": 8088,
"origin": "class path resource [application.yml]:2:9"
},
"spring.datasource.url": {
"value": "jdbc:mysql://localhost:3306/mall_tiny?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
"origin": "class path resource [application.yml]:6:10"
},
"spring.datasource.username": {
"value": "root",
"origin": "class path resource [application.yml]:7:15"
},
"spring.datasource.password": {
"value": "******",
"origin": "class path resource [application.yml]:8:15"
}
}
}
]
}2. 运行时度量监控
- /metrics端点
默认返回所有可用指标名称(如jvm.memory.used、http.server.requests等),需指定名称查看详情:
# 查看JVM内存使用情况
http://localhost:8088/actuator/metrics/jvm.memory.used- /loggers端点
动态查看或修改日志级别。例如,以下配置表示:
{
"ROOT": {"configuredLevel": "INFO"},
"com.macro.mall.tiny": {"configuredLevel": "DEBUG"}
}访问地址:`http://localhost:8088/actuator/loggers`。
{
"levels": [
"OFF",
"ERROR",
"WARN",
"INFO",
"DEBUG",
"TRACE"
],
"loggers": {
"ROOT": {
"configuredLevel": "INFO",
"effectiveLevel": "INFO"
},
"com.macro.mall.tiny": {
"configuredLevel": "DEBUG",
"effectiveLevel": "DEBUG"
}
}
}3. 健康检查/关闭应用
- /health端点
返回应用健康状态(UP表示正常),支持集成自定义健康检查(如数据库连接):
{ "status": "UP" }- /shutdown端点
通过POST请求关闭应用(需配置启用):
management.endpoint.shutdown.enabled=true请求示例:
curl -X POST http://localhost:8088/actuator/shutdown三、自定义Actuator配置
1. 开启所有端点
默认仅开放health和info端点,可通过配置暴露全部端点:
management:
endpoints:
web:
exposure:
include: '*'2. 修改端点基础路径
自定义Actuator的访问路径(如改为/monitor):
management:
endpoints:
web:
base-path: /monitor此时端点地址变为:http://localhost:8088/monitor/health。
3. 安全建议
敏感端点保护:结合Spring Security限制端点的访问权限。
禁用危险端点:生产环境中建议关闭
/shutdown等高风险端点。
四、总结
Spring Boot Actuator 是提升应用可观测性的核心工具,通过其丰富的端点,开发者可以轻松实现:
实时监控:掌握JVM状态、请求映射、日志级别等运行时信息。
快速排障:分析Bean依赖、自动配置报告和环境变量。
灵活定制:按需启用或隐藏端点,适配不同环境需求。
合理使用Actuator,不仅能提升运维效率,还能为应用的稳定性保驾护航。赶紧尝试这些端点,让你的Spring Boot应用更加透明可控吧!
相关资源
完整代码示例:GitHub仓库链接(根据实际情况补充)