其他分享
首页 > 其他分享> > Spring Boot 2.0.5中来自PrometheusPushGateway的@PreDestroy批注中的BeanCreationNotAllowedException

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

作者:互联网

我使用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]

解决方法:

该PrometheusMetricsExportAutoConfiguration确实存在问题:

(Do not request a bean from a BeanFactory in a destroy method implementation!)

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中的修复程序,我们无法在销毁BeanFactory的过程中从其调用getBean().

我看不到对您有直接的修复方法,除非通过management.metrics.export.prometheus.enabled = false禁用PrometheusMetricsExportAutoConfiguration并将该类的内容复制/粘贴到您自己的类中,然后将@PreDestroy替换为ApplicationListener< ContextClosedEvent>.在销毁豆子之前将这一称呼.参见AbstractApplicationContext.doClose():

 try {
            // Publish shutdown event.
            publishEvent(new ContextClosedEvent(this));
        }
        catch (Throwable ex) {
           ...
        }

        // Destroy all cached singletons in the context's BeanFactory.
        destroyBeans();

另外,请针对Micrometer项目提出一个问题,以按照我为您建议的方式在PrometheusMetricsExportAutoConfiguration中解决此问题.

标签:spring-boot,spring,spring-integration
来源: https://codeday.me/bug/20191108/2008361.html