如何用Spring Boot Actuator跟踪HTTP请求和响应

x33g5p2x  于2022-09-30 转载在 Spring  
字(0.9k)|赞(0)|评价(0)|浏览(604)

Spring Boot Actuator端点让你监控并与你的应用程序互动。Spring Boot包括一些内置的端点,并允许你添加自己的端点。例如,健康端点提供基本的应用程序健康信息。在本教程中,我们将学习如何使用HTTP Tracing Actuator Endpoint。

激活Spring Boot Actuator

为了使用Spring Boot Actuator,你需要做的就是在pom.xml文件中包括以下依赖项。

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

默认情况下,只有**/health/info**两个端点被暴露。要暴露httptrace端点,你需要在你的application.properties中加入以下内容。

management.endpoints.web.exposure.include=httptrace

现在你可以为你的应用程序发出一些请求,然后检查actuator/httptrace端点。

curl http://localhost:8080/actuator/httptrace

下面是执行器httptrace端点的输出:``

你可以看到,返回的响应包含几个字段。

timestamp:收到请求的时间
principal:发出请求的认证用户,如果有的话
session:与该请求相关的会话
request:关于请求的信息,如方法、完整的URI或头信息。
response:关于响应的信息,如状态或头信息。
timeTaken:处理请求的时间。

除此之外,执行器还包含返回的请求和响应。

相关文章

微信公众号

最新文章

更多