个性化阅读
专注于IT技术分析

使用Spring Boot Actuator监视API

本文概述

弹簧启动器

Spring Boot提供了执行器, 可以有效地监视和管理应用程序。它是具有HTTP端点(资源所在的地方)的工具。它是Spring Boot的子项目。它以更少的努力为我们的应用程序增加了几种生产级服务。

如果服务的性能下降或出现故障, 我们应该尽快知道原因。我们需要围绕API构建监控, 尤其是在构建微服务时。 Spring Boot为提供监视提供了强大的支持。

要使用生产就绪功能, 我们将在pom.xml中添加spring-boot-actuator依赖项。

让我们在Spring Boot项目中添加监视服务。

步骤1:打开pom.xml并添加以下依赖项:

Spring Boot Starter执行器:它为你的服务提供了许多监视工具。

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

Spring Data Rest HAL浏览器:HAL使我们的API可以导出, 并且可以从API本身中轻松发现其文档。

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>

超文本应用程序语言(HAL)是一种简单的语言, 为在API中的资源之间提供超链接提供了一致且简便的方法。弹簧启动启动器执行器实际上是HAL格式。 HAL浏览器搜索API并标识链接。它在屏幕上显示了链接, 以便我们可以轻松浏览API。

步骤2:重新启动应用程序。

开始3:在浏览器中输入URL localhost:8080 / actuator, 然后按Enter键。如果不起作用, 请使用URL localhost:8080 / application。

它启动显示三个URL的执行器:自我, 健康和信息。

{"_links":{"self":{"href":"http://localhost:8080/actuator", "templated":false}, "health":{"href":"http://localhost:8080/actuator/health", "templated":false}, "health-component":{"href":"http://localhost:8080/actuator/health/{component}", "templated":true}, "health-component-instance":{"href":"http://localhost:8080/actuator/health/{component}/{instance}", "templated":true}, "info":{"href":"http://localhost:8080/actuator/info", "templated":false}}}

当我们单击运行状况URL时, 它将显示应用程序的运行状况。在下图中, 状态为up表示应用程序正在运行。

使用Spring Boot Actuator监视API

当我们单击信息URL时, 它将显示应用程序的信息。一对空白大括号表示没有可用信息。

使用Spring Boot Actuator监视API

要启用信息, 我们需要配置属性。

  • 打开application.properties文件并启用Web公开。
management.endpoints.web.exposure.include=*
  • 重新启动应用程序。
  • 通过使用URL localhost:8080 / actuator重新启动执行器。

它显示了很多URL。

HAL浏览器

要访问HAL浏览器, 请在浏览器中键入localhost:8080并按Enter键。

使用Spring Boot Actuator监视API

现在我们可以通过HAL浏览器访问执行器。

在资源管理器的文本框中输入/ actuator, 然后单击执行按钮。

使用Spring Boot Actuator监视API

它显示了与执行器有关的所有信息。促动器中最重要的是豆。

使用Spring Boot Actuator监视API

当我们单击bean的箭头时, 它将显示在spring boot项目中配置的所有bean。

使用Spring Boot Actuator监视API

如果我们要检查应用程序的状态, 可以单击运行状况链接。

使用Spring Boot Actuator监视API

它显示了应用程序的运行状况。

使用Spring Boot Actuator监视API

执行器中还有一个名为“度量”的链接。它显示有效指标的列表。

使用Spring Boot Actuator监视API

假设我们想知道应用程序使用了多少内存。我们已经访问了/jvm.memory.max。

使用Spring Boot Actuator监视API

在下图中, 该值指示应用程序使用的最大内存。

使用Spring Boot Actuator监视API

执行器中存在两个重要链接httptrace和映射。

使用Spring Boot Actuator监视API

httptrace显示了我们之前执行的所有请求。我们可以看到以前执行的请求的所有详细信息, 如下所示:

使用Spring Boot Actuator监视API

映射显示了映射到URL的所有不同内容。每当我们创建Web服务或Web应用程序时, 我们都会映射许多URL。

使用Spring Boot Actuator监视API

在本节中, 我们了解了执行器的功能。


赞(0)
未经允许不得转载:srcmini » 使用Spring Boot Actuator监视API

评论 抢沙发

评论前必须登录!