springboot监控 之 Actuator自定义端点
基本介绍
Spring Boot Actuator 是 Spring Boot 框架中的一个核心模块,专门用于监控和管理生产环境中的应用程序,它通过提供一系列内置端点(endpoints)和指标(metrics),帮助开发者实时了解应用的健康状态、性能表现及运行环境信息,从而提升应用的可观测性和可管理性。
核心功能
-
健康检查(Health Endpoints)
/actuator/health:提供应用的健康状态信息(如 UP/DOWN),支持自定义健康指标(如数据库连接、缓存状态等)。- 适用场景:用于负载均衡器的健康检查或自动化运维工具的监控。
-
性能指标(Metrics Endpoints)
/actuator/metrics:暴露关键性能指标,如 JVM 内存使用、线程数、HTTP 请求统计等。- 扩展性:可集成 Prometheus、Grafana 等工具,实现可视化监控。
-
环境信息(Environment Endpoints)
/actuator/env:显示应用的运行时环境变量、配置属性(如application.properties中的配置)。- 安全风险:需谨慎暴露,避免泄露敏感信息(如 API 密钥、密码)。
-
应用信息(Info Endpoints)
/actuator/info:展示应用的基本信息(如版本号、描述),可通过配置文件自定义内容。
-
动态管理(Management Endpoints)
/actuator/shutdown:允许远程关闭应用(需显式启用并严格权限控制)。- 其他操作:如刷新配置(
/actuator/refresh,需结合 Spring Cloud Config 使用)。
技术实现
- 访问方式:
- HTTP 端点:通过 RESTful API 访问(如
http://localhost:8080/actuator/health)。 - JMX(Java Management Extensions):通过 JMX 工具(如 JConsole)管理应用。
- HTTP 端点:通过 RESTful API 访问(如
- 依赖配置:
- Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> - Gradle:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
- Maven:
- 端点暴露配置:
在application.properties或application.yml中控制端点的可见性:# 暴露所有端点(谨慎使用) management.endpoints.web.exposure.include=* # 仅暴露健康和信息端点 management.endpoints.web.exposure.include=health,info
安全建议
- 限制端点访问:
- 使用 Spring Security 保护敏感端点(如
/actuator/env、/actuator/shutdown)。 - 示例配置:
@Configuration public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/actuator/health").permitAll() .antMatchers("/actuator/**").hasRole("ADMIN") .and().httpBasic(); } }
- 使用 Spring Security 保护敏感端点(如
- 避免暴露敏感信息:
- 不要默认暴露
/actuator/env或/actuator/mappings等可能泄露内部信息的端点。
- 不要默认暴露
- 监控端点性能:
- 定期检查 Actuator 端点的响应时间,避免因监控功能影响主应用性能。
应用场景
- 生产环境监控:通过 Prometheus + Grafana 搭建可视化监控面板。
- 自动化运维:结合 Kubernetes 的健康检查机制,实现容器化应用的自愈能力。
- 故障排查:快速定位内存泄漏、线程阻塞等问题(通过
/actuator/metrics/jvm.memory.used等指标)。 - 动态配置管理:在微服务架构中,通过
/actuator/refresh动态更新配置(需 Spring Cloud Config 支持)。
总结
Spring Boot Actuator 是提升应用可观测性的关键工具,其丰富的端点和灵活的配置方式使其成为生产环境运维的得力助手。然而,开发者需严格遵循安全最佳实践,避免因配置不当导致数据泄露或系统风险。通过合理使用 Actuator,可以显著降低运维复杂度,提升应用的稳定性和可靠性。
自定义端点
在Spring Boot中,自定义Actuator功能主要通过创建自定义端点实现,同时可通过配置文件调整端点访问路径和端口,以下是具体实现方式及示例:
一、自定义Actuator功能
自定义Actuator端点需通过@Endpoint注解创建,并配合@ReadOperation、@WriteOperation或@DeleteOperation注解定义HTTP方法(GET/POST/DELETE)。以下是完整步骤和示例:
-
添加依赖
确保项目中已引入spring-boot-starter-actuator依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> -
创建自定义端点类
使用@Endpoint注解标记类,并通过@Component将其注册为Spring Bean。示例如下:import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.stereotype.Component; import java.util.Collections; import java.util.Map; @Endpoint(id = "custom") // 定义端点ID为"custom" @Component public class CustomEndpoint { @ReadOperation // 对应HTTP GET请求 public Map<String, String> customInfo() { return Collections.singletonMap("message", "Hello from Custom Actuator Endpoint!"); } }- 功能说明:访问该端点将返回JSON格式的自定义信息(如
{"message":"Hello from Custom Actuator Endpoint!"})。 - 扩展操作:
- 使用
@WriteOperation定义POST请求(如修改配置)。 - 使用
@DeleteOperation定义DELETE请求(如删除缓存)。
- 使用
- 功能说明:访问该端点将返回JSON格式的自定义信息(如
-
访问自定义端点
启动应用后,通过HTTP访问端点:GET http://localhost:8080/actuator/custom响应示例:
{ "message": "Hello from Custom Actuator Endpoint!" }
二、自定义Actuator地址
通过配置文件调整端点路径和端口,增强安全性并避免冲突:
-
修改端点基础路径
在application.yml中配置management.endpoints.web.base-path,将所有Actuator端点路径前缀改为自定义值(如/my-actuator):management: endpoints: web: base-path: /my-actuator- 效果:自定义端点访问路径变为
http://localhost:8080/my-actuator/custom。
- 效果:自定义端点访问路径变为
-
独立管理端口
为Actuator分配独立端口(如8888),与主应用端口分离:management: server: port: 8888- 效果:访问自定义端点需通过新端口:
GET http://localhost:8888/my-actuator/custom
- 效果:访问自定义端点需通过新端口:
-
暴露特定端点
仅暴露需要的端点(如health、info和自定义的custom):management: endpoints: web: exposure: include: health,info,custom
三、安全配置建议
-
结合Spring Security保护端点
通过Spring Security限制访问权限,例如仅允许管理员角色访问:import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/actuator/health").permitAll() // 健康检查公开 .antMatchers("/actuator/**").hasRole("ADMIN") // 其他端点需管理员权限 .and().httpBasic(); // 启用基本认证 } } -
禁用敏感端点
避免暴露env、mappings等可能泄露内部信息的端点:management: endpoints: web: exposure: exclude: env,mappings
四、应用场景示例
-
监控自定义业务指标
在自定义端点中返回业务数据(如订单处理延迟、QPS):@Endpoint(id = "business-metrics") @Component public class BusinessMetricsEndpoint { @ReadOperation public Map<String, Object> metrics() { return Map.of( "orderDelayMs", 8, "qps", 120000 ); } }- 访问:
GET http://localhost:8888/my-actuator/business-metrics - 响应:
{"orderDelayMs":8,"qps":120000}
- 访问:
-
动态配置管理
通过POST请求修改应用配置(需结合@WriteOperation):@Endpoint(id = "config") @Component public class ConfigEndpoint { private String featureFlag = "disabled"; @WriteOperation public String updateFeatureFlag(String flag) { this.featureFlag = flag; return "Feature flag updated to: " + flag; } @ReadOperation public String getFeatureFlag() { return featureFlag; } }- 修改配置:
POST http://localhost:8888/my-actuator/config Body: {"flag":"enabled"} - 查询配置:
GET http://localhost:8888/my-actuator/config
- 修改配置:
五、总结
- 自定义功能:通过
@Endpoint注解创建端点,支持GET/POST/DELETE操作,可返回任意业务数据。 - 地址配置:通过
management.endpoints.web.base-path修改路径前缀,通过management.server.port分配独立端口。 - 安全实践:结合Spring Security限制访问权限,避免暴露敏感端点。
- 典型场景:监控业务指标、动态配置管理、健康检查扩展等。
通过以上方法,可灵活扩展Actuator功能并定制访问路径,满足生产环境监控和管理需求。
本文来自博客园,作者:蓝迷梦,转载请注明原文链接:https://www.cnblogs.com/hewei-blogs/articles/19232122

浙公网安备 33010602011771号