我想要报告应用程序的健康状态作为量规,并且我希望使用与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吗?
发布于 2021-11-08 17:16:39
参考文档describes how to do this by mapping the HealthEndpoint's response to a gauge
@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://stackoverflow.com/questions/69887048
复制相似问题