Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring Boot Actuator中注册匿名健康指示器_Java_Spring_Spring Boot_Spring Boot Actuator - Fatal编程技术网

Java 如何在Spring Boot Actuator中注册匿名健康指示器

Java 如何在Spring Boot Actuator中注册匿名健康指示器,java,spring,spring-boot,spring-boot-actuator,Java,Spring,Spring Boot,Spring Boot Actuator,此链接介绍如何注册自定义健康指示灯,以便与弹簧启动执行器一起使用。链接中的示例使用@Component注册实现healthdindicator接口的自定义类 但我正在创建匿名healthdindicator实例。下面是如何创建这些匿名HealthIndicators实例的示例 HealthIndicator healthIndicator = () -> { Health h; //custom code to instant

此链接介绍如何注册自定义
健康指示灯
,以便与弹簧启动执行器一起使用。链接中的示例使用
@Component
注册实现
healthdindicator
接口的自定义类

但我正在创建匿名
healthdindicator
实例。下面是如何创建这些匿名
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能够识别链接中提到的它们?

一种方法是提供自定义设置,例如:

@配置
类HealthConfig{
@豆子
HealthEndpoint HealthEndpoint(映射默认指标){
for(int i=0;i Health.down().build());
}
返回新的HealthEndpoint(new OrderedHealthAggregator(),defaultIndicators);
}
}

太棒了。我今天要试试这个。
@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);
    }
}