简体   繁体   English

Spring Kafka Test with Confluent Kafka Avro Serializer 找不到 ZKClientConfig

[英]Spring Kafka Test with Confluent Kafka Avro Serializer cannot find ZKClientConfig

I struggle with starting embedded Kafka for intergration testing.我在启动嵌入式 Kafka 进行集成测试时遇到了困难。 This error is displayed:显示此错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedKafka': Invocation of init method failed;原因:org.springframework.beans.factory.BeanCreationException:创建名为“embeddedKafka”的bean时出错:调用init方法失败; nested exception is java.lang.NoClassDefFoundError: org/apache/zookeeper/client/ZKClientConfig嵌套异常是 java.lang.NoClassDefFoundError: org/apache/zookeeper/client/ZKClientConfig

This is my minimal sample.这是我的最小样本。 I need to use @ExtendWith(SpringExtension.class) as long as I need to autowire further Spring beans.只要我需要进一步自动装配 Spring bean,我就需要使用@ExtendWith(SpringExtension.class)

@ExtendWith(SpringExtension.class)
@EmbeddedKafka(
        partitions = 1,
        bootstrapServersProperty = "spring.kafka.bootstrap-servers")
public class KafkaTest {

    @Test
    public void test(EmbeddedKafkaBroker broker) {
        var brokerList = broker.getBrokersAsString();
        log.debug("Embedded Kakfa topics {}", broker.getTopics());
    }
}

I use:我用:

  • Java 11 Java 11
  • Spring Boot 2.3 Spring 启动 2.3
  • jUnit 5 jUnit 5

My guess the dependency inconsistence takes the place here, however, it is weird since I believe Spring Boot parent can achieve the dependencies compatibility.我猜这里发生了依赖不一致,但是,这很奇怪,因为我相信 Spring Boot parent 可以实现依赖兼容性。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/>
</parent>

For sure, I list related dependencies in pom.xml below:当然,我在下面的pom.xml中列出了相关的依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
    <groupId>io.confluent</groupId>
    <artifactId>kafka-avro-serializer</artifactId>
    <version>5.2.3</version>
</dependency>

<!-- TEST SCOPE -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.6.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka-test</artifactId>
    <scope>test</scope>
</dependency>

I tried to exclude org.apache.kafka:kafka-clients in both org.springframework.kafka:spring-kafka-test and io.confluent:kafka-avro-serializer with no luck and the same error ocurred.我试图排除org.apache.kafka:kafka-clientsorg.springframework.kafka:spring-kafka-testio.confluent:kafka-avro-serializer没有运气和相同的错误。

<exclusion>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
</exclusion>

Short answer:简短的回答:

These dependencies are in clash:这些依赖是冲突的:

  • io.confluent:kafka-avro-serializer:5.2.3
  • org.springframework.kafka:spring-kafka-test:jar:2.5.0.RELEASE
  • ... from the parent org.springframework.boot:spring-boot-starter-parent:2.3.0.RELEASE ...来自父org.springframework.boot:spring-boot-starter-parent:2.3.0.RELEASE

The former one uses org.apache.zookeeper:zookeeper:jar:3.4.13:compile which should be excluded:前者使用org.apache.zookeeper:zookeeper:jar:3.4.13:compile应该排除:

<dependency>
    <groupId>io.confluent</groupId>
    <artifactId>kafka-avro-serializer</artifactId>
    <version>5.2.3</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Long answer:长答案:

Inspecing mvn dependency:tree before shows that kafka-avro-serializer includes surprisingly org.apache.zookeeper dependency while spring-kafka-test doesn't:检查mvn dependency:tree before 显示kafka-avro-serializer包含令人惊讶的org.apache.zookeeper依赖项,而spring-kafka-test没有:

Extracted for kafka-avro-serializerkafka-avro-serializer提取

[INFO] +- io.confluent:kafka-avro-serializer:jar:5.2.3:compile  
[INFO] |  +- io.confluent:kafka-schema-registry-client:jar:5.2.3:compile    
[INFO] |  +- io.confluent:common-config:jar:5.2.3:compile   
[INFO] |  \- io.confluent:common-utils:jar:5.2.3:compile    
[INFO] |     +- org.apache.zookeeper:zookeeper:jar:3.4.13:compile   
[INFO] |     |  +- jline:jline:jar:0.9.94:compile   
[INFO] |     |  +- org.apache.yetus:audience-annotations:jar:0.5.0:compile  
[INFO] |     |  \- io.netty:netty:jar:3.10.6.Final:compile  
[INFO] |     \- com.101tec:zkclient:jar:0.10:compile

Extracted for spring-kafka-testspring-kafka-test提取

