gpt4 book ai didi

java - 如何在 Spring Boot Actuator 中注册匿名 HealthIndicators

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:41 24 4
gpt4 key购买 nike

此链接writing_custom_healthindicators描述了如何注册自定义 HealthIndicators 以便它们可以与 Spring Boot Actuator 一起使用。链接中的示例使用 @Component 注册实现 HealthIndicator 接口(interface)的自定义类。

但我正在创建匿名 HealthIndicator 实例。下面是如何创建这些匿名 HealthIndicators 实例的示例。

        HealthIndicator healthIndicator = () -> {
Health h;
//custom code to instantiate Health object.
return h;
};
//how to register h with Spring?

假设上面的代码在一个 for 循环中,其中创建了许多 HealthIndicators。如何将它们注册到 Spring,以便 Spring Boot Actuator 能够识别链接中提到的它们?

最佳答案

一种方法是提供自定义 HealthEndpoint像这样:

@Configuration
class HealthConfig {
@Bean
HealthEndpoint healthEndpoint(Map<String, HealthIndicator> defaultIndicators){
for(int i=0;i<10;i+=1){
defaultIndicators.put("custom_indicator_" + i, ()-> Health.down().build());
}
return new HealthEndpoint(new OrderedHealthAggregator(), defaultIndicators);
}
}

关于java - 如何在 Spring Boot Actuator 中注册匿名 HealthIndicators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com