Spring Boot 执行器教程

开发 架构
开发应用程序并将其部署到生产环境后,检查已启动和运行的应用程序的运行状况非常重要,特别是对于银行应用程序等任务关键型应用程序,如果面向客户的应用程序存在下降,将直接影响银行的业务。

朋友们好,在本教程中,我们将了解 Spring 执行器及其对我们的帮助。

1.什么是弹簧执行器?

2. Maven项目或Gradle项目如何添加Spring actuator?

3、创建一个Spring Boot项目,依赖Spring Actuator。

4. 使用 Spring Actuator Endpoints 监控应用程序。

什么是弹簧执行器?

开发应用程序并将其部署到生产环境后,检查已启动和运行的应用程序的运行状况非常重要,特别是对于银行应用程序等任务关键型应用程序,如果面向客户的应用程序存在下降,将直接影响银行的业务。

在传统的方式中,在 Spring Actuator 之前,我们需要编写代码来检查应用程序的 Health,但是使用 Spring Actuator,我们不需要编写任何 Health Check 代码,但是 Spring Actuator 提供了一些开箱即用的端点,可以对于应用程序的健康检查非常有用。

如何将 Spring actuator 添加到 Maven 项目或 Gradle 项目?

马文

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"><依赖项></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<依赖></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<groupId>org.springframework.boot</groupId></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
<artifactId>spring-boot-starter-actuator</artifactId></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
</依赖></font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
</依赖></font></font>

摇篮

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">依赖{</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
编译(“org.springframework.bootspring-boot-starter-actuator”)</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
}</font></font>

使用 Spring Actuator 依赖项创建 Spring Boot 项目

让我们继续使用 Spring Initializer https://start.spring.io/创建一个具有 Spring Actuator 依赖项(以及 Web 和 DevTools)的 Spring Boot 项目。

请注意,在编写本教程时,Spring Boot 版本为 2.1.0。

在 Eclipse 或您选择的任何其他 IDE 中导入项目并运行SpringActuatorApplication.java。

您将在 Eclipse 控制台中看到以下内容:

这表明嵌入式 Tomcat 已在 8080 端口启动,SpringActuatorApplication 已在 Tomcat 上启动。同样在控制台日志中,您可以看到执行器端点通过 /actuator URI 公开。

018-11-09 20:00:29.346 INFO 8338 — [restartedMain]。

osbwembedded.tomcat.TomcatWebServer:Tomcat 在端口上启动:8080 (http) 与上下文路径”。

2018-11-09 20:00:29.354 INFO 8338 — [restartedMain]。

nbjsSpringActuatorApplication:在 9.273 秒内启动 SpringActuatorApplication(JVM 运行时间为 11.823)。

2018-11-09 20:00:29.190INFO 8338 — [restartedMain]。

osbaeweb.EndpointLinksResolver:在基本路径“/actuator”下暴露 2 个端点。

使用 Spring Actuator Endpoints 监控应用程序。

正如我们上面所讨论的,Spring 执行器提供了一些开箱即用的端点,我们可以使用它们来监控应用程序的健康状况。

启用端点

默认情况下,除关闭之外的所有端点都已启用。要启用端点,请在 application.properties 文件中使用以下属性。

management.endpoint.<id>.enabled

例子:

要启用关闭端点,我们需要在 application.properties 文件中创建以下条目:

management.endpoint.shutdown.enabled=true

或者,我们可以禁用所有端点,然后有选择地启用我们想要的端点。使用以下配置,除了 info 之外的所有端点都将被禁用。

management.endpoints.enabled-by-default=false<font></font>
<font></font>
management.endpoint.info.enabled=true

端点执行器

让我们点击 URLhttp://localhost:8080/actuator 并查看端点。

注意:我正在使用 Postman 测试端点,因为它以结构良好的格式显示 JSON。您可以自由使用任何其他此类工具或仅使用浏览器。

在 Spring Actuator 中暴露端点

正如您已经注意到的那样,这里只能看到健康和信息端点。这是因为这些是默认公开的唯一端点。出于安全原因,默认情况下不会公开所有端点,因为它们可能包含一些敏感信息,因此可能会受到损害。

暴露特定端点

如果我们想通过 Web(Http) 公开其他端点,我们需要在 application.properties 文件中创建以下条目。

management.endpoints.web.exposure.include= <Comma separated list of Ids of endpoints which we want to expose>

例子

management.endpoints.web.exposure.include= health,info,env

现在在 application.properties 中添加上述条目后,让我们再次点击 URLhttp://localhost:8080/actuator。

正如我们在下面的屏幕截图中看到的,env 端点也被启用。

暴露所有端点

如果我们想启用所有端点,我们可以在 application.properties 中使用通配符 *,如下所示。

management.endpoints.web.exposure.include=*。

暴露除少数特定端点外的所有端点

以下两个条目将启用所有端点,但仅禁用 env 端点。

management.endpoints.web.exposure.include=*<font></font>
<font></font>
management.endpoints.web.exposure.exclude=env

禁用 HTTP 端点

如果您不想通过 HTTP 公开端点,可以通过在 application.properties 中配置以下内容来完成:

management.server.port=-1

