Actuator
Spring Boot 包含 Spring Boot Actuator。本節回答其使用中經常出現的問題。
變更 Actuator 端點的 HTTP Port 或位址
在獨立應用程式中,Actuator HTTP port 預設與主要 HTTP port 相同。若要讓應用程式監聽不同的 port,請設定外部屬性:management.server.port
。若要監聽完全不同的網路位址(例如當您有內部網路用於管理,而外部網路用於使用者應用程式時),您也可以將 management.server.address
設定為伺服器能夠綁定的有效 IP 位址。
如需更多詳細資訊,請參閱 ManagementServerProperties
原始碼,以及「生產就緒功能」章節中的 Customizing the Management Server Port。
自訂清理
若要控制清理,請定義一個 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.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
}
}