[INFO] \- org.springframework.kafka:spring-kafka-test:jar:2.5.0.RELEASE:test    
[INFO]    +- org.apache.kafka:kafka-clients:jar:test:2.5.0:test 
[INFO]    +- org.apache.kafka:kafka-streams:jar:2.5.0:test  
[INFO]    |  +- org.apache.kafka:connect-json:jar:2.5.0:test    
[INFO]    |  |  \- org.apache.kafka:connect-api:jar:2.5.0:test  
[INFO]    |  \- org.rocksdb:rocksdbjni:jar:5.18.3:test  
[INFO]    +- org.apache.kafka:kafka-streams-test-utils:jar:2.5.0:test   
[INFO]    +- org.apache.kafka:kafka_2.12:jar:2.5.0:test 
[INFO]    |  +- com.fasterxml.jackson.module:jackson-module-scala_2.12:jar:2.11.0:test  
[INFO]    |  |  \- com.fasterxml.jackson.module:jackson-module-paranamer:jar:2.11.0:test    
[INFO]    |  |     \- com.thoughtworks.paranamer:paranamer:jar:2.8:test 
[INFO]    |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-csv:jar:2.11.0:test 
[INFO]    |  +- net.sf.jopt-simple:jopt-simple:jar:5.0.4:test   
[INFO]    |  +- com.yammer.metrics:metrics-core:jar:2.2.0:test  
[INFO]    |  +- org.scala-lang.modules:scala-collection-compat_2.12:jar:2.1.3:test  
[INFO]    |  +- org.scala-lang.modules:scala-java8-compat_2.12:jar:0.9.0:test   
[INFO]    |  +- org.scala-lang:scala-library:jar:2.12.10:test   
[INFO]    |  +- org.scala-lang:scala-reflect:jar:2.12.10:test   
[INFO]    |  +- com.typesafe.scala-logging:scala-logging_2.12:jar:3.9.2:test    
[INFO]    |  \- commons-cli:commons-cli:jar:1.4:test    
[INFO]    +- org.apache.kafka:kafka_2.12:jar:test:2.5.0:test    
[INFO]    \- org.junit.jupiter:junit-jupiter-api:jar:5.6.2:test 
[INFO]       +- org.apiguardian:apiguardian-api:jar:1.1.0:test  
[INFO]       +- org.opentest4j:opentest4j:jar:1.2.0:test    
[INFO]       \- org.junit.platform:junit-platform-commons:jar:1.6.2:test

The spring-kafka-test is missing org.apache.zookeeper:zookeeper therefore the test cannot connect to Zookeeper and error is shown: spring-kafka-test缺少org.apache.zookeeper:zookeeper因此测试无法连接到 Zookeeper 并显示错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedKafka': Invocation of init method failed;原因:org.springframework.beans.factory.BeanCreationException:创建名为“embeddedKafka”的bean时出错:调用init方法失败; nested exception is java.lang.NoClassDefFoundError: org/apache/zookeeper/client/ZKClientConfig嵌套异常是 java.lang.NoClassDefFoundError: org/apache/zookeeper/client/ZKClientConfig

Excluding such dependency from kafka-avro-serializer fixes the problem (see the Short Answer ).kafka-avro-serializer中排除这种依赖可以解决问题(请参阅简短答案)。 The ouptut of mvn dependency:tree confirms the org.apache.zookeeper:zookeeper is properly excluded from kafka-avro-serializer and included in spring-kafka-test . mvn dependency:tree的输出确认org.apache.zookeeper:zookeeper已正确排除在kafka-avro-serializer之外并包含在spring-kafka-test中。

Extracted for kafka-avro-serializer :kafka-avro-serializer提取:

+- io.confluent:kafka-avro-serializer:jar:5.2.3:compile
[INFO] |  +- io.confluent:kafka-schema-registry-client:jar:5.2.3:compile
[INFO] |  +- io.confluent:common-config:jar:5.2.3:compile
[INFO] |  \- io.confluent:common-utils:jar:5.2.3:compile
[INFO] |     \- com.101tec:zkclient:jar:0.10:compile

Extracted for spring-kafka-test :spring-kafka-test提取:

