端点
原文:https://docs.spring.io/spring-boot/reference/actuator/endpoints.html (Spring Boot 4.2.0)
Actuator 端点让你可以监控和与应用交互。Spring Boot 包含许多内置端点,并允许添加自己的端点。例如,health 端点提供基本的应用健康信息。
可以控制对每个端点的访问,并通过 HTTP 或 JMX 暴露它们(使其可远程访问)。当允许访问且已暴露时,端点被认为是可用的。内置端点仅在可用时才自动配置。大多数应用选择通过 HTTP 暴露,端点的 ID 加上 /actuator 前缀映射到 URL。例如,默认情况下 health 端点映射到 /actuator/health。
要了解 Actuator 端点及其请求和响应格式的更多信息,请参阅 API 文档。
以下是与技术无关的端点:
| ID | 描述 |
|---|---|
auditevents | 暴露当前应用的审计事件信息。需要 AuditEventRepository bean。 |
beans | 显示应用中所有 Spring bean 的完整列表。 |
caches | 暴露可用的缓存。 |
conditions | 显示配置和自动配置类上评估的条件及其匹配或不匹配的原因。 |
configprops | 显示所有 @ConfigurationProperties 的整理列表。经过敏感值清理。 |
env | 暴露 Spring ConfigurableEnvironment 中的属性。经过敏感值清理。 |
flyway | 显示已应用的 Flyway 数据库迁移。需要 Flyway bean。 |
health | 显示应用健康信息。 |
httpexchanges | 显示 HTTP 交换信息(默认最后 100 个请求-响应交换)。需要 HttpExchangeRepository bean。 |
info | 显示任意应用信息。 |
integrationgraph | 显示 Spring Integration 图。需要 spring-integration-core 依赖。 |
loggers | 显示和修改应用中 logger 的配置。 |
liquibase | 显示已应用的 Liquibase 数据库迁移。需要 Liquibase bean。 |
metrics | 显示当前应用的指标信息以诊断应用记录的指标。 |
mappings | 显示所有 @RequestMapping 路径的整理列表。 |
scheduledtasks | 显示应用中的计划任务。 |
sessions | 显示 HTTP 会话(需要 Spring Session)。 |
shutdown | 允许应用优雅关闭(默认禁用)。 |
startup | 显示应用启动步骤信息。 |
threaddump | 执行线程转储。 |
heapdump | 执行堆转储(需要 HotSpot)。 |
还有 web 应用专属端点,health、info、metrics 等。
控制端点访问
可以使用 management.endpoint.<id>.enabled 属性控制单个端点的启用。例如,禁用 shutdown 端点:
management.endpoint.shutdown.enabled=false
限制访问
大多数端点应仅对授权用户可用。可以结合 Spring Security 使用 management.endpoints.web.exposure 和 Spring Security 配置限制访问。
暴露端点
默认情况下,只有 health 端点通过 HTTP 和 JMX 暴露。由于端点可能包含敏感信息,应仔细考虑何时暴露它们。
要更改暴露的端点,使用以下技术特定的 include 和 exclude 属性:
| 属性 | 默认值 |
|---|---|
management.endpoints.jmx.exposure.exclude | |
management.endpoints.jmx.exposure.include | health |
management.endpoints.web.exposure.exclude | |
management.endpoints.web.exposure.include | health |
include 属性列出要暴露的端点 ID,exclude 属性列出不应暴露的端点 ID。exclude 优先于 include。例如,仅暴露 health 和 info:
management.endpoints.web.exposure.include=health,info
使用 * 暴露所有端点(注意安全风险):
management.endpoints.web.exposure.include=*
安全
如果 Spring Security 在 classpath 上,Actuator 的 Web 端点默认受到保护。可以通过配置 SecurityFilterChain bean 定制安全规则。要允许匿名访问某些端点,可以配置 permitAll()。
如果希望允许对所有端点进行无认证的 HTTP 访问,可以设置
management.endpoints.web.exposure.include=*并配置 Spring Security 允许访问,但强烈建议不要在生产环境中这样做。
跨站请求伪造保护
Actuator 端点默认不提供 CSRF 保护。如果启用了 Spring Security 的 CSRF 保护,向 actuator 端点发送非 GET 请求时可能会被拒绝。可以通过配置排除 actuator 端点来禁用 CSRF 保护。
配置端点
可以使用 management.endpoint.<id>.* 属性配置单个端点,例如 management.endpoint.health.show-details=always。
敏感值清理
configprops 和 env 端点的值在显示前会被清理,以避免泄露敏感信息(如密码、密钥)。清理规则由 management.endpoint.configprops.keys-to-sanitize 和 management.endpoint.env.keys-to-sanitize 属性控制。可以通过 management.endpoint.<id>.show-values 控制是否显示值。
Actuator Web 端点的超媒体
Actuator 为 HTTP 端点提供超媒体支持,端点响应中包含 _links 字段,便于发现其他端点。
CORS 支持
可以通过 management.endpoints.web.cors.* 属性为 Actuator Web 端点配置 CORS:
management.endpoints.web.cors.allowed-origins=https://example.com management.endpoints.web.cors.allowed-methods=GET,POST
JSON
Actuator 端点的响应使用 Jackson 序列化为 JSON。
实现自定义端点
可以通过使用 @Endpoint 注解类并暴露操作来添加自定义端点:
@Component @Endpoint(id = "myendpoint") public class MyEndpoint { @ReadOperation public String get() { return "Hello from custom endpoint"; } }
@ReadOperation、@WriteOperation 和 @DeleteOperation 分别对应 GET、POST 和 DELETE 操作。
接收输入
写操作可以通过 @Selector 注解的参数接收输入,或使用 @WriteOperation 方法的参数接收 JSON 请求体。
自定义 Web 端点
可以通过使用 @WebEndpoint 或 @EndpointWebExtension 创建自定义 Web 端点,或使用 @RestControllerEndpoint 将现有 controller 暴露为端点。
健康信息
可以使用健康信息检查运行中应用的状态。health 端点暴露的信息取决于 management.endpoint.health.show-details 和 management.endpoint.health.show-components 属性:
| 值 | 描述 |
|---|---|
never | 从不显示详情。 |
when-authorized | 仅向授权用户显示详情。可通过 management.endpoint.health.roles 配置角色。 |
always | 向所有用户显示详情。 |
默认值为 never。健康指示器返回 Status 对象表示应用状态,常见状态有 UP、DOWN、OUT_OF_SERVICE、UNKNOWN。
自动配置的健康指示器
Spring Boot 自动配置多个健康指示器,包括 DiskSpaceHealthIndicator、DataSourceHealthIndicator、PingHealthIndicator 等。可以通过 management.health.<name>.enabled 属性禁用特定指示器。
编写自定义健康指示器
可以实现 HealthIndicator 接口创建自定义健康指示器:
@Component public class MyHealthIndicator implements HealthIndicator { @Override public Health health() { int errorCode = check(); if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } }
响应式健康指示器
对于响应式应用,可以实现 ReactiveHealthIndicator 返回 Mono<Health>。
健康组
可以通过 management.endpoint.health.group.<name>.include 属性定义健康组,将多个指示器分组。例如定义 custom 组:
management.endpoint.health.group.custom.include=ping,db
DataSource 健康
DataSourceHealthIndicator 会验证与数据源的连接。
Kubernetes 探针
Spring Boot 管理 health 端点暴露的两个 Kubernetes 探针组:liveness 和 readiness。它们可以通过 management.endpoint.health.group.liveness 和 management.endpoint.health.group.readiness 配置。探针状态与应用的可用性状态(LivenessState 和 ReadinessState)对应。
使用 Kubernetes 探针检查外部状态
可以配置健康组包含外部依赖的指示器,以便探针反映外部系统状态。
应用生命周期与探针状态
应用在启动期间处于 readiness 状态为 REFUSING_TRAFFIC,启动完成后变为 ACCEPTING_TRAFFIC。
应用信息
info 端点显示应用信息。可以通过 info.* 属性添加自定义信息,例如:
info.app.name=My Application info.app.version=1.0.0
自动配置的信息贡献者
Spring Boot 自动配置多个 InfoContributor,提供 Git 提交信息、构建信息、Java 信息、OS 信息、进程信息和 SSL 信息。
自定义应用信息
可以定义 InfoContributor bean 添加自定义信息。
Git 提交信息
通过 git.properties 文件(由 Maven/Gradle 插件生成)提供 Git 信息,可通过 management.info.git.mode 配置。
构建信息
通过 META-INF/build-info.properties 提供构建信息。
软件物料清单(SBOM)
sbom 端点提供 CycloneDX 格式的软件物料清单信息。其他格式(如 SPDX)也可以支持,通过 management.endpoint.sbom.* 配置。