nosuchmethoderror:kafkaconsumer.subscribe

q9yhzks0  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(414)

我使用以下依赖项:

val akkaVersion = "2.4.9"
val kafkaVersion = "0.10.0.1"

dependencies =  ...
  "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  "com.typesafe.akka" %% "akka-cluster" % akkaVersion,
  "com.softwaremill.reactivekafka" %% "reactive-kafka-core" % "0.10.0"   
        excludeAll(
          ExclusionRule("org.slf4j", "log4j-over-slf4j"),
          ExclusionRule("org.apache.kafka", "kafka_2.11")),
   "org.apache.kafka" % "kafka_2.11" % kafkaVersion,
   "org.apache.kafka" % "kafka-clients" % kafkaVersion,
   "org.apache.kafka" % "connect-json" % kafkaVersion

还有演员:

class ExpKafkaActor extends Actor {

   override def preStart(): Unit = {
    super.preStart()
    val kafka = new ReactiveKafka()

    val publisher = kafka.consume(ConsumerProperties(
      bootstrapServers = "localhost:9092",
      topic = "someTopicName",
      groupId = "groupName",
      valueDeserializer = new StringDeserializer()
    ))

    Source.fromPublisher(publisher).map(m => {
      val message = ProducerMessage(m.value().toUpperCase)
      log.info(s"handle message ${m.value()}")
      message
    })
   }
   ...
}

然后我尝试启动上面的actor,出现以下异常:

java.lang.NoSuchMethodError:
 org.apache.kafka.clients.consumer.KafkaConsumer.subscribe

我发现了以下问题,但没有帮助。如何解决此api冲突?

jhiyze9q

jhiyze9q1#

此问题是由于kafka客户端api不兼容造成的。请使用正确的依赖项作为项目pom.xml的一部分:

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>{kafka.version}</version>
</dependency>

ex: for Kafka 0.10.2.0, use the following one
<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>0.10.2.0</version>
</dependency>

相关问题