Spring Boot 2.0.5中来自PrometheusPushGateway的@PreDestroy批注中的BeanCreationNotAllowedException

时间:2018-10-12 18:53:24

标签: spring spring-boot spring-integration

我使用PushGatewayConfiguration进行了一些小的更改来配置PrometheusPushGateway。

PrometheusPushGateway设置为在任务结束时通过@PreDestroy批注发送指标。在Spring 2.0.3中,PushGateway可以正常工作,但是在Spring 2.0.5中,我从PushGatewayHandler.shutdown()调用中获得了堆栈跟踪。

通常,我认为正在发生的是在DefaultSingletonBeanRegistry.destroySingletons()方法调用期间删除了单例。

我注意到在2.0.5中删除了Singleton,而PushGatewayHandler.push()依赖于此,而在2.0.3中没有删除。我正在努力解决该问题,并提出了建议。

堆栈跟踪:

 org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'channelRemoteTaskOut': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:208) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:514) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:502) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1198) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.integration.support.management.IntegrationManagementConfigurer.lambda$registerComponentGauges$1(IntegrationManagementConfigurer.java:395) ~[spring-integration-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
        at io.micrometer.core.instrument.internal.DefaultGauge.value(DefaultGauge.java:40) ~[micrometer-core-1.0.6.jar:1.0.6]
        at io.micrometer.prometheus.PrometheusMeterRegistry.lambda$newGauge$3(PrometheusMeterRegistry.java:230) ~[micrometer-registry-prometheus-1.0.6.jar:1.0.6]
        at io.micrometer.prometheus.MicrometerCollector.lambda$collect$0(MicrometerCollector.java:81) ~[micrometer-registry-prometheus-1.0.6.jar:1.0.6]
        at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:267) ~[na:1.8.0_152-ea]
        at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:1.8.0_152-ea]
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_152-ea]
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_152-ea]
        at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_152-ea]
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_152-ea]
        at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_152-ea]
        at io.micrometer.prometheus.MicrometerCollector.collect(MicrometerCollector.java:82) ~[micrometer-registry-prometheus-1.0.6.jar:1.0.6]
        at io.prometheus.client.CollectorRegistry$MetricFamilySamplesEnumeration.findNextElement(CollectorRegistry.java:183) ~[simpleclient-0.4.0.jar:na]
        at io.prometheus.client.CollectorRegistry$MetricFamilySamplesEnumeration.nextElement(CollectorRegistry.java:216) ~[simpleclient-0.4.0.jar:na]
        at io.prometheus.client.CollectorRegistry$MetricFamilySamplesEnumeration.nextElement(CollectorRegistry.java:137) ~[simpleclient-0.4.0.jar:na]
        at io.prometheus.client.exporter.common.TextFormat.write004(TextFormat.java:22) ~[simpleclient_common-0.4.0.jar:na]
        at io.prometheus.client.exporter.PushGateway.doRequest(PushGateway.java:290) ~[simpleclient_pushgateway-0.4.0.jar:na]
        at io.prometheus.client.exporter.PushGateway.pushAdd(PushGateway.java:171) ~[simpleclient_pushgateway-0.4.0.jar:na]
        at com.build.task.batch.metrics.PushGatewayHandler.push(PushGatewayHandler.java:57) [api-task-batch-2.0.3-SNAPSHOT.jar:2.0.3-SNAPSHOT]
        at com.build.task.batch.metrics.PushGatewayHandler.shutdown(PushGatewayHandler.java:70) [api-task-batch-2.0.3-SNAPSHOT.jar:2.0.3-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152-ea]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152-ea]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152-ea]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152-ea]
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeDestroyMethods(InitDestroyAnnotationBeanPostProcessor.java:324) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeDestruction(InitDestroyAnnotationBeanPostProcessor.java:156) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:240) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:571) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:543) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:954) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:504) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:961) [spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1041) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1017) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:967) [spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE]
        at com.build.task.customer.CustomerAnalyticsApplication.main(CustomerAnalyticsApplication.java:23) [classes/:na]

1 个答案:

答案 0 :(得分:3)

PrometheusMetricsExportAutoConfiguration确实存在问题:

  

(请勿在destroy方法的实现中向BeanFactory请求Bean!)

shutdown()调用push();,最终执行DefaultGauge.value(),而该变量与此调用中的lambda完全一样:

this.metricsCaptor.gaugeBuilder("spring.integration.channels", this,
            (c) -> this.applicationContext.getBeansOfType(MessageChannel.class).size())
            .description("The number of message channels")
            .build();

根据Spring Framework中的修复程序,我们无法在销毁期间从getBean()调用BeanFactory

除非您通过PrometheusMetricsExportAutoConfiguration禁用management.metrics.export.prometheus.enabled=false并将该类的内容复制/粘贴到自己的内容,然后将@PreDestroy替换为{ {1}}。在销毁豆子之前将这一称呼。参见ApplicationListener<ContextClosedEvent>

AbstractApplicationContext.doClose()

另外,请针对Micrometer项目提出一个问题,以 try { // Publish shutdown event. publishEvent(new ContextClosedEvent(this)); } catch (Throwable ex) { ... } // Destroy all cached singletons in the context's BeanFactory. destroyBeans(); 的方式为我建议的方法来解决此问题。