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

标签 java spring spring-boot spring-boot-actuator

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

相关文章:

java - Spring Boot +百里叶;第:object+field in checkbox "arbitrarily" giving errors

java - 为什么直接访问无法获取单例实例?

java - 设置最大堆大小是否会影响字符串池大小?

java - 球体多边形中的点,

java - 在 CXF RS 中,我可以在请求过滤器中获取资源方法吗?

带有@PreAuthorize 注解的java.lang.NoSuchMethodError

java - Tomcat 服务静态文件

java - 如何使用 spring-boot 注入(inject) Map<Object, List<Object>> ?

spring - org.apache.tomcat.websocket.WsWebSocketContainer 无法转换为 io.undertow.websockets.jsr.ServerWebSocketContainer

java - Spring Boot + Spring Data JPA + H2 :Error creating bean with name 'entityManagerFactory' defined in class path resource