繁体   English   中英

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

[英]how to register anonymous HealthIndicators in Spring Boot Actuator

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

但我正在创建匿名的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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM