Spring Boot「13」使用 Actuator

228 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第13天,点击查看活动详情

01-简介

Actuator 是 Spring Boot 中的一个监控工具,它提供了以下功能:

  • 监控应用
  • 收集 metrics
  • 分析流量
  • 监控数据库状态等

因此,Actuator 主要用于暴露正在运行的应用的各种信息,例如应用健康状态、各类指标、日志、转储、环境等其他信息; 而且 Actuator 提供了基于 HTTP API 或 JMX Bean 的交互方式,开发者可以非常容易的获取这些信息。

2.x 版本与 1.x 版本的 API 是不兼容的,而且 2.x 是依赖特定技术的,而 1.x 版本是依赖 MVC 或者说 Servlet API 的。 因此,2.x 就更容易扩展,使用其他技术扩展(例如 WebFlux)时,仅需要按照要求实现特定的适配器即可。

2.x 版本默认情况下大多数 API EndPoint 是不开启的,仅有 /health 和 /info 两个默认开启。 如果要开启其他的 endpoint,可以通过 property 来指定,management.endpoints.web.exposure.include=info,env,beans 默认情况下,endpoint 都在 /actuator 路径下,也可以通过 property 改变这个路径:management.endpoints.web.base-path 上述 property 的配置方式可以在 application.properties 文件中,也可在命令行中添加--management.endpoints.web.exposure.include=info,env,beans

直接访问 http://localhost:8080/actuator 可以得到当前包含的所有有效 endpoint,例如:

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
      "templated": false
    },
    "beans": {
      "href": "http://localhost:8080/actuator/beans",
      "templated": false
    },
    "info": {
      "href": "http://localhost:8080/actuator/info",
      "templated": false
    },
    "env-toMatch": {
      "href": "http://localhost:8080/actuator/env/{toMatch}",
      "templated": true
    },
    "env": {
      "href": "http://localhost:8080/actuator/env",
      "templated": false
    }
  }
}

如何开启所有的 Endpoint? management.endpoints.web.exposure.include=*可以开启所有的 Actuator endpoint。 可以搭配management.endpoints.web.exposure.exclude=threaddump来排除某些 endpoint。 而且,exclude 配置的优先级要比 include 的优先级高。

02-特殊的 endpoint

有些 endpoint 单纯的通过上述配置激活并不生效,例如 /startup 用来记录应用启动过程中的事件; /shutdown 用来停止应用; 下面,我们来逐个分析下这两个特殊的 endpoint。

首先,我们来分析 /startup endpoint。 除了上节中需要通过 include 将 /startup 激活外,为了监控(或监听)系统启动过程中的事件,应用中需要额外配置一个 ApplicationStartup 接口的实现。 Spring Boot 提供了一个基于内存的实现 BufferingApplicationStartup,它能够缓存一定数量的事件,如下所示(最多缓存2048个事件):

SpringApplication application = new SpringApplication(ListAllManagedBeanApplication.class);
application.setApplicationStartup(new BufferingApplicationStartup(2048));

final ConfigurableApplicationContext ctx = application.run(args);

之后,我们可以运行应用并访问curl http://localhost:8080/actuator/startup -X GET | jq,会得到如下输出:

{
  "springBootVersion": "2.7.4",
  "timeline": {
    "startTime": "2022-10-24T03:24:38.595661600Z",
    "events": [
      {
        "endTime": "2022-10-24T03:24:38.626657600Z",
        "duration": "PT0.0189966S",
        "startTime": "2022-10-24T03:24:38.607661Z",
        "startupStep": {
          "name": "spring.boot.application.starting",
          "id": 0,
          "tags": [
            {
              "key": "mainApplicationClass",
              "value": "self.samson.example.core.ListAllManagedBeanApplication"
            }
          ],
          "parentId": null
        }
      }, { ... }
    ]
  }
}

如果我们用-X POST的方式访问,缓存的事件会被清空,再次访问只可以得到一个空列表。

因为 BufferingApplicationStartup 是有容量的,容量满后便不会再存储新的事件了。 所以,我们有必要对事件进行过滤,来忽略不关心的事件,例如我们只关心 Bean 实例化事件(关于所有事件可以参考官方文档1):

final BufferingApplicationStartup startup = new BufferingApplicationStartup(2048);
startup.addFilter(step -> step.getName().contains("spring.beans.instantiate"));

接下来,我们将分析 /shutdown endpoint。 /shutdown endpoint 需要通过management.endpoints.enabled-by-default=true来开启。 开启过后,我们需要通过curl http://localhost:8080/actuator/shutdown -X POST |jq 访问,得到的结果如下:

{
  "message": "Shutting down, bye..."
}

这里如果我们不使用-X POST,则会得到如下错误提示:

{
  "timestamp": "2022-10-24T03:36:42.350+00:00",
  "status": 405,
  "error": "Method Not Allowed",
  "path": "/actuator/shutdown"
}

注:上述这两个 endpoint 其实并不推荐在生产环境使用。特别是第一个,在开发、测试阶段是比较有用。

03-总结

今天,我们首先简单介绍了 Spring Boot 中的 Actuator 及开启它们的方式。 接下来,我们详细介绍了两个特殊的 Actuator 及开启它们的配置。 Actuator 是 Spring Boot 提供的一种开箱即用的监控工具,帮助开发者更好地了解线上的进程,也可帮助开发者