ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – Spring Boot Actuator:如何在自定义HealthIndicator中获取指标正常运行时间?

2019-07-22 11:02:34  阅读:200  来源: 互联网

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


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

}

但是有一个错误:应用程序上下文中的两个bean之间存在循环依赖关系.

我可以通过休息调用我的端点/执行器/健康来获得正常运行时间指标.

但也许有可能以编程方式进行吗?

附:记录堆栈跟踪:

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 - 

解决方法:

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

标签:spring-boot-actuator,java,spring,spring-boot
来源: https://codeday.me/bug/20190722/1502155.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有