敬礼,哈布里沃派! 在一周内,课程将在
“ Spring框架开发人员 ”课程的新组中开始。 在这方面,我们与您分享有用的材料,这些材料告诉您什么是弹簧执行器以及它如何发挥作用。

- 什么是弹簧执行器?
- 如何将Spring Actuator添加到Maven或Gradle项目?
- 创建一个具有Spring Actuator依赖项的Spring Boot项目。
- 使用弹簧执行器端点进行应用监控。
什么是弹簧执行器?开发应用程序并将其部署到生产环境中之后,监视其性能非常重要。 对于诸如银行系统之类的关键任务应用程序尤其如此,其中应用程序故障直接影响业务。
传统上,在使用Spring Actuator之前,我们需要编写代码来测试应用程序的运行状况,但是使用Spring Actuator时,我们不需要编写代码。 Spring Actuator提供了几个端点,可用于监视应用程序。
如何将Spring Actuator添加到Maven或Gradle项目?马文<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
摇篮 dependencies { compile("org.springframework.boot:spring-boot-starter-actuator") }
使用Spring Actuator创建Spring Boot项目让我们继续使用
Spring Initializer创建一个具有Spring Actuator,Web和DevTools依赖项的Spring Boot项目。
请注意,在撰写本文时,Spring Boot版本为2.1.0。

将项目导入Eclipse或任何其他IDE并运行
SpringActuatorApplication.java
。
在控制台中,您将看到以下内容:

您可以看到嵌入式Tomcat在端口8080
SpringActuatorApplication
运行,而
SpringActuatorApplication
在Tomcat
SpringActuatorApplication
运行。 您还可以看到
/actuator.
有执行器端点
/actuator.
018-11-09 20:00:29.346 INFO 8338 --- [ restartedMain] osbwembedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-11-09 20:00:29.354 INFO 8338 --- [ restartedMain] nbjsSpringActuatorApplication : Started SpringActuatorApplication in 9.273 seconds (JVM running for 11.823) 2018-11-09 20:00:29.190 INFO 8338 --- [ restartedMain] osbaeweb.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'.
弹簧执行器端点的应用监控如上所述,Spring Actuator提供了多个端点,可用来监视应用程序的运行状况。
启用端点默认情况下,除
shutdown
之外,所有端点均启用。 要启用端点,请在
application.properties
文件中使用以下属性。
management.endpoint.<code><</code>id<code>></code>.enabled
译者注:默认情况下,仅通过JMX访问所有端点,而没有通过HTTP访问所有端点的权限(请参见下文)。
一个例子:要启用
shutdown
端点,我们需要在
application.properties
文件中进行以下输入:
management.endpoint.shutdown.enabled=true
我们可以禁用所有端点,然后仅包含我们需要的端点。 在下一个配置中,将禁用除
info
之外的所有端点。
management.endpoints.enabled-by-default=false management.endpoint.info.enabled=true
通过HTTP访问端点让我们转到
localhost URL:8080 / actuator,查看可用的端点。
注意:我使用
Postman进行测试,因为它以结构良好的格式显示JSON。 您可以使用任何其他工具或仅使用浏览器。

您已经注意到,此处仅显示
health
和
info
端点。 因为这些是默认情况下只能通过http访问的唯一端点。 由于安全原因,默认情况下将关闭通过http到其他端点的访问,因为它们可能包含机密信息,因此可能会受到威胁。
访问特定端点如果要通过Web(http)提供对其他端点的访问,则需要在
application.properties
文件中进行以下输入。
management.endpoints.web.exposure.include=< ><a href="http://localhost:8080/actuator"></a>
一个例子 :
management.endpoints.web.exposure.include= health,info,env
现在,将上面的条目添加到
application.properties
,让我们再次转到
http:// localhost:8080 / ctuator正如我们在下面的屏幕快照中看到的,
env
端点也包括在内。
访问所有端点如果要包括所有端点,则可以使用
*
符号,如下所示。
management.endpoints.web.exposure.include=*
访问除少数几个端点外的所有端点下面的两个条目激活所有端点,但禁用环境端点。
management.endpoints.web.exposure.include=* management.endpoints.web.exposure.exclude=env

禁用所有HTTP端点如果您不想通过HTTP提供端点,则可以通过在
application.properties
文件中设置以下内容来实现:
management.server.port=-1
左右:
management.endpoints.web.exposure.exclude=*
配置用于访问端点的URL默认情况下,所有端点都可以通过URL
/actuator
访问,格式为
/actuator/{id}
。 但是,您可以使用
application.properties
的以下属性来更改基本路径
/actuator
。
management.endpoints.web.base-path
例如,如果要将基本URL设置为
/monitor
而不是
/actuator
可以执行以下操作:
management.endpoints.web.base-path=/monitor

