Springboot 集成 Actuator 使用示例

本文最后更新于: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:
# 暴露 EndPoint 以供访问,有jmx和web两种方式,exclude 的优先级高于 include
jmx: # 供JMX使用的
exposure:
exclude: '*' # 除非使用,否则就别开启
include: '*' # 除非使用,否则就别开启
web: # 供web使用的
exposure:
# exclude: '*'
#include: ["health","info","beans","mappings","logfile","metrics","shutdown","env","prometheus"]
include: # 正常开启这几个即可,不要全开,不然容易被攻击注入挖矿程序
- health
- metrics
- info
- prometheus # 这是供普罗米修斯监控软件使用的
base-path: /actuator # 配置 Endpoint 的基础路径
cors: # 配置跨域资源共享
allowed-origins: http://example.com
allowed-methods: GET,POST
enabled-by-default: true # 修改全局 endpoint 默认设置
endpoint:
auditevents: # 1、显示当前引用程序的审计事件信息,默认开启
enabled: true
cache:
time-to-live: 10s # 配置端点缓存响应的时间
beans: # 2、显示一个应用中所有 Spring Beans 的完整列表,默认开启
enabled: true
conditions: # 3、显示配置类和自动配置类的状态及它们被应用和未被应用的原因,默认开启
enabled: true
configprops: # 4、显示一个所有@ConfigurationProperties的集合列表,默认开启
enabled: true
env: # 5、显示来自Spring的 ConfigurableEnvironment的属性,默认开启
enabled: true
flyway: # 6、显示数据库迁移路径,如果有的话,默认开启
enabled: true
health: # 7、显示健康信息,默认开启
enabled: true
show-details: always # 显示详细信息,例如宕机原因
info: # 8、显示任意的应用信息,默认开启
enabled: true
liquibase: # 9、展示任何Liquibase数据库迁移路径,如果有的话,默认开启
enabled: true
metrics: # 10、展示当前应用的metrics信息,默认开启
enabled: true
mappings: # 11、显示一个所有@RequestMapping路径的集合列表,默认开启
enabled: true
scheduledtasks: # 12、显示应用程序中的计划任务,默认开启
enabled: true
sessions: # 13、允许从Spring会话支持的会话存储中检索和删除(retrieval and deletion)用户会话。使用Spring Session对反应性Web应用程序的支持时不可用。默认开启。
enabled: true
shutdown: # 14、允许应用以优雅的方式关闭,默认关闭
enabled: true
threaddump: # 15、执行一个线程dump
enabled: true
# web 应用时可以使用以下端点
heapdump: # 16、 返回一个GZip压缩的hprof堆dump文件,默认开启
enabled: true
jolokia: # 17、通过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用),默认开启
enabled: true
logfile: # 18、返回日志文件内容(如果设置了logging.file或logging.path属性的话),支持使用HTTP Range头接收日志文件内容的部分信息,默认开启
enabled: true
prometheus: #19、以可以被Prometheus服务器抓取的格式显示metrics信息,默认开启
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;

/**
* 自定义服务器监控信息 >>> Actuator
* @author peter
* @date 2024/11/12
* @className CustomInfoContributor
* @packageName org.example.config
*/
@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", "这是一段描述。");

// 将 Map 转为不可修改的 Map
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:已用内存,按区域(如 heapnon-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 暂停时间)。

Springboot 集成 Actuator 使用示例
https://superlovelace.top/2024/11/13/Actuator服务监控/
作者
棱境
发布于
2024年11月13日
更新于
2025年2月18日
许可协议