Skip to content

Spring Boot Actuator Project Runtime Monitoring

After the completion of our project development, it will eventually need to be deployed and run online. During the project's operation, we may need to monitor it in order to observe its running status in real-time and make corresponding adjustments when issues arise. Therefore, integrating project runtime monitoring is essential.

我们的项目开发完成之后,肯定是需要上线运行的,不过在项目的运行过程中,我们可能需要对其进行监控,从而实时观察其运行状态,并在发生问题时做出对应调整,因此,集成项目运行监控就很有必要了。

The Spring Boot framework provides the spring-boot-starter-actuator module to achieve this monitoring functionality:

SpringBoot 框架提供了spring-boot-starter-actuator模块来实现监控效果:

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

Once added, Actuator will automatically register several endpoints that can be used to check the current status of the Spring Boot application. You can find the official documentation here: Spring Boot Actuator API Documentation

添加好之后,Actuator 会自动注册一些接口用于查询当前 SpringBoot 应用程序的状态,官方文档如下:https://docs.spring.io/spring-boot/docs/3.1.1/actuator-api/htmlsingle/#overview

Screenshot 2024-06-12 at 23.13.13

By default, all Actuator endpoints are registered with the path format /actuator/{id} (this can be modified in the configuration file). For example, if we want to check the current health status of the server, we can access this endpoint: http://localhost:8080/actuator/health. The result will be returned to us in JSON format.

默认情况下,所有 Actuator 自动注册的接口路径都是/actuator/{id}格式的(可在配置文件中修改),比如我们想要查询当前服务器的健康状态,就可以访问这个接口:http://localhost:8080/actuator/health,结果会以 JSON 格式返回给我们:

image-20230716205752392

Screenshot 2024-06-12 at 23.13.42

By directly accessing the root path http://localhost:8080/actuator, you can view all the currently enabled endpoints. By default, only the following endpoints are enabled:

直接访问:http://localhost:8080/actuator 根路径,可以查看当前已经开启的所有接口,默认情况下只开启以下接口:

{
  "_links": {
    "self": { "href": "http://localhost:8080/actuator", "templated": false }, //actuator自己的信息
    "health-path": {
      "href": "http://localhost:8080/actuator/health/{*path}",
      "templated": true
    },
    "health": {
      "href": "http://localhost:8080/actuator/health",
      "templated": false
    } //应用程序健康情况监控
  }
}

Screenshot 2024-06-12 at 23.13.56

We can modify the configuration file to expose all the endpoints:

我们可以来修改一下配置文件,让其暴露全部接口:

management:
  endpoints:
    web:
      exposure:
        include: "*" #使用*表示暴露全部接口  using `*` to indicate that all endpoints should be exposed

Screenshot 2024-06-12 at 23.15.12

Screenshot 2024-06-12 at 23.16.13

After restarting the server, you can retrieve the available endpoints again and see all the information. Here, we won't list all of them but will highlight some of the commonly used ones:

重启服务器,再次获取可用接口就可以看到全部的信息了,这里就不全部搬出来了,只列举一些常用的:

{
  "_links": {
    //包含Actuator自己的信息 // Contains Actuator's own information
    "self": {"href":"http://localhost:8080/actuator","templated":false},
    //已注册的Bean信息 // Information about registered Beans
    "beans":{"href":"http://localhost:8080/actuator/beans","templated":false},
    //应用程序健康情况监控 // Application health status monitoring
    "health":{"href":"http://localhost:8080/actuator/health","templated":false},
    "health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},
    //应用程序运行信息 // Application runtime information
    "info":{"href":"http://localhost:8080/actuator/info","templated":false},
    //系统环境相关信息 // Information related to the system environment
    "env": {"href":"http://localhost:8080/actuator/env","templated":false},
    "env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},
    //日志相关信息 // Information related to logging
    "loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},
    "loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},
    //快速获取JVM堆转储文件 // Quick access to JVM heap dump files
    "heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},
    //快速获取JVM线程转储信息 // Quick access to JVM thread dump information
    "threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},
    //计划任务相关信息 // Information related to scheduled tasks
    "scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},
    //请求映射相关信息 // Information related to request mappings
    "mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false},
    ...
  }
}

For example, we can use the http://localhost:8080/actuator/info endpoint to view the current system runtime environment information.

比如我们可以通过 http://localhost:8080/actuator/info 接口查看当前系统运行环境信息:

image-20230716211517338

Screenshot 2024-06-12 at 23.16.31

We notice that the data retrieved here is empty. This is because we still need to enable the corresponding module separately to access this information.

我们发现,这里得到的数据是一个空的,这是因为我们还需要单独开启对应模块才可以:

management:
  endpoints:
    web:
      exposure:
        include: "*"
  #开启某些默认为false的信息
  info:
    env:
      enabled: true
    os:
      enabled: true
    java:
      enabled: true

After enabling the module and making the request again, you will be able to obtain runtime environment information, such as the Java version, JVM details, operating system information, and more.

再次请求,就能获得运行环境相关信息了,比如这里的 Java 版本、JVM 信息、操作系统信息等:

image-20230716211648263

Screenshot 2024-06-12 at 23.17.15

We can also make the health endpoint display more detailed system status information. Let's enable the following configuration:

我们也可以让 health 显示更加详细的系统状态信息,这里我们开启一下配置:

management:
    ...
  endpoint:
    health:
      show-details: always  #展示详细内容
    env:
      show-values: always  #总是直接展示值

Now, you can view detailed information about the current system usage, such as disk space, database status, and more.

现在就能查看当前系统占用相关信息了,比如下面的磁盘占用、数据库等信息:

image-20230716212238191

包括完整的系统环境信息,比如我们配置的服务器 8080 端口:

image-20230716212456642

By using these endpoints, we can quickly obtain the current runtime information of the application.

For more advanced analysis, features like thread dump and heap memory dump file generation are available, which are useful for analyzing the running state of a Java application. For example, we can obtain a heap memory dump file by accessing http://localhost:8080/actuator/heapdump. Once the file is downloaded, you can open it directly with IntelliJ IDEA for further analysis.

我们只需要通过这些接口就能快速获取到当前应用程序的运行信息了。

高级一点的还有线程转储和堆内存转储文件直接生成,便于我们对 Java 程序的运行情况进行分析,这里我们获取一下堆内存转储文件:http://localhost:8080/actuator/heapdump,文件下载之后直接使用 IDEA 就能打开:

image-20230716212801376

Screenshot 2024-06-12 at 23.18.41

We can see that the number of created byte[] array objects has reached 72,020, while our own TestController object has only one instance.

可以看到其中创建的 byte 数组对象计数达到了 72020 个,其中我们自己的 TestController 对象只有有一个:

image-20230716212920673

Additionally, you can obtain the corresponding thread dump information by accessing http://localhost:8080/actuator/threaddump directly.

以及对应的线程转储信息,也可以通过 http://localhost:8080/actuator/threaddump 直接获取:

image-20230716214134109