在这种情况下,所有端点都将用作
/monitor/{id}
而不是
/actuator/{id}
端点弹簧启动执行器让我们讨论一些最重要的端点。
/健康health
终结点提供了应用程序的总体状态:是否已启动并正在运行。 这对于在生产中监视应用程序的状态非常重要。 该端点可以与监视应用程序集成在一起,对于实时确定应用程序的运行状况非常有用。
health
终结点提供的信息量取决于
application.properties
文件中的
management.endpoint.health.show-details
属性。
如果
management.endpoint.health.show-details=never
,则不会显示其他信息。 在这种情况下,您只会看到以下内容(这是默认行为)。

如果
management.endpoint.health.show-details=always
,那么将向所有用户显示其他信息。 正如我们在下面的答案中看到的那样,我们具有有关磁盘空间(diskSpace)的信息。 如果您的应用程序已连接到数据库,那么您还将看到有关数据库状态的信息。

如果
management.endpoint.health.show-details=when-authorized
,则仅向授权用户显示其他信息。 可以使用
management.endpoint.health.roles
属性配置授权。
预设指标Spring Boot Actuator具有许多自动配置的“运行状况指示器”(HeathIndicators),用于测试应用程序各个部分的运行状况。 例如,
DiskspaceHealthIndicator
提供磁盘空间信息。 如果您使用的是MongoDB,则
MongoHealthIndicator
将检查Mongo数据库(服务器是否正在运行)并显示相应的信息。 默认情况下,应用程序的最终状态由
HealthAggregator
确定,该方法
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提供的内置
HealthIndicator
,我们可以创建自定义状态指示器。 为此,您需要创建一个实现
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;
让我们再次转到运行状况端点,看看是否反映了我们的指标。

我们看到了指标。
单组件状态您还可以检查单个组件的状态。 在上面的示例中,我们看到了编写的指标和diskSpace。
如果我们只想查看磁盘的状态,则可以使用以下URL:
http://本地主机:8080 /执行器/运行状况/ diskSpace
/资讯info
终结点提供了有关它从诸如
build-info.properties
或
git.properties
文件或从
application.properties
指定的属性接收的应用程序的常规信息。
由于我们的项目中没有此类文件,因此答案将为空,如下所示:

如果存在
META-INF/build-info.properties
Spring Boot Actuator将显示构建信息。 该项目信息文件是在
build-info
时由
build-info
目标创建的。 在这里,您还可以添加任意数量的其他属性。
让我们在
pom.xm
l中添加
spring-boot-maven-plugin
的
build-info
目标。
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.0.RELEASE</version> <executions> <execution> <goals> <goal>build-info</goal> </goals> <configuration> <additionalProperties> <encoding.source>UTF-8</encoding.source> <encoding.reporting>UTF-8</encoding.reporting> <java.source>${maven.compiler.source}</java.source> <java.target>${maven.compiler.target}</java.target> </additionalProperties> </configuration> </execution> </executions> </plugin>
现在,让我们再次查看
info
端点,并查看程序集信息,如下所示:

另外,我们可以使用
info
键将
application.properties
程序信息添加到
application.properties
,如下所示,它将显示在端点
/info
。
info.application.name=spring-actuator info.application.description=spring boot actuator application info.application.version=0.0.1-SNAPSHOT
/豆beans
端点显示了在Spring容器中定义的所有
beans
,以及有关每个bean的以下信息:
aliases : scope : type : resource : (), dependencies :
例如,我创建了一个名为
TestController
的RestController并注入了一个名为
TestService
的组件。
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 { }
您可以在下面的屏幕截图中看到testController的显示方式。
/ configpropsconfigProps
显示所有
@ConfigurationProperties
注释的
@ConfigurationProperties
。

在上面的屏幕截图中,我们看到了两个在Spring Framework本身中定义的bean,并用
@ConfigurationProperties
注释,因此显示在此端点上。
下面的屏幕快照显示了
@ConfigurationProperties
注释的
HttpTraceProperties
源代码。
/环境env
端点按以下顺序提供所有环境信息:
/堆转储heapdump端点转储应用程序堆。 该端点以HPROF格式返回二进制数据。 由于通常会返回很多数据,因此您必须保存并分析它们。
/记录器loggers
为应用程序记录器提供有关其配置的日志级别(configuredLevel)和有效级别(effectiveLevel)的信息。 如果未为记录器及其父记录(空)指定配置的级别,则根记录器级别将是有效级别。
level
属性指示日志记录框架支持哪些日志记录级别。

要获取特定记录器的信息,请在
/loggers
后面的URL中传递记录器的名称(id),如下所示:
http://本地主机:8080 /作动器/记录器/ nl.blogpsot.javasolutionsguide.springactuator.SpringActuatorApplication
/指标metrics
终结点显示您可以为应用程序跟踪的所有指标。
检查单个指标您可以通过在
/metrics
之后的url中传递单个指标来观看单个
/metrics
,如下所示:
HTTP://本地主机:8080 /执行器/指标/jvm.memory.used
参考文献docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.htmldocs.spring.io/spring-boot/docs/current/actuator-api/html根据既定传统,我们正在等待您的评论,并邀请所有人参加将于5月23日举行
的开放日 。