kafka_2.12:3.4.0引入的org.apache.zookeeper.server无ZooKeeperServer类求替代
问题解决:Kafka 3.4.0依赖的ZooKeeper中找不到ZooKeeperServer类
你遇到的问题本质是:Kafka 3.4.0引入的org.apache.zookeeper:zookeeper:3.6.3仅包含ZooKeeper客户端相关代码,不包含服务端类(比如ZooKeeperServer)。ZooKeeper从3.5+版本开始,将服务端和客户端代码拆分为不同模块,Kafka默认只依赖客户端模块。
替代方案
1. 显式引入ZooKeeper服务端模块
如果代码确实需要直接使用ZooKeeperServer类,在项目依赖中添加ZooKeeper服务端模块,版本要和Kafka依赖的ZooKeeper版本保持一致(这里是3.6.3):
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper-server</artifactId> <version>3.6.3</version> </dependency>
2. 使用Kafka提供的嵌入式ZooKeeper(仅测试场景)
如果是为了测试需要启动本地ZooKeeper,Kafka的测试包中提供了EmbeddedZooKeeper类,可直接使用。需要引入Kafka测试依赖:
<dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka_2.12</artifactId> <version>3.4.0</version> <classifier>test</classifier> <scope>test</scope> </dependency>
3. 迁移至Kafka KRaft模式
Kafka 2.8及以上版本支持无ZooKeeper的KRaft模式,完全不需要依赖ZooKeeper。如果业务场景允许,建议直接迁移到KRaft模式,从根源上避免ZooKeeper相关依赖问题。
你的依赖层级信息
[INFO] +- org.apache.kafka:kafka_2.12:jar:3.4.0:compile [INFO] | +- org.scala-lang:scala-library:jar:2.12.15:compile [INFO] | +- org.apache.kafka:kafka-server-common:jar:3.4.0:runtime [INFO] | +- org.apache.kafka:kafka-group-coordinator:jar:3.4.0:runtime [INFO] | +- org.apache.kafka:kafka-metadata:jar:3.4.0:runtime [INFO] | +- org.apache.kafka:kafka-raft:jar:3.4.0:runtime [INFO] | +- org.apache.kafka:kafka-storage:jar:3.4.0:runtime [INFO] | | \- org.apache.kafka:kafka-storage-api:jar:3.4.0:runtime [INFO] | +- net.sourceforge.argparse4j:argparse4j:jar:0.7.0:runtime [INFO] | +- com.fasterxml.jackson.module:jackson-module-scala_2.12:jar:2.13.4:runtime [INFO] | | \- com.thoughtworks.paranamer:paranamer:jar:2.8:runtime [INFO] | +- com.fasterxml.jackson.dataformat:jackson-dataformat-csv:jar:2.13.4:runtime [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.4:runtime [INFO] | +- net.sf.jopt-simple:jopt-simple:jar:5.0.4:runtime [INFO] | +- org.bitbucket.b_c:jose4j:jar:0.7.9:runtime [INFO] | +- com.yammer.metrics:metrics-core:jar:2.2.0:runtime [INFO] | +- org.scala-lang.modules:scala-collection-compat_2.12:jar:2.6.0:runtime [INFO] | +- org.scala-lang.modules:scala-java8-compat_2.12:jar:1.0.2:runtime [INFO] | +- org.scala-lang:scala-reflect:jar:2.12.15:runtime [INFO] | +- com.typesafe.scala-logging:scala-logging_2.12:jar:3.9.4:runtime [INFO] | +- org.apache.zookeeper:zookeeper:jar:3.6.3:runtime
内容的提问来源于stack exchange,提问作者purvi ijantkar
相关产品推荐
相关产品推荐