或者,您可以在 application.properties 中配置以下内容:

management.endpoints.web.exposure.exclude=*

自定义执行器 URL 以访问各种端点

默认情况下,所有 Web 端点都在 /actuator 下可用,其 URL 格式为 /actuator/{id}。

但是,可以通过在 application.properties 中配置以下属性来配置基本路径 /actuator。

management.endpoints.web.base-path

例如,如果要将基本 URL 设为 /monitor 而不是 /actuator。

则可以在 application.properties 中进行如下配置:

management.endpoints.web.base-path=/monitor

这样,所有端点都可以作为 /monitor/{id} 而不是 /actuator/{id} 访问。

Spring Boot 执行器端点

让我们讨论一些最重要的端点。

健康

健康端点提供应用程序的状态,如果它是启动和运行与否。这对于在生产中监控应用程序的运行状况非常重要。该端点可以与监控应用程序集成,将非常有助于告知应用程序的实时运行状况。

健康信息

将暴露多少健康端点信息取决于 application.properties 文件中属性management.endpoint.health.show-details 的配置。

如果management.endpoint.health.show-details=never,则从不显示详细信息。在这种情况下,您只会看到以下信息。这也是默认行为。

如果management.endpoint.health.show-details=always,详细信息会显示给所有用户。所以我们可以在下面的响应中看到,我们也有磁盘空间信息。如果您的应用程序连接到数据库,那么您还将获得有关数据库运行状况的信息。

如果management.endpoint.health.show-details=when-authorized,详细信息仅显示给授权用户。授权角色可以使用management.endpoint.health.roles 属性进行配置。

自动配置的健康指标

Spring Boot Actuator 有许多自动配置的 HeathIndicators 来检查应用程序各个部分的健康状况。例如,Spring Boot Actuator 提供了 DiskspaceHealthIndicator,它提供了有关应用程序使用的磁盘空间健康状况的信息。同样,如果您使用的是 MongoDB,那么 MongoHealthIndicator 将检查 Mongo DB 的健康状况(是否为 UP)并显示相关信息。默认情况下,最终应用程序状态由 HealthAggregator 派生,它基本上根据状态的有序列表对来自每个 HealthIndicator 的状态进行排序。排序列表中的第一个状态用作应用程序的最终状态。

禁用所有自动配置的运行状况指标

这些健康指标默认启用,但是,可以使用以下属性禁用它们:

management.health.defaults.enabled=false

禁用单个自动配置的运行状况指示器或者,也可以禁用单个 HealthIndicator,如下所示,例如禁用磁盘空间的健康检查:

management.health.diskspace.enabled=false

注意:任何 HealthIndicator 的标识符都是没有 HealthIndicator 后缀的 bean 的名称。

例如 :

DiskSpaceHealthIndicator       diskspace<font></font>
MongoHealthIndicator mongo<font></font>
CassandraHealthIndicator cassandra<font></font>
DataSourceHealthIndicator datasource

等等…

自定义健康指标

除了 Spring Boot Actuator 提供的内置 HealthIndicators 之外,我们还可以创建自己的自定义 Health Indicators。为此,您需要创建一个实现 HealthIndicator 接口并实现其 health() 方法的类,并将 Health 作为响应返回,相关信息如下:

import org.springframework.boot.actuate.health.Health;<font></font>
import org.springframework.boot.actuate.health.HealthIndicator;<font></font>
import org.springframework.stereotype.Component;<font></font>
<font></font>
@Component<font></font>
public class CustomHealthIndicator implements HealthIndicator {<font></font>
<font></font>
@Override<font></font>
public Health health() {<font></font>
int errorCode = 0; <font></font>
// In the above line,I am simple assigning zero,but you can call Health check related code like below commented line and that method can return the appropriate code.<font></font>
// int errorCode = performHealthCheck();<font></font>
if (errorCode != 0) {<font></font>
return Health.down().withDetail("Error Code", errorCode).build();<font></font>
}<font></font>
return Health.up().build();<font></font>
}<font></font>
<font></font>
}

现在让我们再次点击健康端点,看看我们的自定义健康指标是否得到反映。

正如我们在上面的屏幕截图中看到的那样,已包含自定义健康检查。

每个组件的健康状况

也可以检查单个组件的健康状态。在上面的示例中,我们看到了自定义健康状态以及 diskSpace 健康状态。

如果我们只想查看 diskSpace 的健康状况,那么我们可以执行以下操作:http://localhost:8080/actuator/health/diskSpace。

信息

info 端点提供有关应用程序的一般信息,它从 build-info.properties 或 git.properties 等文件或从 application.properties 中的关键信息下的任何属性中获取。

在我们的项目中,没有这样的文件,所以如果我们点击 info 端点,它将只显示空响应,如下所示:

如果存在META-INF/build-info.properties 文件,Spring Boot Actuator 会显示与构建相关的信息。 build-info目标生成带有项目坐标和构建时间的此类文件。它还允许您添加任意数量的附加属性。

让我们在我们项目的 pom.xml 中添加一个 build-info 目标,如下所示在 spring-boot-maven-plugin 插件中。

