跳到主要内容
版本:3.5.10

Actuator

QWen Max 中英对照 Actuator

Spring Boot 包含了 Spring Boot Actuator。本节解答了在使用过程中经常出现的问题。

更改 Actuator 端点的 HTTP 端口或地址

在独立应用程序中,Actuator HTTP 端口默认与主 HTTP 端口相同。要让应用程序监听不同的端口,请设置外部属性:management.server.port。若要监听完全不同的网络地址(例如,当您为管理流量使用内部网络,而为用户应用程序使用外部网络时),还可以将 management.server.address 设置为服务器能够绑定的有效 IP 地址。

更多细节,请参阅 ManagementServerProperties 源代码以及 “Production-Ready Features” 章节中的 Customizing the Management Server Port

自定义脱敏

要对清理(sanitization)过程进行控制,请定义一个 SanitizingFunction Bean。调用该函数时传入的 SanitizableData 对象提供了对键(key)和值(value)的访问,以及它们来源的 PropertySource。这样,例如,你可以清理来自特定 PropertySource 的所有值。每个 SanitizingFunction 会按顺序被调用,直到某个函数修改了 sanitizable data 的值为止。

将健康指标映射到 Micrometer 指标

Spring Boot 的健康指示器(health indicators)会返回一个 Status 类型,以表示系统的整体健康状况。如果你想对特定应用程序的健康级别进行监控或告警,可以使用 Micrometer 将这些状态导出为指标(metrics)。默认情况下,Spring Boot 使用 “UP”、“DOWN”、“OUT_OF_SERVICE” 和 “UNKNOWN” 这些状态码。为了导出这些状态,你需要将这些状态转换为一组数值,以便与 Micrometer 的 Gauge 一起使用。

以下示例展示了一种编写此类 exporter 的方法:

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;

import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
public class MyHealthMetricsExportConfiguration {

public MyHealthMetricsExportConfiguration(MeterRegistry registry, HealthEndpoint healthEndpoint) {
// This example presumes common tags (such as the app) are applied elsewhere
Gauge.builder("health", healthEndpoint, this::getStatusCode).strongReference(true).register(registry);
}

private int getStatusCode(HealthEndpoint health) {
Status status = health.health().getStatus();
if (Status.UP.equals(status)) {
return 3;
}
if (Status.OUT_OF_SERVICE.equals(status)) {
return 2;
}
if (Status.DOWN.equals(status)) {
return 1;
}
return 0;
}

}