# 07.SpirngBoot-指标控制
# 1.SpringBoot Actuator与Endpoint
官方文档 - Spring Boot Actuator: Production-ready Features (opens new window)
未来每一个微服务在云上部署以后,我们都需要对其进行监控、追踪、审计、控制等。SpringBoot就抽取了Actuator场景,使得我们每个微服务快速引用即可获得生产级别的应用监控、审计等功能。
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2
3
4
暴露所有监控信息为HTTP。
management:
endpoints:
enabled-by-default: true #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
2
3
4
5
6
测试例子 http://localhost:8080/actuator/beans http://localhost:8080/actuator/configprops http://localhost:8080/actuator/metrics http://localhost:8080/actuator/metrics/jvm.gc.pause http://localhost:8080/actuator/metrics/endpointName/detailPath
# 2.常使用的端点及开启与禁用
常使用的端点
| ID | 描述 | 依赖 / 前置条件 |
|---|---|---|
| auditevents | 暴露当前应用程序的审核事件信息。 | 需要 AuditEventRepository 组件 |
| beans | 显示应用程序中所有 Spring Bean 的完整列表。 | 无 |
| caches | 暴露可用的缓存。 | 无 |
| conditions | 显示自动配置的所有条件信息,包括匹配或不匹配的原因。 | 无 |
| configprops | 显示所有 @ConfigurationProperties 配置。 | 无 |
| env | 暴露 Spring 的 ConfigurableEnvironment 环境属性。 | 无 |
| flyway | 显示已应用的所有 Flyway 数据库迁移。 | 需要一个或多个 Flyway 组件 |
| health | 显示应用程序运行状况信息。 | 无(可自定义健康检查指标) |
| httptrace | 显示 HTTP 跟踪信息(默认最近 100 个请求 - 响应)。 | 需要 HttpTraceRepository 组件 |
| info | 显示自定义的应用程序信息(如版本、作者)。 | 无(需在配置文件中自定义 info 前缀配置) |
| integrationgraph | 显示 Spring Integration 拓扑图。 | 需要依赖 spring-integration-core |
| loggers | 显示和修改应用程序中日志的级别配置。 | 无 |
| liquibase | 显示已应用的所有 Liquibase 数据库迁移。 | 需要一个或多个 Liquibase 组件 |
| metrics | 显示当前应用程序的指标信息(如接口耗时、JVM 指标)。 | 无 |
| mappings | 显示所有 @RequestMapping 路径列表(接口路由映射)。 | 无 |
| scheduledtasks | 显示应用程序中的计划任务(如 @Scheduled 注解的任务)。 | 无 |
| sessions | 检索 / 删除 Spring Session 存储的用户会话。 | 仅适用于使用 Spring Session 的 Servlet 型 Web 应用 |
| shutdown | 使应用程序正常关闭。 | 默认禁用,需手动开启(management.endpoint.shutdown.enabled=true) |
| startup | 显示 ApplicationStartup 收集的启动步骤数据。 | 需要配置 BufferingApplicationStartup |
| threaddump | 执行线程转储,返回所有线程的状态信息。 | 无 |
如果您的应用程序是Web应用程序(Spring MVC,Spring WebFlux或Jersey),则可以使用以下附加端点:
| ID | 描述 | 依赖 / 前置条件 |
|---|---|---|
| heapdump | 返回 hprof 格式的堆转储文件(用于排查内存泄漏)。 | 仅适用于 Web 应用(Spring MVC/WebFlux/Jersey) |
| jolokia | 通过 HTTP 暴露 JMX Bean 信息。 | 需要引入 jolokia-core 依赖,不适用于 WebFlux 应用 |
| logfile | 返回日志文件内容(支持范围读取)。 | 需配置 logging.file.name 或 logging.file.path 属性 |
| prometheus | 以 Prometheus 可抓取的格式暴露指标。 | 需要引入 micrometer-registry-prometheus 依赖 |
其中最常用的Endpoint:
- Health:监控状况
- Metrics:运行时指标
- Loggers:日志记录
# Health Endpoint
健康检查端点,我们一般用于在云平台,平台会定时的检查应用的健康状况,我们就需要Health Endpoint可以为平台返回当前应用的一系列组件健康状况的集合。
重要的几点:
- health endpoint返回的结果,应该是一系列健康检查后的一个汇总报告。
- 很多的健康检查默认已经自动配置好了,比如:数据库、redis等。
- 可以很容易的添加自定义的健康检查机制。
# Metrics Endpoint
提供详细的、层级的、空间指标信息,这些信息可以被pull(被动获取)或者push(主动推送)方式得到:
- 通过Metrics对接多种监控系统。
- 简化核心Metrics开发。
- 添加自定义Metrics或者扩展已有Metrics。
# 开启与禁用Endpoints
- 默认所有的Endpoint除shutdown(禁用)都是开启的。
- 需要开启或者禁用某个Endpoint。配置模式为
management.endpoint.<endpointName>.enabled = true
出于安全考虑,我们可以禁用所有端点,开启需要开启的
management:
endpoints:
enabled-by-default: false #暴露所有端点信息
web:
exposure:
include: '*' #以web方式暴露
endpoint:
health:
enabled: true
show-details: always
info:
enabled: true
beans:
enabled: true
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 暴露Endpoints
支持的暴露方式
- HTTP:默认只暴露health和info。
- JMX:默认暴露所有Endpoint。(命令行输入jconsole打开)
# 3.定制Endpoint
# 1.定制Health
management:
health:
enabled: true
show-details: always #总是显示详细信息。可显示每个模块的状态信息
2
3
4
通过实现HealthIndicator 接口
@Component
public class myHealthIndicator implements HealthIndicator {
@Override
public Health health() {
if (true){
return Health.up().withDetail("success",200).build();
}
return Health.down().withDetail("error",500).build();
}
}
2
3
4
5
6
7
8
9
10
继承MyComHealthIndicator 类
@Component
public class myComHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
HashMap<String, Object> map = new HashMap<>();
if (true){
builder.status(Status.UP);
map.put("count",1);
map.put("info","ok");
}else {
builder.status(Status.DOWN);
map.put("count",0);
map.put("info","error");
}
builder.withDetail("code",200)
.withDetails(map);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 2.定制info
方式一:配置文件实现
management:
info:
env:
enabled: true
info:
appName: boot-admin
version: 1.0.0
mavenProjectName: @project.artifactId@
mavenProjectVersion: @project.version@
2
3
4
5
6
7
8
9
方式二:编写InfoContributor
@Component
public class myInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("company","boot")
.withDetail("author", Collections.singleton("spring"));
}
}
2
3
4
5
6
7
8
# 3.定制metrics
增加定制的metrics信息
class MyService{
Counter counter;
public MyService(MeterRegistry meterRegistry){
counter = meterRegistry.counter("myservice.method.running.counter");
}
public void hello() {
counter.increment();
}
}
2
3
4
5
6
7
8
9
10
11
# 4.定制endPoint
@Component
@Endpoint(id = "container")
public class DockerEndPoint {
@ReadOperation
public String getContainerInfo(){
return "this is a docker container";
}
@WriteOperation
public String setContainerInfo(String info){
return "set container info:"+info;
}
}
2
3
4
5
6
7
8
9
10
11
12
# 4.Spring Boot Admin (opens new window)
可视化指标监控