
朋友们好,执行在本教程中,器教我们将了解 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”) }</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 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= *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 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} </plugin>现在让我们再次点击 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 的截图中的。  configpropsconfigProps 端点为您提供所有使用 @ConfigurationProperties 注释的 bean。 
在上面的屏幕截图中,我们可以看到 Spring 框架本身预定义的两个 bean,并使用 @ConfigurationProperties 进行注释,因此显示在此端点下。 下面的屏幕截图显示了带有 @ConfigurationProperties 注释的 HttpTraceProperties 的源代码。  envenv 端点按以下顺序为您提供所有特定于环境的信息: 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 的全部内容。 |