开启更多监控信息
如之前文章最终效果
默认支持的链接有:
/actuator
/actuator/health
/health/{component}/{instance}
/health/{component}
/actuator/info
可以在application.yml 配置文件中配置开启更多的监控信息:
#注意,不要使用 application.properties 配置文件,会报一些奇怪的错误(比如配置不生效)。
management:
endpoints:
web:
exposure:
# 显式配置不需要权限验证对外开放的端点
include: "*"
# Actuator 的 Web 访问方式的根地址为 /actuator,可以通过 management.endpoints.web.base-path 参数进行修改
# base-path: /monitor
# 禁用所有执行器端点
# enabled: false
endpoint:
health:
enabled: true
show-details: always
info:
enabled: true
# JSON 打印输出
spring:
jackson:
serialization:
indent_output: true
-
management.endpoints.web.exposure.include=’*’,代表开启全部监控,也可仅配置需要开启的监控,如: management.endpoints.web.exposure.include=beans,trace。
-
management.endpoint.health.show-details=always,health endpoint开启显示全部细节。默认情况下/actuator/health是公开的,但不显示细节。
-
management.endpoints.web.base-path=/monitor,启用指定的url地址访问根路径,默认路径为/actuator/,开启则访问路径变为/monitor/。
-
management.endpoint.shutdown.enabled=true,启用接口关闭SpringBoot。
监控信息如果需要跨越调用,可通过CORS配置来支持,默认处于禁用状态。设置management.endpoints.web.cors.allowed-origins属性后开启。
比如允许来自https://www.choupangxia.com 域的GET和POST调用:
management:
endpoints:
web:
cors:
allowed-origins: https://www.choupangxia.com
allowed-methods: GET,POST
经过以上步骤的操作,启动SpringBoot项目,可通过:http://localhost:8080/actuator 访问,结果如下:
会显示更多监控信息
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"beans": {
"href": "http://localhost:8080/actuator/beans",
"templated": false
},
"caches-cache": {
"href": "http://localhost:8080/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://localhost:8080/actuator/caches",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"health-path": {
"href": "http://localhost:8080/actuator/health/{*path}",
"templated": true
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
},
"conditions": {
"href": "http://localhost:8080/actuator/conditions",
"templated": false
},
"shutdown": {
"href": "http://localhost:8080/actuator/shutdown",
"templated": false
},
"configprops": {
"href": "http://localhost:8080/actuator/configprops",
"templated": false
},
"env": {
"href": "http://localhost:8080/actuator/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8080/actuator/env/{toMatch}",
"templated": true
},
"loggers-name": {
"href": "http://localhost:8080/actuator/loggers/{name}",
"templated": true
},
"loggers": {
"href": "http://localhost:8080/actuator/loggers",
"templated": false
},
"heapdump": {
"href": "http://localhost:8080/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8080/actuator/threaddump",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
"templated": true
},
"metrics": {
"href": "http://localhost:8080/actuator/metrics",
"templated": false
},
"scheduledtasks": {
"href": "http://localhost:8080/actuator/scheduledtasks",
"templated": false
},
"mappings": {
"href": "http://localhost:8080/actuator/mappings",
"templated": false
}
}
}
REST接口
Spring Boot Actuator 的关键特性是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况。Actuator 提供了 13 个接口,可以分为三大类:配置接口、度量接口和其它接口,具体如下表所示。
HTTP 方法 | 路径 | 描述 |
---|---|---|
GET | /autoconfig | 提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过 |
GET | /configprops | 描述配置属性(包含默认值)如何注入Bean |
GET | /beans | 描述应用程序上下文里全部的Bean,以及它们的关系 |
GET | /dump | 获取线程活动的快照 |
GET | /env | 获取全部环境属性 |
GET | /env/{name} | 根据名称获取特定的环境属性值 |
GET | /health | 报告应用程序的健康指标,这些值由HealthIndicator的实现类提供 |
GET | /info | 获取应用程序的定制信息,这些信息由info打头的属性提供 |
GET | /mappings | 描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系 |
GET | /metrics | 报告各种应用程序度量信息,比如内存用量和HTTP请求计数 |
GET | /metrics/{name} | 报告指定名称的应用程序度量值 |
POST | /shutdown | 关闭应用程序,要求endpoints.shutdown.enabled设置为true |
GET | /trace | 提供基本的HTTP请求跟踪信息(时间戳、HTTP头等) |
endpoint 可以理解为被管理(或被监控)对象,actuator 就是通过这些 endpoint 来实现对应用程序的监控管理。
spring 提供了大量的内置 endpoint,比如 health,beans,mappings,endpoint 名称也称为 endpoint id:
原生端点
原生端点分为三大类:
- 应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring Boot应用密切相关的配置类信息。
- 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如:内存信息、线程池信息、HTTP请求统计等。
- 操作控制类:提供了对应用的关闭等操作类功能。
定制 Actuator
虽然Actuator提供了很多运行中Spring Boot应用程序的内部工作细节,但难免和你的需求有所偏差。也许你并不需要它提供的所有功能,想要关闭一些也说不定。或者,你需要对Actuator 稍作扩展,增加一些自定义的度量信息,以满足你对应用程序的需求。
- 修改接口 ID
每个Actuator 接口都有一个ID用来决定接口的路径,比方说,/beans接口的默认ID就是beans。比如要修改 /beans 为 /instances,则设置如下:
endpoints.beans.id = instances
- 启用和禁用接口
虽然Actuator的接口都很有用,但你不一定需要全部这些接口。默认情况下,所有接口(除 了/shutdown)都启用。比如要禁用 /metrics 接口,则可以设置如下:
endpoints.metrics.enabled = false
如果你只想打开一两个接口,那就先禁用全部接口,然后启用那几个你要的,这样更方便。
endpoints.enabled = false
endpoints.metrics.enabled = true
- 添加自定义度量信息
Actuator 自动配置有两个实例 CounterService 和 GaugeService 可以用来计数使用,我们所要做的就是把它们的实例注入所需的 bean 然后调用相应的方法。除此之外,我们还可以实现 PublicMetrics 接口,提供自己需要的度量信息。 - 创建自定义跟踪仓库
默认情况下,/trace 接口报告的跟踪信息都存储在内存仓库里,100个条目封顶。一旦仓库满了,就开始移除老的条目,给新的条目腾出空间。在开发阶段这没什么问题,但在生产环境中,大流量会造成跟踪信息还没来得及看就被丢弃。我们可以将那些跟踪条目存储在其他地方——既不消耗内存,又能长久保存的地方。只需实现Spring Boot的TraceRepository接口即可。 - 插入自定义的健康指示器
实现 HealthIndicator 接口则可以实现自定义的健康指示器。 - 保护 Actuator 接口
很多Actuator端点发布的信息都可能涉及敏感数据,还有一些端点,(比如/shutdown)非常危险,可以用来关闭应用程序。因此,保护这些端点尤为重要,能访问它们的只能是那些经过授权的客户端。