为什么SpringBoot从2.3.0升级到2.5.0启动失败tomcat出现Prometheus异常?

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: 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]

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

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

我找到了这个参考:https://github.com/prometheus/client_java/issues/452,事实上,该项目包括该答案中提到的致动器和千分尺的依赖项。我最初并没有设置它,所以组件之间的相互作用或者通常如何排除故障和修复这类事情对我来说并不是很清楚。

caused by 看来,Micrometer 依赖项似乎丢失或版本不正确,您需要将其添加到项目的 pom.xml 中。 Micrometer 是一个维度优先的指标收集门面,其目的是让您使用供应商中立的 AP 来计时、计数和衡量您的代码。

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