ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

SpringBoot监控怎么做?Actuator隐藏技巧

SpringBoot监控怎么做?Actuator隐藏技巧 项目上线只是开始真正的考验在运行时。CPU飙高、内存泄漏、接口变慢、线程死锁——这些问题不会提前打招呼。SpringBoot Actuator就是你的监控利器但大多数人只用了它10%的功能。今天聊聊那些藏在文档角落里的实用技巧。一、引入依赖打开监控大门xml复制下载运行dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-actuator/artifactId /dependency加完依赖默认只暴露/actuator/health和/actuator/info两个端点。想用更多必须手动开启yaml复制下载management: endpoints: web: exposure: include: endpoint: health: show-details: always注意生产环境千万别用include: /actuator/env、/actuator/heapdump这些端点暴露出去等于把家门钥匙插在门上。二、隐藏技巧一自定义Health Indicator默认的健康检查只告诉你“UP”或“DOWN”但业务系统需要更细粒度的检查。比如数据库连接池是否耗尽、Redis是否可达、第三方API是否正常。自定义HealthIndicatorjava复制下载Component public class CustomHealthIndicator implements HealthIndicator { Override public Health health() { if (isServiceHealthy()) { return Health.up() .withDetail(database, connected) .withDetail(redis, connected) .withDetail(responseTime, 12ms) .build(); } return Health.down() .withDetail(reason, 数据库连接池耗尽) .build(); } }这样/actuator/health返回的就不只是状态还有具体原因。配合Kubernetes的liveness和readiness探针能实现真正的自愈。三、隐藏技巧二自定义Metrics指标Micrometer是Actuator的度量门面默认帮你采集JVM、HTTP请求、数据库连接等指标。但业务指标才是最有价值的。比如统计每分钟订单创建数java复制下载Service public class OrderService { private final Counter orderCounter; private final Timer orderTimer; public OrderService(MeterRegistry registry) { this.orderCounter Counter.builder(order.created.total) .description(订单创建总数) .tag(type, normal) .register(registry); this.orderTimer Timer.builder(order.create.duration) .description(订单创建耗时) .register(registry); } public void createOrder() { orderTimer.record(() - { // 业务逻辑 orderCounter.increment(); }); } }这些指标会出现在/actuator/metrics/order.created.totalPrometheus直接抓取Grafana画图业务趋势一目了然。四、隐藏技巧三动态修改日志级别线上出问题想看DEBUG日志但不想重启应用/actuator/loggers端点就是干这个的bash复制下载# 查看所有日志级别 curl http://localhost:8080/actuator/loggers # 动态修改某个包的日志级别 curl -X POST http://localhost:8080/actuator/loggers/com.example.service \ -H Content-Type: application/json \ -d {configuredLevel: DEBUG}排查完再改回INFO整个过程无需重启。这个技巧在定位线上问题时能救命。五、隐藏技巧四线程池监控线程池满了却不自知接口全部阻塞。通过自定义Metrics暴露线程池状态java复制下载Bean public ExecutorService monitorExecutor(MeterRegistry registry) { ThreadPoolExecutor executor new ThreadPoolExecutor( 10, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue(200) ); Gauge.builder(executor.active, executor, e - e.getActiveCount()).register(registry); Gauge.builder(executor.queue, executor, e - e.getQueue().size()).register(registry); Gauge.builder(executor.completed, executor, e - e.getCompletedTaskCount()).register(registry); return executor; }队列长度持续增长说明消费能力不足活跃线程数长期等于最大值说明需要扩容。数据不会说谎。六、隐藏技巧五/actuator/info 不只是摆设很多人忽略了info端点其实它能展示构建信息、Git提交记录、自定义属性。配合git-commit-id-plugin每次部署后访问/actuator/info就能知道当前跑的是哪个commityaml复制下载management: info: git: mode: full env: enabled: true再也不用问“这个bug修复上线了吗”看一眼commit id就知道。七、安全加固别让监控成为漏洞Actuator的端点必须加权限。Spring Security配置java复制下载Configuration public class ActuatorSecurityConfig { Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.requestMatcher(EndpointRequest.toAnyEndpoint()) .authorizeRequests() .anyRequest().hasRole(ADMIN) .and().httpBasic(); return http.build(); } }或者直接把Actuator端口和管理端口分离yaml复制下载management: server: port: 9090 endpoints: web: base-path: /manage9090端口只对内网开放8080对外服务安全又清晰。结语Actuator不是加个依赖就完事了它是一套完整的生产级监控方案。Health Indicator做健康检查Metrics做业务度量Loggers做动态调试Info做版本追踪。把这些隐藏技巧用起来你的SpringBoot应用才算真正具备了可观测性。监控不是运维的事是每个后端开发者的基本功。
返回列表