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);
}
}