五大搜索引擎 三大门户网站,濮阳公司网站建设企业,平面设计培训机构价位,网站建设规划要点详解一、Spring Boot Actuator简介
什么是Spring Boot Actuator#xff1f;Spring Boot Actuator 是 Spring Boot 提供的生产级监控和管理模块#xff0c;旨在帮助开发者实时监控应用状态、收集运行时指标#xff0c;并提供一系列管理端点#xff08;Endpoints#xff09;Spring Boot Actuator 是 Spring Boot 提供的生产级监控和管理模块旨在帮助开发者实时监控应用状态、收集运行时指标并提供一系列管理端点Endpoints它的关键特性是提供了一系列的Web接口通过它们能够快速诊断问题、优化性能并确保应用健康运行。
Actuator 的核心功能
1.1 应用健康检查 /health 端点检查应用关键组件如数据库、磁盘、消息队列是否正常。 1.2 性能指标监控 /metrics 端点提供 JVM 内存、CPU 使用率、HTTP 请求统计等指标。 1.3 动态日志调整 /loggers 端点运行时修改日志级别如临时开启 DEBUG 日志排查问题。 1.4 请求追踪HTTP Trace /httptrace 端点Spring Boot 2.x记录最近请求的 URL、方法、响应时间等。 1.5 线程与堆栈分析 /threaddump 端点导出当前所有线程状态用于诊断死锁或性能瓶颈。 /heapdump 端点生成 JVM 堆内存快照。
二、开启Http接口监控
在一般的分布式架构中我们一般有查看API接口的请求/响应的需求而Spring Boot Actuator通过其httptrace端点即能够返回基本的HTTP跟踪信息本文介绍该功能的使用 环境SpringBoot 2.7.6 1.为SpringBoot项目添加依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency2.添加yml配置
management:endpoint:health:show-details: alwaystrace:http:enabled: true # 启用HTTP追踪include:endpoints:jmx:exposure:include: *web:exposure:include: *# 暴露端点这里开启所有默认Web端点server:port: 8081 # 这里访问/actuator 端点的端口此时我们本地访问/actuator端点的地址为http://127.0.0.1/actuator返回结果如下 我们可以看到返回了很多Actuator的端点接口路径但并没有发现httptrace模块要开启接口监控功能我们还要注册一个HttpTraceRepository类型的Bean
3.声明存储Bean
Spring Boot Actuator 默认会把最近100次的HTTP请求记录到内存中对应的实现类是InMemoryHttpTraceRepository
package com.example.springhttptrace.config;import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class HttpActuatorConfig {BeanInMemoryHttpTraceRepository inMemoryHttpTraceRepository() {return new InMemoryHttpTraceRepository() ;}
}完成上面类配置启动项目并访问访问http://localhost:8081/actuator可以看到
注意Spirngboot2.x版本可以通过注入 InMemoryHttpTraceRepository来开启httptrace但Spirngboot3.x版本中是不能直接注入InMemoryHttpTraceRepository 开启需要使用httpExchanges开启http trace官方解释生产环境可能存在一些性能问题官方建议使用 zipKin或OpenTelemetry等开源框架
4.写测试API接口
Controller
public class BasicController {// http://127.0.0.1:8080/hello?namelisiRequestMapping(/hello)ResponseBodypublic String hello(RequestParam(name name, defaultValue unknown user) String name) {return Hello name;}// http://127.0.0.1:8080/userRequestMapping(/user)ResponseBodypublic User user() {User user new User();user.setName(theonefx);user.setAge(666);return user;}// http://127.0.0.1:8080/save_user?namenewNameage11RequestMapping(/save_user)ResponseBodypublic String saveUser(User u) {return user will save: name u.getName() , age u.getAge();}ModelAttributepublic void parseUser(RequestParam(name name, defaultValue unknown user) String name, RequestParam(name age, defaultValue 12) Integer age, User user) {user.setName(zhangsan);user.setAge(18);}
}访问http://127.0.0.1:8080/hello?namelisi 返回结果后再访问http://localhost:8081/actuator/httptrace结果如下 其中
timestamp请求发生的时间戳UTC 格式principal认证用户信息未认证时为 nullsession会话 ID未启用会话时为 nullrequest请求对象信息response响应对象信息timeTaken请求处理耗时毫秒未记录时为 null 值为 150 表示处理耗时 150ms
总结
Spring Boot Actuator的HTTP Trace功能为监控HTTP接口提供了便捷的解决方案它开箱即用地记录请求方法、URL、状态码和耗时等基础信息适合快速定位问题。然而该功能默认不记录请求体/响应体等关键数据内存存储仅保留100条记录且高频请求下可能产生性能开销生产环境建议优先考虑更专业的APM工具。
参考
https://docs.spring.io/spring-boot/reference/actuator/endpoints.html#page-title https://docs.spring.io/spring-boot/docs/3.1.0/reference/html/actuator.html#actuator.http-exchanges https://github.com/spring-projects/spring-boot/issues/17047