Spring Boot 2.5.5版本MicrometerClock配置异常求助
问题背景
项目基于Spring Boot 2.5.5开发,启动时触发配置异常,推测是版本冲突导致,升级Spring Boot版本后问题仍未解决。
项目pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>zodiac.zdc.microservice</groupId> <artifactId>zdc-microservice-event-dispatcher</artifactId> <version>115.0.0.0</version> <name>zdc-microservice-event-dispatcher</name> <description>Event dispatcher microservice project</description> <properties> <java.version>1.8</java.version> <zara.zerba.version>91.0.0.0</zara.foundation.version> </properties> <dependencies> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-atlas</artifactId> <version>1.11.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>runtime</scope> <version>2.2.6.RELEASE</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> <version>0.10.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_hotspot</artifactId> <version>0.10.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_servlet</artifactId> <version>0.10.0</version> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-micrometer</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.javers</groupId> <artifactId>javers-core</artifactId> <version>5.12.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> <version>2.2.6.RELEASE</version> </dependency> </dependencies> </project>
异常信息
java.lang.IllegalStateException: Error processing condition on io.micrometer.spring.autoconfigure.MetricsAutoConfiguration.micrometerClock at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-2.4.3.jar:2.4.3]
核心原因及修复方案
1. Spring Boot Starter版本冲突
父依赖指定为2.5.5,但手动给spring-boot-starter-web、spring-boot-starter-test、spring-boot-starter-integration指定了2.2.6.RELEASE版本,这会导致Spring Boot核心组件(如autoconfigure)版本混乱,出现spring-boot-autoconfigure-2.4.3.jar就是冲突的直接表现。
修复: 移除这些starter的手动版本指定,继承父依赖的统一版本:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency>
2. Micrometer版本不一致
当前项目中Micrometer组件版本混乱:micrometer-registry-atlas用1.11.0,micrometer-core和micrometer-registry-prometheus用1.6.6,而Spring Boot 2.5.5对应的Micrometer版本是1.7.5,版本不兼容会触发MetricsAutoConfiguration的条件处理异常。
修复: 统一使用Spring Boot父依赖管理的Micrometer版本,移除手动指定的版本:
<!-- 移除手动version,使用父依赖管理的1.7.5版本 --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-atlas</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> </dependency>
3. Resilience4j版本适配问题
Resilience4j 1.6.1与Spring Boot 2.5.5适配性不足,建议升级到兼容版本(如1.7.1,适配Spring Boot 2.5.x),同时替换为Spring Boot专用starter:
<dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring-boot2</artifactId> <version>1.7.1</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-micrometer</artifactId> <version>1.7.1</version> </dependency>
4. Prometheus客户端版本优化
simpleclient 0.10.0版本较老,建议升级到适配Micrometer 1.7.x的版本(如0.16.0),或者直接使用Spring Boot的Actuator starter简化集成:
<!-- 新增Actuator依赖,配合Prometheus注册器使用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 升级simpleclient版本 --> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> <version>0.16.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_hotspot</artifactId> <version>0.16.0</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_servlet</artifactId> <version>0.16.0</version> </dependency>
修改后的完整pom.xml示例
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>zodiac.zdc.microservice</groupId> <artifactId>zdc-microservice-event-dispatcher</artifactId> <version>115.0.0.0</version> <name>zdc-microservice-event-dispatcher</name> <description>Event dispatcher microservice project</description> <properties> <java.version>1.8</java.version> <zara.zerba.version>91.0.0.0</zara.foundation.version> <resilience4j.version>1.7.1</resilience4j.version> <prometheus-simpleclient.version>0.16.0</prometheus-simpleclient.version> </properties> <dependencies> <!-- Spring Boot Starters --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Micrometer --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-atlas</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <!-- Resilience4j --> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring-boot2</artifactId> <version>${resilience4j.version}</version> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-micrometer</artifactId> <version>${resilience4j.version}</version> </dependency> <!-- Prometheus --> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient</artifactId> <version>${prometheus-simpleclient.version}</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_hotspot</artifactId> <version>${prometheus-simpleclient.version}</version> </dependency> <dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_servlet</artifactId> <version>${prometheus-simpleclient.version}</version> </dependency> <!-- Javers --> <dependency> <groupId>org.javers</groupId> <artifactId>javers-core</artifactId> <version>5.12.0</version> </dependency> </dependencies> </project>
内容的提问来源于stack exchange,提问作者Dharmin Patel

