本文最后更新于:2025年2月18日 下午
Actuator >> 服务监控
SpringBoot 2.7.18 - Actuator官方文档地址:
https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/actuator.html
Maven依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
|
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| management: endpoints: jmx: exposure: exclude: '*' include: '*' web: exposure: include: - health - metrics - info - prometheus base-path: /actuator cors: allowed-origins: http://example.com allowed-methods: GET,POST enabled-by-default: true endpoint: auditevents: enabled: true cache: time-to-live: 10s beans: enabled: true conditions: enabled: true configprops: enabled: true env: enabled: true flyway: enabled: true health: enabled: true show-details: always info: enabled: true liquibase: enabled: true metrics: enabled: true mappings: enabled: true scheduledtasks: enabled: true sessions: enabled: true shutdown: enabled: true threaddump: enabled: true heapdump: enabled: true jolokia: enabled: true logfile: enabled: true prometheus: enabled: true
|
配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package org.example.config;
import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.stereotype.Component;
import java.util.Collections; import java.util.HashMap; import java.util.Map;
@Component public class CustomInfoContributor implements InfoContributor {
@Override public void contribute(Info.Builder builder) { Map<String, String> infoDetails = new HashMap<>(); infoDetails.put("name", "MyApplication"); infoDetails.put("version", "1.0.0"); infoDetails.put("description", "这是一段描述。");
infoDetails = Collections.unmodifiableMap(infoDetails);
builder.withDetail("app", infoDetails); } }
|
使用
默认接口为 /actuator
,其中包含了所有已开放的可供访问的接口
Spring Boot Actuator 的 /actuator/metrics
端点提供了多种系统和 JVM 指标数据。以下是一些关键的指标:
- CPU:可以查看当前 CPU 使用情况,例如:
system.cpu.usage
:系统的整体 CPU 使用率(0 到 1 之间的小数)。
process.cpu.usage
:当前 Java 进程的 CPU 使用率。
- 内存:提供堆内存和非堆内存的使用情况。
jvm.memory.used
:已用内存,按区域(如 heap
和 non-heap
)划分。
jvm.memory.max
:最大内存。
jvm.memory.committed
:已分配给 JVM 的内存量。
- 磁盘:磁盘存储信息。
disk.total
:总磁盘空间。
disk.free
:剩余磁盘空间。
disk.usable
:可用磁盘空间。
- Java 虚拟机:可以监控 JVM 的线程、垃圾回收等。
jvm.threads.live
:当前活动线程数。
jvm.threads.peak
:线程的峰值数量。
jvm.gc.*
:垃圾回收统计,如 jvm.gc.memory.allocated
(分配的内存量)、jvm.gc.pause
(GC 暂停时间)。