摘要:无。
环境
Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18
1 概述
Spring Boot Actuator是Spring Boot提供的监控和管理模块,用于对应用系统进行实时监控和管理。
通过引入Actuator依赖,可以获得一系列开箱即用的监控端点,了解应用的运行状态、健康状况、配置信息等。
官方文档:https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/actuator.html
1.1 核心能力
- 健康检查:检查应用是否正常运行,包括数据库连接、磁盘空间等。
- 应用信息:展示应用的基本信息,如版本号、构建信息等。
- 日志管理:查看和修改应用的日志级别。
- 指标监控:收集应用的性能指标,如JVM内存、HTTP请求统计等。
- 线程快照:获取当前线程的快照信息,用于排查线程问题。
1.2 工作原理
Actuator基于@Endpoint注解实现端点的自动发现和注册。每个端点对应一个Bean对象,通过注解定义请求的处理方法。
端点默认通过HTTP协议暴露为REST接口,也可以通过JMX协议暴露。
2 使用
2.1 项目结构
项目结构:
code1 2 3 4 5 6 7 8 9 10 11
| demo-actuator ├── src/ │ ├── main/ │ │ ├── java/ │ │ │ └── com/ │ │ │ └── example/ │ │ │ └── DemoApplication.java │ │ └── resources/ │ │ └── application.yml │ └── test/ └── pom.xml
|
2.2 配置依赖
配置依赖:
pom.xml1 2 3 4 5 6 7 8 9 10
| <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>
|
2.3 配置文件
启动应用后,访问http://localhost:8080/actuator可以查看默认暴露的端点列表:
json1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| { "_links": { "self": { "href": "http://localhost:8080/actuator", "templated": false }, "health": { "href": "http://localhost:8080/actuator/health", "templated": false }, "health-path": { "href": "http://localhost:8080/actuator/health/{*path}", "templated": true } } }
|
修改配置:
application.yml1 2 3 4 5 6 7 8 9 10 11 12 13
| management: server: port: 8081 endpoints: web: base-path: /actuator exposure: include: "*" exclude: "env,loggers"
|
常用端点路径:
- **
/actuator**:端点列表,返回所有已暴露端点的链接。
- **
/actuator/beans**:Bean列表,对应BeansEndpoint类。
- **
/actuator/health**:健康检查,对应HealthEndpoint类。
- **
/actuator/info**:应用信息,对应InfoEndpoint类。
- **
/actuator/configprops**:配置详情,对应ConfigurationPropertiesReportEndpoint类。
- **
/actuator/env**:环境变量,对应EnvironmentEndpoint类。
- **
/actuator/loggers**:日志级别,对应LoggersEndpoint类。
- **
/actuator/heapdump**:堆快照,对应HeapDumpWebEndpoint类。
- **
/actuator/threaddump**:线程快照,对应ThreadDumpEndpoint类。
- **
/actuator/metrics**:应用指标,对应MetricsEndpoint类。
2.4 端点控制
2.4.1 暴露端点
2.4.1.1 HTTP协议
通过HTTP协议暴露端点:
application.yml1 2 3 4 5 6
| management: endpoints: web: exposure: include: "*" exclude: "env,health"
|
2.4.1.2 JMX协议
通过JMX协议暴露端点:
application.yml1 2 3 4 5 6
| management: endpoints: jmx: exposure: include: "*" exclude: "env,health"
|
2.4.2 启用端点
默认启用除shutdown以外的端点。
只有启用端点之后,才能暴露端点,进而访问端点。
启用所有端点:
application.yml1 2 3
| management: endpoints: enabled-by-default: true
|
启用指定端点:
application.yml1 2 3 4
| management: endpoint: shutdown: enabled: true
|
手动禁用所有端点,启用指定端点:
application.yml1 2 3 4 5 6
| management: endpoints: enabled-by-default: false endpoint: health: enabled: true
|
3 内置端点
3.1 健康检查
health端点用于检查应用的健康状态。
配置健康端点:
application.yml1 2 3 4 5
| management: endpoint: health: show-details: always
|
访问/actuator/health查看应用健康信息:
json1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| { "status": "UP", "components": { "diskSpace": { "status": "UP", "details": { "total": 350717689856, "free": 98274390016, "threshold": 10485760, "exists": true } }, "ping": { "status": "UP" } } }
|
健康状态值:
- **
UP**:组件正常运行。
- **
DOWN**:组件运行异常。
- **
OUT_OF_SERVICE**:组件不可用。
- **
UNKNOWN**:组件状态未知。
支持通过实现HealthIndicator接口来自定义健康检查逻辑:
java1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Component public class DemoHealthIndicator implements HealthIndicator { @Override public Health health() { Health.Builder builder = new Health.Builder(); if (true) { builder.status(Status.UP); } else { builder.status(Status.DOWN); } builder.withDetail("ms", "100"); return builder.build(); } }
|
3.2 应用信息
info端点用于展示应用的自定义信息。
支持以下信息:
- build:构建信息,使用
META-INF/build-info.properties文件作为前提。
- git:Git信息,使用
git.properties文件作为前提。
- env:环境变量信息,无前提条件,默认禁用。
- java:Java信息,无前提条件,默认禁用。
- os:操作系统信息,无前提条件,默认禁用。
配置文件:
application.yml1 2 3 4 5 6 7 8 9 10 11 12
| management: info: env: enabled: true
info: app: name: demo-spring-boot version: 1.0.0 description: Spring Boot Actuator encoding: "@project.build.sourceEncoding@"
|
访问/actuator/info查看应用信息:
json1 2 3 4 5 6 7 8
| { "app": { "name": "demo-spring-boot", "version": "1.0.0", "description": "Spring Boot Actuator", "encoding": "UTF-8" } }
|
支持通过实现InfoContributor接口来自定义应用信息:
java1 2 3 4 5 6 7
| @Component public class DemoInfoContributor implements InfoContributor { @Override public void contribute(Info.Builder builder) { builder.withDetail("demo", "info"); } }
|
3.3 应用指标
metrics端点用于收集应用运行时的指标数据。
访问/actuator/metrics,查看可用的指标名称列表:
json1 2 3 4 5 6 7 8 9 10
| { "names": [ "jvm.memory.used", "jvm.memory.max", "system.cpu.count", "system.cpu.usage", "jvm.threads.live", "http.server.requests" ] }
|
访问/actuator/metrics/{name}查看具体指标。
4 自定义端点
使用@Endpoint注解创建自定义端点,id属性指定端点路径。
需要在配置类中将自定义端点注册为Bean对象:
java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Component @Endpoint(id = "demo") public class DemoEndpoint { @ReadOperation public Map<String, Object> read(@Selector String key) { Map<String, Object> result = new HashMap<>(); result.put("name", "demo-spring-boot"); result.put("version", "1.0.0"); result.put("key", key); return result; }
@WriteOperation public void write(@Selector String key, String value) { System.out.println(key + "=" + value); }
@DeleteOperation public void delete(@Selector String key) { System.out.println("key=" + key); } }
|
注解说明:
- **
@ReadOperation**:处理GET请求,用于读取数据。
- **
@WriteOperation**:处理POST请求,用于写入数据。
- **
@DeleteOperation**:处理DELETE请求,用于删除数据。
- **
@Selector**:用于标注路径参数。
访问端点:
- **GET /actuator/demo/{key}**:读取数据。
- **POST /actuator/demo/{key} value={value}**:写入数据。
- **DELETE /actuator/demo/{key}**:删除数据。
条