[英]Spring Boot Actuator: How to get metrics uptime inside a custom HealthIndicator?
我想做一個取決於應用程序正常運行時間的自定義 HealthIndicator。
@Component
public class HealthActuator implements HealthIndicator {
private final MetricsEndpoint metricsEndpoint;
@Autowired
public HealthActuator(MetricsEndpoint metricsEndpoint) {
this.metricsEndpoint = metricsEndpoint;
}
@Override
public Health health() {
long uptime = (Long) metricsEndpoint.invoke().get("uptime");
// logic with uptime
return Health.up().build();
}
}
但是有一個錯誤: a circular dependency between 2 beans in the application context
。
我可以通過對端點 /actuator/health 的休息調用來獲得正常運行時間指標。
但也許有可能以編程方式做到這一點?
PS 日志堆棧跟蹤:
11-01 14:34:09 WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthActuator' defined in file [/Users/serge/projects/bb/bb-imapl/target/classes/bb/imapl/config/HealthActuator.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$$EnhancerBySpringCGLIB$$2bb06d4a]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'healthActuator': Requested bean is currently in creation: Is there an unresolvable circular reference?
11-01 14:34:09 WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthActuator' defined in file [/Users/serge/projects/bb/bb-imapl/target/classes/bb/imapl/config/HealthActuator.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration$$EnhancerBySpringCGLIB$$2bb06d4a]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'healthActuator': Requested bean is currently in creation: Is there an unresolvable circular reference?
11-01 14:34:09 ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
There is a circular dependency between 2 beans in the application context:
- healthActuator defined in file [/Users/serge/projects/bb/bb-imapl/target/classes/bb/imapl/config/HealthActuator.class]
- org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
- healthActuator
11-01 14:34:09 ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
如果您不喜歡 setter 注入,也應該可以使用@Lazy
解決此@Lazy
...
@Autowired
public HealthActuator(@Lazy MetricsEndpoint metricsEndpoint) {
this.metricsEndpoint = metricsEndpoint;
}
EndpointAutoConfiguration
依賴於HealthIndicator
(您使用HealthActuator
實現)。
所以你最終會得到一個循環依賴。 當 2 個 bean 需要彼此實例化自己時(通過構造函數注入),就會發生這種情況。 您可以使用 setter 注入來打破循環:
@Component
public class HealthActuator implements HealthIndicator {
private MetricsEndpoint metricsEndpoint;
@Autowired
private void setMetricsEndpoint(MetricsEndpoint metricsEndpoint) {
this.metricsEndpoint = metricsEndpoint;
}
public HealthActuator() {
}
@Override
public Health health() {
long uptime = (Long) metricsEndpoint.invoke().get("uptime");
// logic with uptime
return Health.up().build();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.