Spring Kafka跨服务版本不兼容报错问题排查咨询
Spring Kafka 依赖版本冲突问题排查
问题现象
项目两个服务插件层声明的Spring Boot版本均为2.5.3,在服务中集成Kafka时出现版本不兼容问题,无法统一两个服务的Kafka依赖版本:
- 初始参考已完成集成的老服务,引入如下依赖:
服务启动直接抛出类不存在错误:implementation 'org.springframework.integration:spring-integration-kafka:3.1.5.RELEASE' implementation 'org.springframework.kafka:spring-kafka:2.2.15.RELEASE'Error creating bean with name 'kafkaProducerFactory' defined in class path resource [org/springframework/boot/autoconfigure/kafka/KafkaAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.core.ProducerFactory]: Factory method 'kafkaProducerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/kafka/core/MicrometerProducerListener - 查阅Spring Kafka官方兼容矩阵后,将当前服务依赖调整为仅引入2.7.14版本的spring-kafka,服务可正常启动。但把老服务的Kafka依赖也升级到同版本时,再次抛出类不存在错误:
Error creating bean with name 'org.springframework.kafka.config.internalKafkaListenerAnnotationProcessor': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/log/LogAccessor
附:两个服务的Gradle构建配置
原使用2.2.15.RELEASE版本Kafka的服务配置
plugins { id 'java' id 'org.jetbrains.kotlin.jvm' version '1.5.21' id 'com.github.bjornvester.wsdl2java' version '1.2' id 'org.springframework.boot' version '2.5.3' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'org.jetbrains.kotlin.plugin.lombok' version '1.5.21' id 'io.freefair.lombok' version '5.3.0' } group = 'abc' version = '1.5.3' sourceCompatibility = '1.8' wsdl2java { wsdlDir = '../providers' as File } repositories { mavenLocal() mavenCentral() } dependencyManagement { imports { mavenBom("org.springframework.boot:spring-boot-dependencies:2.1.7.RELEASE") { bomProperty("kotlin.version", "1.4.30") } mavenBom "org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR3" } } dependencies { testCompile "junit:junit:4.11" testCompile "org.springframework:spring-test:5.1.9.RELEASE" implementation 'org.springframework:spring-context:5.1.9.RELEASE' implementation 'commons-io:commons-io:2.4' implementation 'io.springfox:springfox-swagger2:2.9.2' implementation 'io.springfox:springfox-swagger-ui:2.9.2' compileOnly 'org.projectlombok:lombok:1.18.8' annotationProcessor 'org.projectlombok:lombok:1.18.8' implementation 'org.springframework.cloud:spring-cloud-starter-sleuth' implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' implementation 'org.springframework.boot:spring-boot-starter-data-rest' implementation 'net.logstash.logback:logstash-logback-encoder:6.3' implementation 'io.sentry:sentry-spring-boot-starter:5.4.0' implementation 'io.sentry:sentry-logback:5.4.0' implementation 'net.javacrumbs.shedlock:shedlock-spring:4.25.0' implementation 'net.javacrumbs.shedlock:shedlock-provider-jdbc-template:4.25.0' implementation 'org.jetbrains.kotlin:kotlin-stdlib' implementation "org.apache.httpcomponents:httpclient:4.5.+" implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.2.Final' implementation 'org.mapstruct:mapstruct:1.4.2.Final' implementation 'org.springframework:spring-web:5.1.9.RELEASE' implementation 'org.springframework.ws:spring-ws-core:2.2.4.RELEASE' implementation "org.springframework.boot:spring-boot-starter-web" implementation "org.springframework.boot:spring-boot-starter-web-services" implementation "org.springframework.boot:spring-boot-starter-security" implementation group: 'com.rabbitmq', name: 'amqp-client', version: '2.3.1' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.webjars:jquery:2.1.4' implementation 'com.thoughtworks.xstream:xstream:1.4.7' implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.9' implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.9' implementation 'com.hazelcast:hazelcast-spring:3.+' implementation 'org.antlr:ST4:4.0.+' implementation 'org.springframework.boot:spring-boot-starter-mail' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.60' implementation group: 'com.opencsv', name: 'opencsv', version: '5.2' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator' implementation group: 'io.micrometer', name: 'micrometer-core' implementation group: 'io.micrometer', name: 'micrometer-registry-prometheus' implementation group: 'joda-time', name: 'joda-time', version: '2.3' implementation 'com.vladmihalcea:hibernate-types-52:2.9.12' implementation 'org.json:json:20090211' implementation 'org.postgresql:postgresql:42.2.2' implementation 'com.fasterxml.jackson.core:jackson-databind' implementation 'com.fasterxml.jackson.module:jackson-module-kotlin' implementation 'com.amazonaws:aws-java-sdk-s3:1.12.23' implementation 'com.amazonaws:aws-java-sdk-core:1.12.23' implementation 'org.springframework.integration:spring-integration-kafka:3.1.5.RELEASE' implementation 'org.springframework.kafka:spring-kafka:2.2.15.RELEASE' implementation 'com.ibm.mq:com.ibm.mq.allclient:9.2.2.0' implementation 'org.springframework:spring-jms' implementation "io.github.microutils:kotlin-logging-jvm:2.0.10" annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final' }
调整后使用2.7.14版本Kafka的服务配置
plugins { id 'org.springframework.boot' version '2.5.3' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' id 'com.github.bjornvester.wsdl2java' version '1.2' } wsdl2java { wsdlDir = '../providers' as File generatedSourceDir = '../providers/ruru/build/generated/wsdl2java/sources/wsdl2java/java' as File } group = 'abc' version = '1.1.13' sourceCompatibility = '1.8' repositories { mavenCentral() maven {url 'https://maven.vaadin.com/vaadin-addons'} } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-mail' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework:spring-oxm:5.1.9.RELEASE' implementation 'org.springframework.ws:spring-ws-core' implementation 'org.springframework.ldap:spring-ldap-core:2.3.2.RELEASE' implementation 'org.springframework.hateoas:spring-hateoas:1.4.0' implementation 'com.vaadin:vaadin-spring-boot-starter:2.0.1' implementation 'com.wcs.wcslib:wcslib-vaadin-widget-recaptcha:2.0.3' implementation 'com.thoughtworks.xstream:xstream:1.4.7' implementation 'org.postgresql:postgresql:42.2.2' implementation 'com.fasterxml.jackson.core:jackson-databind' implementation 'com.vladmihalcea:hibernate-types-52:2.9.12' implementation 'com.vaadin:vaadin-compatibility-server:8.14.3' implementation 'com.vaadin:vaadin-client-compiled:8.14.3' implementation 'com.vaadin:vaadin-compatibility-client-compiled:8.14.3' implementation 'com.vaadin:vaadin-compatibility-themes:8.14.3' implementation 'com.vaadin:vaadin-themes:8.14.3' implementation 'com.vaadin.addon:jpacontainer:4.0.0' implementation 'org.apache.httpcomponents:httpclient:4.5.+' implementation 'org.apache.commons:commons-csv:1.5' implementation 'dev.samstevens.totp:totp:1.6.1' implementation 'commons-io:commons-io:2.4' implementation 'org.json:json:20090211' implementation 'org.apache.tika:tika-core:1.+' implementation("io.sentry:sentry-spring-boot-starter:5.4.0") implementation("io.sentry:sentry-logback:5.4.0") implementation 'commons-beanutils:commons-beanutils:1.9.4' implementation 'org.apache.commons:commons-collections4:4.4' implementation 'io.micrometer:micrometer-core' implementation 'com.opencsv:opencsv:5.2' implementation 'org.springframework.integration:spring-integration-kafka:3.1.5.RELEASE' implementation 'org.springframework.kafka:spring-kafka:2.7.14' runtimeOnly 'io.micrometer:micrometer-registry-prometheus' compileOnly 'org.projectlombok:lombok:1.18.8' annotationProcessor 'org.projectlombok:lombok:1.18.8' testImplementation 'org.springframework.boot:spring-boot-starter-test' } test { useJUnitPlatform() }
问题根因
所谓“两个服务Spring版本完全一致”是误判,实际两个服务的运行时依赖版本差了两个大版本,参考的兼容矩阵本身没有问题:
- 第一个服务在
dependencyManagement中强制导入了Spring Boot 2.1.7.RELEASE、Spring Cloud Greenwich.SR3的BOM,还手动硬编码了spring-context、spring-web等核心组件版本为5.1.9.RELEASE,实际运行时Spring Framework版本为5.1.x,所有Spring生态组件的版本都被BOM锁到了适配Spring Boot 2.1.x的低版本。最初引入spring-kafka 2.2.15.RELEASE时,BOM拉取的低版本Micrometer没有包含MicrometerProducerListener类,直接触发类不存在错误。 - 第二个服务没有导入低版本的Spring Boot BOM,也没有手动锁死Spring核心版本,实际运行时用的是Spring Boot 2.5.3默认自带的Spring Framework 5.3.x,因此spring-kafka 2.7.14可以正常运行。
- 给第一个服务升级spring-kafka到2.7.14时,没有同步升级spring-integration-kafka版本,3.1.5.RELEASE的spring-integration-kafka是适配Spring Kafka 2.2.x、Spring Framework 5.1.x的老版本,而spring-kafka 2.7.x要求Spring Framework版本至少为5.3.x,
org.springframework.core.log.LogAccessor是Spring Framework 5.2之后才新增的类,5.1.x版本中不存在该类,因此触发第二个类不存在错误。
修复方案
- 统一两个服务的依赖管理规则:删除第一个服务中强制导入的Spring Boot 2.1.7.RELEASE、Spring Cloud Greenwich.SR3 BOM配置,移除所有手动硬编码的Spring核心组件版本号,全部交给Spring Boot 2.5.3的默认BOM统一管理版本,从根源上避免版本锁死导致的依赖冲突。
- 同步升级spring-integration-kafka版本到适配Spring Kafka 2.7.x的5.5.x系列,不要继续使用3.1.5.RELEASE的老版本。
- 不需要手动指定spring-kafka版本,Spring Boot 2.5.x默认管理的spring-kafka版本就是2.7.x,和框架自带的Micrometer、Spring Integration等组件版本完全匹配,不会出现类找不到的问题。
内容的提问来源于stack exchange,提问作者agingcabbage32
相关产品推荐
相关产品推荐

