简体   繁体   English

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

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

This link writing_custom_healthindicators describes how to register custom HealthIndicators so that they can be used with Spring Boot Actuator. 此链接writing_custom_healthindicators描述了如何注册自定义HealthIndicators以便它们可以与Spring Boot Actuator一起使用。 The example in the link uses @Component to register custom classes that implement the HealthIndicator interface. 链接中的示例使用@Component来注册实现HealthIndicator接口的自定义类。

But I am creating anonymous HealthIndicator instances. 但我正在创建匿名的HealthIndicator实例。 Below is an example of how these anonymous HealthIndicators instances can be created. 下面是如何创建这些匿名HealthIndicators实例的示例。

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

Lets say the code above is in a for loop where many HealthIndicators are being created. 让我们说上面的代码是在一个for循环中,正在创建许多HealthIndicators How can they be registered with Spring so that Spring Boot Actuator recognizes them as mentioned in the link? 如何在Spring中注册,以便Spring Boot Actuator能够识别链接中提到的那些?

One way is to provide custom HealthEndpoint like so: 一种方法是提供自定义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