对于最新的稳定版本,请使用 Spring Boot 3.5.5spring-doc.cadn.net.cn

驱动器

Spring Boot 包括 Spring Boot 执行器。本节回答了使用它时经常出现的问题。spring-doc.cadn.net.cn

更改执行器端点的 HTTP 端口或地址

在独立应用程序中,Actuator HTTP 端口默认为与主 HTTP 端口相同。要使应用程序侦听不同的端口,请设置 external 属性:management.server.port. 要监听完全不同的网络地址(例如,当您有一个用于管理的内部网络和一个用于用户应用程序的外部网络时),您还可以将management.server.address到服务器能够绑定到的有效 IP 地址。spring-doc.cadn.net.cn

有关更多详细信息,请参阅ManagementServerProperties源代码和自定义管理服务器端口,在“生产就绪功能”部分。spring-doc.cadn.net.cn

定制消毒

要控制清理,请定义SanitizingFunction豆。 这SanitizableData调用函数时,可以使用该函数来访问键和值,以及PropertySource它们来自哪里。例如,这允许您清理来自特定属性源的每个值。 每SanitizingFunction按顺序调用,直到函数更改可清理数据的值。spring-doc.cadn.net.cn

将健康指标映射到千分尺指标

Spring Boot 运行状况指示器返回一个Statustype 以指示整体系统运行状况。如果要监视特定应用程序的运行状况级别或发出警报,可以使用 Micrometer 将这些状态导出为指标。默认情况下,Spring Boot 使用状态代码“UP”、“DOWN”、“OUT_OF_SERVICE”和“UNKNOWN”。要导出这些状态,您需要将这些状态转换为一组数字,以便它们可以与 Micrometer 一起使用Gauge.spring-doc.cadn.net.cn

以下示例显示了编写此类导出器的一种方法:spring-doc.cadn.net.cn

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;
	}

}
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)
class MyHealthMetricsExportConfiguration(registry: MeterRegistry, healthEndpoint: HealthEndpoint) {

	init {
		// This example presumes common tags (such as the app) are applied elsewhere
		Gauge.builder("health", healthEndpoint) { health ->
			getStatusCode(health).toDouble()
		}.strongReference(true).register(registry)
	}

	private fun getStatusCode(health: HealthEndpoint) = when (health.health().status) {
		Status.UP -> 3
		Status.OUT_OF_SERVICE -> 2
		Status.DOWN -> 1
		else -> 0
	}

}