其他
为 Spring Boot 服务配置生产级监控…
在 Spring Boot 应用中启用并加固 Actuator 端点、健康探针与 Micrometer 指标导出,实现生产可观测性。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 47/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 85/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 51/100 ×0.15
适用场景
为 Spring Boot 服务配置生产通用
解决思路
- 在构建配置中加入 spring-boot-starter-actuator 依赖(Maven 或 Gradle),重启服务后确认 /actuator/health 和 /actuator/info 返回 200 OK。
- 通过 management.endpoints.web.exposure.include 精确列出需要暴露的端点(内部部署可用 "*");当默认 /actuator 与路由冲突时,用 management.endpoints.web.base-path 调整基础路径(如 /management);用 curl http://localhost:8080/actuator 验证暴露的端点列表。
- 用独立的 SecurityFilterChain 配合 EndpointRequest.toAnyEndpoint() 和基于角色的规则保护管理流量;结合 management.server.port 与防火墙或服务网格策略限制为运维人员访问;仅在必要时公开 /actuator/health/**,否则强制认证;验证未认证请求受保护端点返回 401 Unauthorized。
- 设置 management.endpoint.health.probes.enabled=true 启用 /health/liveness 和 /health/readiness;通过 management.endpoint.health.group.* 按平台要求分组指标;通过继承 HealthIndicator 或 ReactiveHealthContributor 实现自定义健康指标;验证 /actuator/health/readiness 在上线前对所有必需组件返回 UP。
- 通过 management.metrics.export.* 启用 Micrometer 导出器(Prometheus、OTLP、Wavefront、StatsD);用 MeterRegistryCustomizer bean 添加 application、environment 和业务标签以便关联观测;在 Spring Boot 3.2+ 中通过 server.observation.* 配置暴露 HTTP 请求指标;抓取 /actuator/prometheus 确认存在 http.server.requests、jvm.memory.used 等必需指标。
- 在事故响应期间开启 /actuator/startup(Spring Boot 3.5+)和 /actuator/conditions 以检查自动配置决策;在启用 /actuator/httpexchanges 前注册 HttpExchangeRepository(如 InMemoryHttpExchangeRepository)用于请求审计;验证 /actuator/startup 和 /actuator/conditions 返回有效 JSON。
- 遵循最小权限原则:只暴露必需端点并限制敏感端点;使用 profile 特定的 YAML 不可变配置对齐各环境;单独监控 actuator 流量以检测抓取滥用或暴力尝试;在 CI/CD 流水线中脚本化 curl 探针做回归检查。
- 保持 SKILL.md 简洁,将冗长文档放入 references/ 以节省上下文。
适用边界 · 注意事项
- 不要在公网暴露 /actuator/env、/actuator/configprops、/actuator/logfile、/actuator/heapdump,以及 /actuator/beans 和 /actuator/mappings 等泄露内部结构的端点。
- 不要编写阻塞事件循环线程或耗时超过 250ms 的自定义健康指标,健康指标不应执行昂贵操作影响应用性能。
- 生产环境绝不能在没有认证的情况下暴露 actuator 端点;旧版 Spring Boot 可能缺少探针和 observation 特性,需注意与 3.5.x 约定兼容。