Spring Boot自带监控组件—Actuator介绍

Actuator介绍

Actuator是Spring Boot提供的应用系统监控的开源框架,它是Spring Boot体系中非常重要的组件。它可以轻松实现应用程序的监控治理,支持通过众多REST接口、远程Shell和JMX收集应用的运行情况。
Actuator的核心是端点(Endpoint),它用来监视、提供应用程序的信息,Spring Boot提供的spring-boot-actuator组件中已经内置了非常多的Endpoint(health、info、beans、metrics、httptrace、shutdown等),每个端点都可以启用和禁用。
Actuator也允许我们扩展自己的端点。通过JMX或HTTP的形式暴露自定义端点,Actuator会将自定义端点的ID默认映射到一个带/actuator前缀的URL。比如,health端点默认映射到/actuator/health。这样就可以通过HTTP的形式获取自定义端点的数据。

启用与暴露的区别

  • 启用端点:会创建对应的bean并进行数据采集(但是未必会暴露)。

  • 暴露:是在启用的前提下,暴露是指对外可以访问到这些数据。

Spring Boot集成Actuator应用监控框架

在pom.xml中新增依赖:

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

spring-boot-starter-actuator除了可以监控Web系统外,还可以监控后台服务等Spring Boot应用。

修改配置文件,配置Actuator端点,例如:

# 打开所有的监控点
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

最后,启动项目并在浏览器中输入http://localhost:8080/actuator,我们可以看到返回的是Actuator提供的各种数据接口信息。

Actuator监控端点

Actuator监控分成两类:原生端点和用户自定义端点。

原生端点是Actuator组件内置的,在应用程序中提供了众多Web接口。通过它们了解应用程序运行时的内部情况,原生端点可以分成3类:

  1. 应用配置类:可以查看应用在运行期的静态信息,比如自动配置信息、加载的Spring Bean信息、YML文件配置信息、环境信息、请求映射信息
  2. 度量指标类:主要是运行期的动态信息,如堆栈、请求连接、健康状态、系统性能等。
  3. 操作控制类:主要是指shutdown,用户可以发送一个请求将应用的监控功能关闭。
ID 描述 默认情况下是否启用
auditevents 公开当前应用程序的审核事件信息。 Yes
beans 显示应用程序中所有Spring bean的完整列表。 Yes
caches 公开可用的缓存 Yes
conditions 显示在配置和自动配置类上评估的条件以及它们匹配或不匹配的原因。 Yes
configprops 显示所有@ConfigurationProperties的有序列表。 Yes
env 公开Spring的ConfigurableEnvironment中的属性 Yes
flyway 显示已应用的任何Flyway数据库迁移。 Yes
health 显示应用健康信息。 Yes
httptrace 显示HTTP跟踪信息(默认情况下,最后100个HTTP请求 – 响应交换)。 Yes
info 显示任意应用信息。 Yes
integrationgraph 显示Spring集成图。 Yes
loggers 显示和修改应用程序中日志记录器的配置。 Yes
liquibase 显示已应用的任何Liquibase数据库迁移。 Yes
metrics 显示当前应用程序的“指标”信息。 Yes
mappings 显示所有@RequestMapping路径的有序列表。 Yes
scheduledtasks 显示应用程序中的计划任务。 Yes
sessions 允许从Spring Session支持的会话存储中检索和删除用户会话。 使用Spring Session对响应式Web应用程序的支持时不可用。 Yes
shutdown 允许应用程序正常关闭。 No
threaddump 执行线程转储。 Yes

如果您的应用程序是Web应用程序(Spring MVC,Spring WebFlux或Jersey),则可以使用以下附加端点:

ID 描述 默认情况下是否启用
heapdump 返回一个hprof堆转储文件。 Yes
jolokia 通过HTTP公开JMX bean(当Jolokia在类路径上时,不适用于WebFlux)。 Yes
logfile 返回日志文件的内容(如果已设置logging.file或logging.path属性)。 支持使用HTTP Range标头来检索部分日志文件的内容。 Yes
prometheus 以Prometheus服务器可以抓取的格式公开指标。 Yes

默认情况下,大部分的端点是开启的,如果想要关闭,则可以通过:

management.endpoints.enabled-by-default=false

启用端点

要配置端点的启用,请使用management.endpoint.xxx.enabled属性。 以下示例启用关闭端点:

management.endpoint.shutdown.enabled=true

如果您希望端点启用是选择加入而不是选择退出,请将management.endpoints.enabled-by-default属性设置为false,并使用启用了各个端点的属性重新加入。以下示例启用info端点并禁用所有其他端点:

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

端点的默认暴露规则

由于端点可能包含敏感信息,因此应仔细考虑何时公开它们。 下表显示了内置端点的默认暴露规则:

ID JMX Web
auditevents Yes No
beans Yes No
caches Yes No
conditions Yes No
configprops Yes No
env Yes No
flyway Yes No
health Yes Yes
heapdump N/A No
httptrace Yes No
info Yes Yes
integrationgraph Yes No
jolokia N/A No
logfile N/A No
loggers Yes No
liquibase Yes No
metrics Yes No
mappings Yes No
prometheus N/A No
scheduledtasks Yes No
sessions Yes No
shutdown Yes No
threaddump Yes No

要更改公开的端点,请使用以下特定于技术的包含和排除属性:

Property Default
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include info, health

include属性列出暴露端点的IDS,exclude属性列出不应该暴露端点的IDS;exclude属性的优先级高于include属性,include和exclude属性都可以使用端点列表来配置IDS.

案例

案例1:停止公开所有在JMX上公开的端点,只公开info和health两个端点,使用如下属性:

management.endpoints.jmx.exposure.include=health,info

案例2:* 可以用来表示所有的端点,例如,通过HTTP公开所有的端点,除了env和beans端点,使用如下的属性:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

案例3:* 在YAML中有特殊的含义,所以如果想使用include或者exclude包含所有的端点时要加上双引号,如下示例:

management:
  endpoints:
    web:
      exposure:
        include: "*"

如果您希望在暴露端点时实施您自己的策略,您可以注册一个EndpointFilter bean。

自定义端点

Spring Boot支持自定义端点,只需要在我们定义的类中使用@Endpoint、@JmxEndpoint、@WebEndpoint等注解,实现对应的方法即可定义一个Actuator中的自定义端点。
从Spring Boot 2.x版本开始,Actuator支持CRUD(增删改查)模型,而不是旧的RW(读/写)模型。我们可以按照3种策略来自定义:

  • 使用@Endpoint注解,同时支持JMX和HTTP方式。
  • 使用@JmxEndpoint注解,只支持JMX技术。
  • 使用@WebEndpoint注解,只支持HTTP。

猜你喜欢

转载自blog.csdn.net/u014163312/article/details/129151837