gpt4 book ai didi

java - Spring 启动执行器 : How to get metrics uptime inside a custom HealthIndicator?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:53:33 27 4
gpt4 key购买 nike

我想做一个取决于应用程序正常运行时间的自定义 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();
}

}

但是有一个错误:应用程序上下文中 2 个 bean 之间的循环依赖

我可以通过对端点/actuator/health 的 rest 调用来获取正常运行时间指标。

但也许可以通过编程来实现?

附言日志堆栈跟踪:

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 注入(inject),也可以使用 @Lazy 来解决这个问题...

@Autowired
public HealthActuator(@Lazy MetricsEndpoint metricsEndpoint) {
this.metricsEndpoint = metricsEndpoint;
}

关于java - Spring 启动执行器 : How to get metrics uptime inside a custom HealthIndicator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40357467/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com