<plugin><font></font>
<groupId>org.springframework.boot</groupId><font></font>
<artifactId>spring-boot-maven-plugin</artifactId><font></font>
<version>2.1.0.RELEASE</version><font></font>
<executions><font></font>
<execution><font></font>
<goals><font></font>
<goal>build-info</goal><font></font>
</goals><font></font>
<configuration><font></font>
<additionalProperties><font></font>
<encoding.source>UTF-8</encoding.source><font></font>
<encoding.reporting>UTF-8</encoding.reporting><font></font>
<java.source>${maven.compiler.source}</java.source><font></font>
<java.target>${maven.compiler.target}</java.target><font></font>
</additionalProperties><font></font>
</configuration><font></font>
</execution><font></font>
</executions><font></font>
</plugin>

现在让我们再次点击 info 端点,我们可以看到构建信息如下:

此外,我们可以在 application.properties 中的 info 键下添加应用程序信息,如下所示,同样将显示在 /info 端点中。

info.application.name=spring-actuator<font></font>
info.application.description=spring boot actuator application<font></font>
info.application.version=0.0.1-SNAPSHOT

豆子

beans 端点为 Spring bean 容器中定义的所有 bean 提供了有关每个 bean 的以下信息:

aliases  : Names of any aliases<font></font>
Scope : Scope of bean<font></font>
type : Fully qualified type of a bean.<font></font>
resource : Resource(class) in which bean is defined.<font></font>
dependencies :names of dependent beans.

例如,我创建了一个名为 TestController.java 的 RestController 并注入了一个名为 TestService.java 的 bean

import org.springframework.beans.factory.annotation.Autowired;<font></font>
import org.springframework.web.bind.annotation.GetMapping;<font></font>
import org.springframework.web.bind.annotation.RestController;<font></font>
<font></font>
@RestController<font></font>
public class TestController {<font></font>
<font></font>
@Autowired<font></font>
private TestService testService;<font></font>
<font></font>
@GetMapping("/messages")<font></font>
public String getMessage() {<font></font>
return "Hello";<font></font>
}<font></font>
}
import org.springframework.context.annotation.Configuration;<font></font>
<font></font>
@Configuration<font></font>
public class TestService {<font></font>
<font></font>
}

你可以看到它是如何反映在下面带有 id testController 的截图中的。

configprops

configProps 端点为您提供所有使用 @ConfigurationProperties 注释的 bean。

在上面的屏幕截图中,我们可以看到 Spring 框架本身预定义的两个 bean,并使用 @ConfigurationProperties 进行注释,因此显示在此端点下。

下面的屏幕截图显示了带有 @ConfigurationProperties 注释的 HttpTraceProperties 的源代码。

env

env 端点按以下顺序为您提供所有特定于环境的信息:

System Properties                     - JVM specific(Platform Independent)<font></font>
System Env. or Env. Variables - Operating System specific(Platform Dependent)<font></font>
application level configuration - Defined in application.properties

堆转储

heapdump 端点从应用程序 JVM 提供堆转储。此端点以 HPROF 格式返回二进制数据。由于返回的数据通常很大,您应该将其保存并分析。

记录器

loggers 端点提供应用程序的记录器及其配置级别、有效级别(如果此记录器的配置级别为空并且它也是父级的,则有效级别将是根记录器的记录器级别)。

levels 属性告诉日志框架支持哪些所有级别。

特定记录器的记录器信息

要获取特定记录器的记录器信息,请在 /loggers 端点之后的 URL 中传递记录器的名称/ID,如下所示:

http://localhost:8080/actuator/loggers/nl.blogpsot.javasolutionsguide.springactuator.SpringActuatorApplication。

指标

指标端点为您提供可以为您的应用程序跟踪的所有指标。

检查单个指标

您可以通过将特定指标传递到 /metrics 端点之后的 URL 来跟踪单个指标,如下所示:

http://localhost:8080/actuator/metrics/jvm.memory.used。

以上就是 Spring Actuator 的全部内容。

责任编辑:姜华 来源: 今日头条
相关推荐

2022-05-05 08:43:22

SQL执行器参数

2017-07-13 17:00:17

内置执行器开发

2023-08-24 10:24:54

GitLabPodman

2020-10-16 08:26:07

JavaScript开发技术

2022-09-04 18:23:33

asyncJavascript异步编程

2015-04-16 09:38:23

2023-11-07 07:56:40

2021-07-21 10:48:03

物联网传感器执行器

2022-02-09 10:06:21

触觉设备计算机人工智能

2021-03-04 10:11:50

MongoDBSpring BootSpring Boot

2022-02-17 13:39:09

AOP接口方式

2023-09-27 08:14:56

2010-07-16 10:19:31

2023-11-30 07:00:56

SpringBoot处理器

2017-09-20 09:46:38

Spring BootSpring Clou内存

2023-06-02 16:24:46

SpringBootSSM

2022-05-18 08:32:05

服务监控Prometheus开源

2020-12-15 10:46:29

事件监听器Spring Boot

2022-04-28 08:05:05

数据库数据库交互

2021-09-15 09:02:20

Spring 6Spring BootJava
点赞
收藏

51CTO技术栈公众号