尝试使用kafka客户端2.6时出错

blmhpbnm  于 2021-06-04  发布在  Kafka
关注(0)|答案(2)|浏览(423)

我在学习Kafka,当我试图实现一个简单的生产者,只会发送“你好世界”

package com.github.joe.kafka.tutorial1;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class ProducerDemo {
    public static void main(String[] args) {

        Properties properties = new Properties();
        String bootstrapServers = "127.0.0.1:9092";
        String topic = "first_topic";
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrapServers);
        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);

        ProducerRecord<String,String> record = new ProducerRecord<String, String>(topic,"Hello World");

        producer.send(record);
        producer.flush();
        producer.close();

    }
}

问题是当我使用kafka client v2.0.0时,它工作正常,但当我使用kafka client 2.6.0时,它失败,并出现以下错误:

[kafka-producer-network-thread | producer-1] ERROR org.apache.kafka.common.utils.KafkaThread - Uncaught exception in thread 'kafka-producer-network-thread | producer-1':
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode
    at org.apache.kafka.common.requests.ApiVersionsRequest$Builder.<clinit>(ApiVersionsRequest.java:36)
    at org.apache.kafka.clients.NetworkClient.handleConnections(NetworkClient.java:910)
    at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:555)
    at org.apache.kafka.clients.producer.internals.Sender.runOnce(Sender.java:325)
    at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:240)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.JsonNode
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 6 more
[main] INFO org.apache.kafka.clients.producer.KafkaProducer - [Producer clientId=producer-1] Closing the Kafka producer with timeoutMillis = 9223372036854775807 ms.
zvms9eto

zvms9eto1#

事实证明,对于kafka客户机2.6.0,还有一个附加的依赖项:jackson databind版本2.0.0不需要它

kmpatx3s

kmpatx3s2#

上面的建议对我有效。我添加了以下解决问题的依赖项。

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.8</version>
</dependency>

相关问题