全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-18 7 分钟 ✍️ juanwangdev

内嵌容器监控与诊断

容器监控是保障服务稳定运行的关键,Spring Boot 提供了完善的监控体系。

Actuator 集成

添加依赖

XML
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

暴露监控端点

YAML
management:
  endpoints:
    web:
      exposure:
        include: health,metrics,threaddump,heapdump,info
  endpoint:
    health:
      show-details: always
    metrics:
      enabled: true

容器 Metrics 指标

Tomcat 指标监控

YAML
management:
  metrics:
    enable:
      tomcat: true

获取关键指标

Bash
# 请求统计
GET /actuator/metrics/tomcat.requests

# 线程池状态
GET /actuator/metrics/tomcat.threads.busy
GET /actuator/metrics/tomcat.threads.current

# 连接数
GET /actuator/metrics/tomcat.global.current

自定义 Metrics

注入监控指标

Java
@Component
public class ContainerMetrics {
    private final MeterRegistry registry;

    public ContainerMetrics(MeterRegistry registry) {
        this.registry = registry;
        registry.gauge("container.active.requests",
            Tags.of("type", "http"), this::getActiveRequests);
    }

    private double getActiveRequests() {
        // 返回当前活跃请求数
        return RequestCounter.getActiveCount();
    }
}

线程诊断

获取线程转储

Bash
# Actuator 方式
GET /actuator/threaddump

# JDK 工具方式
jstack <pid> > thread_dump.txt

分析线程状态

Java
@RestController
public class ThreadAnalysisController {
    @GetMapping("/thread-status")
    public Map<String, Integer> getThreadStatus() {
        Map<String, Integer> status = new HashMap<>();
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
        for (ThreadInfo info : threadBean.dumpAllThreads(false, false)) {
            status.merge(info.getThreadState().name(), 1, Integer::sum);
        }
        return status;
    }
}

健康检查

自定义健康指示器

Java
@Component
public class ContainerHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // 检查线程池状态
        int availableThreads = getAvailableThreads();
        if (availableThreads < 10) {
            return Health.down()
                .withDetail("availableThreads", availableThreads)
                .withDetail("message", "线程池即将耗尽")
                .build();
        }
        return Health.up()
            .withDetail("availableThreads", availableThreads)
            .build();
    }
}

Prometheus 集成

添加依赖

XML
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

配置 Prometheus 端点

YAML
management:
  endpoints:
    web:
      exposure:
        include: prometheus
  metrics:
    export:
      prometheus:
        enabled: true

常用诊断命令

场景命令/端点说明
线程分析/actuator/threaddump获取线程快照
堆转储/actuator/heapdump获取堆内存快照
指标查询/actuator/metrics/{name}查询具体指标
健康状态/actuator/health服务健康检查

注意:生产环境需限制 Actuator 端点访问,避免敏感信息泄露。

要点总结

  • Actuator 是容器监控的核心入口
  • 关注线程池、请求数、响应时间等关键指标
  • 集成 Prometheus 实现可视化监控
  • 自定义 HealthIndicator 实现业务健康检查
  • 线程转储是诊断问题的关键手段

📝 发现内容有误?点击此处直接编辑

← 上一篇 内嵌容器原理与选择
下一篇 → 连接器参数调优
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库