我想將應用程式的健康狀態報告為儀表,并且我希望使用與 spring-boot-actuator 相同的健康指標,但是,我沒有看到 spring-boot-actuator 依賴項中的任何可匯出組件,我可能可以在這里使用。
我想寫的代碼:
@Component
public class HealthCounterMetric {
private final Counter statusCounter;
public HealthCounterMetric(MeterRegistry meterRegistry, SystemHealth systemHealth) {
this.statusCounter = meterRegistry.counter("service.status");
}
@Scheduled(fixedRate = 30000L)
public void reportHealth() {
//do report health
}
}
當然SystemHealth不是匯出的bean。Spring Boot 執行器是否會匯出我可以通過這種方式消費的 bean?
uj5u.com熱心網友回復:
參考檔案描述了如何通過將HealthEndpoint的回應映射到儀表來做到這一點:
@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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354969.html
