为什么将SpringBoot从2.3.0升级到2.5.0后,启动Tomcat时出现Prometheus异常?

huangapple go评论118阅读模式
英文:

Why is SpringBoot upgrade from 2.3.0 to 2.5.0 failing to start tomcat with Prometheus exceptions?

问题

之前的 SpringBoot 升级都很顺利,但我们升级到 2.5.0 后无法启动。以下是异常的简短版本:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 在类路径资源 [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.class] 中定义的 bean 名称为 'webMvcMetricsFilter' 的创建错误:通过方法 'webMvcMetricsFilter' 的参数 0 表达的不满足的依赖项;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 在类路径资源 [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class] 中定义的 bean 名称为 'prometheusMeterRegistry' 的创建错误:通过方法 'prometheusMeterRegistry' 的参数 0 表达的不满足的依赖项;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 在类路径资源 [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class] 中定义的 bean 名称为 'prometheusConfig' 的创建错误:通过方法 'prometheusConfig' 的参数 0 表达的不满足的依赖项;嵌套异常是 org.springframework.beans.factory.BeanCreationException: 创建 bean 名称为 'management.metrics.export.prometheus-org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusProperties' 的错误:查找方法解析失败;嵌套异常是 java.lang.IllegalStateException: 从类加载器 [org.springframework.boot.loader.LaunchedURLClassLoader@7c0e2abd] 中反射检查类 [org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusProperties] 失败

而最后一个异常似乎导致了这整个混乱:

Caused by: java.lang.IllegalStateException: 从类加载器 [org.springframework.boot.loader.LaunchedURLClassLoader@7c0e2abd] 中反射检查类 [org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusProperties] 失败
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
        at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:267)
        ... 103 common frames omitted
Caused by: java.lang.NoClassDefFoundError: io/micrometer/prometheus/HistogramFlavor
        at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
        at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3244)
        at java.base/java.lang.Class.getDeclaredMethods(Class.java:2387)
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
        ... 105 common frames omitted
Caused by: java.lang.ClassNotFoundException: io.micrometer.prometheus.HistogramFlavor
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:435)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:590)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:129)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:523)
        ... 109 common frames omitted

我在这个引用中找到了相关信息:https://github.com/prometheus/client_java/issues/452,实际上,项目中包括了该答案中提到的 actuator 和 micrometer 的依赖项。起初我并没有设置这个,所以组件之间的相互作用以及通常如何排查和修复这种问题对我来说并不是很清楚。

英文:

Previous SpringBoot upgrades have been painless, but our upgrade to 2.5.0 doesn't start. Here's the sort version of the exception:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcMetricsFilter' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.class]: Unsatisfied dependency expressed through method 'webMvcMetricsFilter' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'prometheusMeterRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]: Unsatisfied dependency expressed through method 'prometheusMeterRegistry' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'prometheusConfig' defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.class]: Unsatisfied dependency expressed through method 'prometheusConfig' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'management.metrics.export.prometheus-org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusProperties': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusProperties] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@7c0e2abd]

and the very last exception that seems to be causing this whole mess:

Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusProperties] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@7c0e2abd]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
        at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:267)
        ... 103 common frames omitted
Caused by: java.lang.NoClassDefFoundError: io/micrometer/prometheus/HistogramFlavor
        at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
        at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3244)
        at java.base/java.lang.Class.getDeclaredMethods(Class.java:2387)
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
        ... 105 common frames omitted
Caused by: java.lang.ClassNotFoundException: io.micrometer.prometheus.HistogramFlavor
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:435)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:590)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:129)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:523)
        ... 109 common frames omitted

I found this reference: https://github.com/prometheus/client_java/issues/452, and in fact the project includes the dependencies for actuator and micrometer mentioned in that answer. I didn't set this up originally so the interplay between components or how one normally troubleshoots and repairs this kind of thing isn't really clear to me.

答案1

得分: 2

caused by看起来像是缺少Micrometer依赖或者版本不正确,您需要将其添加到项目的pom.xml中。Micrometer是一个以维度为先的度量收集外观,其目标是允许您使用中立的供应商API对您的代码进行计时、计数和测量。

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-spring-legacy</artifactId>
    <version>1.5.1</version>
</dependency>
英文:

From the caused by it looks like Micrometer dependecny is missing or not the correct version, you need to add it to pom.xml of your project. Micrometer is a dimensional-first metrics collection facade whose aim is to allow you to time, count, and gauge your code with a vendor neutral AP.

&lt;dependency&gt;
    &lt;groupId&gt;io.micrometer&lt;/groupId&gt;
    &lt;artifactId&gt;micrometer-spring-legacy&lt;/artifactId&gt;
    &lt;version&gt;1.5.1&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年5月30日 07:11:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/62095858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定