锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

Spring Boot 执行器教程

时间:2023-02-11 07:00:00 弹簧目标连接器

朋友们好,我们将在本教程中理解 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 项目?

马文

<依赖项>  <依赖>   org.springframework.boot   spring-boot-starter-actuator   

摇篮

依赖{  编译(“org.springframework.boot:spring-boot-starter-actuator”) }

使用 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..enabled

例子:

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

management.endpoint.shutdown.enabled=true

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

management.endpoints.enabled-by-default=false  management.endpoint.info.enabled=true

端点执行器

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

注:我在用 Postman 测试端点以良好的结构格式显示 JSON。您可以自由使用任何其他工具或只使用浏览器。

在 Spring Actuator 中暴露端点

正如你所注意到的,你只能在这里看到健康和信息端点。这是因为这些是默认披露的唯一端点。由于安全原因,所有端点不会在默认情况下披露,因为它们可能包含一些敏感信息,因此可能会损坏。

暴露特定端点

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

management.endpoints.web.exposure.include= 

例子

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=*  management.endpoits.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
MongoHealthIndicator             mongo
CassandraHealthIndicator        cassandra
DataSourceHealthIndicator      datasource

等等…

自定义健康指标

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

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {

 @Override
 public Health health() {
  int errorCode = 0; 
  // 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.
  // int errorCode = performHealthCheck();
  if (errorCode != 0) {
   return Health.down().withDetail("Error Code", errorCode).build();
  }
  return Health.up().build();
 }

}

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

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

每个组件的健康状况

也可以检查单个组件的健康状态。在上面的示例中,我们看到了自定义健康状态以及 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 插件中。


        org.springframework.boot
        spring-boot-maven-plugin
        2.1.0.RELEASE
        
          
            
              build-info
            
            
              
                UTF-8
                UTF-8
                ${maven.compiler.source}
                ${maven.compiler.target}
              
            
          
        
 

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

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

info.application.name=spring-actuator
info.application.description=spring boot actuator application
info.application.version=0.0.1-SNAPSHOT

豆子

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

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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Configuration
public class TestService {

}

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

configprops

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

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

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

env

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

System Properties                     - JVM specific(Platform Independent)
System Env. or Env. Variables  - Operating System specific(Platform Dependent)
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 的全部内容。

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章