執行器

Spring Boot 包含 Spring Boot 執行器。本節回答了使用它時經常出現的問題。

更改執行器端點的 HTTP 埠或地址

在獨立應用程式中,執行器 HTTP 埠預設為與主 HTTP 埠相同。要使應用程式監聽不同的埠,請設定外部屬性:management.server.port。要監聽完全不同的網路地址(例如,當您有一個用於管理的內部網路和一個用於使用者應用程式的外部網路時),您還可以將 management.server.address 設定為伺服器可以繫結的有效 IP 地址。

有關更多詳細資訊,請參閱 ManagementServerProperties 原始碼和“生產就緒功能”部分中的 自定義管理伺服器埠

自定義清理

要控制清理,請定義一個 SanitizingFunction bean。與函式一起呼叫的 SanitizableData 提供對鍵和值的訪問,以及它們來自的 PropertySource。這允許您,例如,清理來自特定屬性源的每個值。每個 SanitizingFunction 會按順序呼叫,直到某個函式更改了可清理資料的值。

將健康指標對映到 Micrometer 指標

Spring Boot 健康指標返回一個 Status 型別來指示整體系統健康狀況。如果您想監控特定應用程式的健康水平或發出警報,您可以將這些狀態作為 Micrometer 指標匯出。預設情況下,Spring Boot 使用狀態碼“UP”、“DOWN”、“OUT_OF_SERVICE”和“UNKNOWN”。要匯出這些狀態,您需要將這些狀態轉換為一組數字,以便它們可以與 Micrometer Gauge 一起使用。

以下示例展示了編寫此類匯出器的一種方式

  • Java

  • Kotlin

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

import org.springframework.boot.health.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.health.contributor.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.health.actuate.endpoint.HealthEndpoint
import org.springframework.boot.health.contributor.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
	}

}
© . This site is unofficial and not affiliated with VMware.