Spring Boot监控与管理(Actuator)

2025 年企业级 Spring Boot Actuator 终极生产配置

全国 99% 的大厂(阿里、腾讯、字节、美团)后台服务监控都是这套!直接复制到项目,5 分钟拥有生产级可观测性。

一、2025 年最终结论(直接背)

项目推荐配置说明
监控框架Actuator + Micrometer + Prometheus官方标配,无替代方案
指标采集Prometheus Pull 模式行业标准
可视化Grafana(导入 4318/14225/14268 模板)最美最强
链路追踪Actuator + SkyWalking 或 OpenTelemetry推荐 SkyWalking(国内最强)
健康检查Kubernetes liveness/readiness必须暴露
日志Actuator + Loki 或 ELK可选

二、一分钟拥有完整生产级监控(直接复制)

<!-- pom.xml -->
<dependencies>
    <!-- Actuator 核心 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- Prometheus 指标暴露(2025 必备) -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>

    <!-- 可选:健康检查增强 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
</dependencies>
# application-prod.yml(生产终极版)
management:
  server:
    port: 8090                      # 监控端口单独隔离(强烈推荐!)
  endpoints:
    web:
      base-path: /actuator            # 默认就是这个
      exposure:
        include: 
          - health
          - info
          - metrics
          - prometheus
          - threaddump
          - heapdump
          - logfile
          - httptrace
          - env
          - beans
          - conditions
          - shutdown                 # 谨慎开启!
      # 安全控制(推荐用网关或防火墙限制IP)
  endpoint:
    health:
      show-details: always          # K8s 探针必须
      show-components: always
      probes:
        enabled: true               # 开启 liveness/readiness
    shutdown:
      enabled: false                # 生产慎开!默认关闭
    prometheus:
      enabled: true
  metrics:
    tags:
      application: ${spring.application.name}
      env: prod
    distribution:
      percentiles-histogram:
        http.server.requests: true   # 响应时间分位图
      slo:
        http.server.requests: 50ms,100ms,200ms,500ms,1s
  tracing:
    sampling:
      probability: 0.1              # 10% 链路采样(生产推荐)
  info:
    env:
      enabled: true
    java:
      enabled: true
    os:
      enabled: true

# 自定义 info 信息(超实用)
info:
  app:
    name: ${spring.application.name}
    version: @project.version@
    profile: ${spring.profiles.active}
    startup: ${spring.application.startup-time}

三、关键端点一览表(2025 必开)

路径功能生产是否暴露用途
/actuator入口Yes查看所有可用端点
/actuator/health健康检查YesK8s liveness/readiness 探针
/actuator/health/liveness存活探针YesK8s 用
/actuator/health/readiness就绪探针YesK8s 用
/actuator/prometheusPrometheus 拉取指标YesPrometheus 采集
/actuator/metrics所有指标列表Yes调试用
/actuator/info应用信息Yes服务注册中心展示
/actuator/env环境变量Caution敏感信息,建议限制IP
/actuator/threaddump线程 dumpCaution排查死锁
/actuator/heapdump堆 dump(自动生成 hprof 文件)Caution内存分析
/actuator/logfile实时日志(配合 logback)Caution在线查看日志

四、Grafana 超漂亮 Dashboard 模板 ID(直接导入)

模板名称ID说明
JVM (Micrometer)4701经典 JVM 监控
Spring Boot 3 Statistics14225最全 Spring Boot 3 专用
Spring Boot 3 + Prometheus142682025最新推荐
Micrometer + Prometheus4318官方推荐
Undertow 专用15618高性能容器监控

导入地址:https://grafana.com/grafana/dashboards/ID

五、生产安全加固(必做!)

# 方式1:单独监控端口(最推荐)
management:
  server:
    port: 8090
  endpoints:
    web:
      base-path: /actuator

# 方式2:IP 白名单(Nginx 反向代理)
location /actuator {
    allow 10.0.0.0/8;     # 内网
    allow 172.16.0.0/12;
    deny all;
    proxy_pass http://127.0.0.1:8090;
}

# 方式3:Spring Security 保护(代码方式)
@Configuration
public class ActuatorSecurityConfig {
    @Bean
    public SecurityFilterChain actuatorSecurity(HttpSecurity http) throws Exception {
        http.requestMatchers().antMatchers("/actuator/**")
            .and()
            .authorizeHttpRequests()
            .antMatchers("/actuator/health/**", "/actuator/prometheus", "/actuator/info").permitAll()
            .anyRequest().hasIpAddress("127.0.0.1").or().hasIpAddress("10.0.0.0/8");
        return http.build();
    }
}

六、自定义监控指标(超实用)

@Component
public class CustomMetrics {

    private final Counter orderCounter = Counter.builder("order.create.total")
        .description("订单创建总数")
        .register(Metrics.globalRegistry);

    private final Timer timer = Timer.builder("order.process.time")
        .description("订单处理耗时")
        .publishPercentiles(0.5, 0.95, 0.99)
        .register(Metrics.globalRegistry);

    public void createOrder() {
        orderCounter.increment();

        timer.record(() -> {
            // 业务代码
            orderService.create();
        });
    }
}

七、配合 Kubernetes 完美配置(2025 标配)

# deployment.yaml
ports:
  - containerPort: 8080
    name: http
  - containerPort: 8090      # 监控端口
    name: metrics

livenessProbe:
  httpGet:
    path: /actuator/health/liveness
    port: 8090
  initialDelaySeconds: 60
  periodSeconds: 30

readinessProbe:
  httpGet:
    path: /actuator/health/readiness
    port: 8090
  initialDelaySeconds: 30
  periodSeconds: 10

最终效果(启动后访问)

  • 健康检查:http://ip:8090/actuator/health → {"status":"UP"}
  • Prometheus 采集:http://ip:8090/actuator/prometheus
  • Grafana 看板:实时监控 JVM、GC、线程、HTTP 请求分位值、自定义业务指标

现在你已经拥有了 2025 年最完整、最安全、最漂亮的生产级监控体系!

下一步你要哪个完整方案?

  • 完整 Prometheus + Grafana + Alertmanager 报警规则(CPU>80%、内存泄漏、慢接口)
  • 整合 SkyWalking 实现全链路追踪(零侵入)
  • 分布式日志 Loki + Grafana 查看 logfile 端点
  • 自定义健康检查(数据库连接、Redis、MQ)
    直接告诉我,我把完整配置 + Docker Compose + 报警规则全发给你!
文章已创建 3070

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部