\- org.springframework.kafka:spring-kafka-test:jar:2.5.0.RELEASE:test
[INFO]    +- org.apache.kafka:kafka-clients:jar:test:2.5.0:test
[INFO]    +- org.apache.kafka:kafka-streams:jar:2.5.0:test
[INFO]    |  +- org.apache.kafka:connect-json:jar:2.5.0:test
[INFO]    |  |  \- org.apache.kafka:connect-api:jar:2.5.0:test
[INFO]    |  \- org.rocksdb:rocksdbjni:jar:5.18.3:test
[INFO]    +- org.apache.kafka:kafka-streams-test-utils:jar:2.5.0:test
[INFO]    +- org.apache.kafka:kafka_2.12:jar:2.5.0:test
[INFO]    |  +- com.fasterxml.jackson.module:jackson-module-scala_2.12:jar:2.11.0:test
[INFO]    |  |  \- com.fasterxml.jackson.module:jackson-module-paranamer:jar:2.11.0:test
[INFO]    |  |     \- com.thoughtworks.paranamer:paranamer:jar:2.8:test
[INFO]    |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-csv:jar:2.11.0:test
[INFO]    |  +- net.sf.jopt-simple:jopt-simple:jar:5.0.4:test
[INFO]    |  +- com.yammer.metrics:metrics-core:jar:2.2.0:test
[INFO]    |  +- org.scala-lang.modules:scala-collection-compat_2.12:jar:2.1.3:test
[INFO]    |  +- org.scala-lang.modules:scala-java8-compat_2.12:jar:0.9.0:test
[INFO]    |  +- org.scala-lang:scala-library:jar:2.12.10:test
[INFO]    |  +- org.scala-lang:scala-reflect:jar:2.12.10:test
[INFO]    |  +- com.typesafe.scala-logging:scala-logging_2.12:jar:3.9.2:test
[INFO]    |  +- org.apache.zookeeper:zookeeper:jar:3.5.7:test
[INFO]    |  |  +- org.apache.zookeeper:zookeeper-jute:jar:3.5.7:test
[INFO]    |  |  +- org.apache.yetus:audience-annotations:jar:0.5.0:test
[INFO]    |  |  +- io.netty:netty-handler:jar:4.1.49.Final:test
[INFO]    |  |  |  +- io.netty:netty-common:jar:4.1.49.Final:test
[INFO]    |  |  |  +- io.netty:netty-resolver:jar:4.1.49.Final:test
[INFO]    |  |  |  +- io.netty:netty-buffer:jar:4.1.49.Final:test
[INFO]    |  |  |  +- io.netty:netty-transport:jar:4.1.49.Final:test
[INFO]    |  |  |  \- io.netty:netty-codec:jar:4.1.49.Final:test
[INFO]    |  |  \- io.netty:netty-transport-native-epoll:jar:4.1.49.Final:test
[INFO]    |  |     \- io.netty:netty-transport-native-unix-common:jar:4.1.49.Final:test
[INFO]    |  \- commons-cli:commons-cli:jar:1.4:test
[INFO]    +- org.apache.kafka:kafka_2.12:jar:test:2.5.0:test
[INFO]    \- org.junit.jupiter:junit-jupiter-api:jar:5.6.2:test
[INFO]       +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO]       +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO]       \- org.junit.platform:junit-platform-commons:jar:1.6.2:test

I would like to add some information to the previous answer, because i ran into a similar problem today.我想在上一个答案中添加一些信息,因为我今天遇到了类似的问题。

The main issue is that the kafka-client 2.5.0 is relying on Zookeeper 3.5 and no longer Zookeeper 3.4 (see https://spring.io/projects/spring-kafka for compatibiliy matrix)主要问题是 kafka-client 2.5.0 依赖于 Zookeeper 3.5 而不再是 Zookeeper 3.4(有关兼容性矩阵,请参见https://spring.io/projects/spring-kafka

The ZkClientConfig class has been introduced in Zookeeper 3.5.2 So any Zookeeper version below this will provoke this error. ZkClientConfig class 已在 Zookeeper 3.5.2 中引入,因此任何低于此的 Zookeeper 版本都会引发此错误。 For testing purpose, you need to remove any zookeeper dependency already present and below 3.5.2 and add post 3.5.2 to your test dependencies.出于测试目的,您需要删除任何已经存在且低于 3.5.2 的 zookeeper 依赖项,并将 3.5.2 后添加到您的测试依赖项中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Kafka 与 Confluent Kafka Avro 反序列化器 - Spring Kafka with Confluent Kafka Avro Deserializer 在 Spring 启动应用程序中使用来自 Confluent 的模式注册表与 Avro 和 Kafka - Using Schema Registry from Confluent with Avro and Kafka in Spring Boot Applications Confluent Kafka Streams - 找不到类 io.confluent.connect.avro.ConnectDefault - Confluent Kafka Streams - Could not find class io.confluent.connect.avro.ConnectDefault 融合的Kafka Avro反序列化器的JAVA版本错误 - JAVA Version Error with Confluent Kafka Avro Deserializer 字节 avro 类型不使用 kafka confluent GenericAvroDeserializer 反序列化 - Bytes avro type not deserializing with kafka confluent GenericAvroDeserializer Spring Kafka 找不到 Kafka Properties Bean - Spring Kafka cannot find Kafka Properties Bean 使用Kafka Java API的Avro序列化器和反序列化器 - Avro serializer and deserializer with kafka java api Spring Cloud Stream Kafka&gt;使用Confluent REST Proxy消费Avro消息 - Spring Cloud Stream Kafka > consuming Avro messages from Confluent REST Proxy Kafka Avro Serializer:org.apache.avro.AvroRuntimeException:未打开 - Kafka Avro Serializer: org.apache.avro.AvroRuntimeException: not open Kafka Avro序列化器和反序列化器异常。 Avro支持的类型 - Kafka Avro Serializer and deserializer exception. Avro supported types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM