Kafka Producer无法通过代码连接

时间:2020-05-25 03:08:26

标签: java apache-kafka

我无法通过代码连接到本地运行的Kafka实例-我可以使用Kafka-Console-Producer和Kafka-Console-Consumer成功连接,但是当我使用Kafka Java SDK并仅使用Java Producer连接时并产生任何消息,它失败并显示以下错误:

[Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected

不确定这是否重要,但是我正在Windows和Kafka 2.12-2.5.0上使用WSL2进行测试。 如何启用更多详细的日志记录并进行调试?

我的生产者代码的片段

Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

ProducerRecord<String, String> record = new ProducerRecord<String, String>("first_topic","first_value");

KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);
kafkaProducer.send(record);

1 个答案:

答案 0 :(得分:1)

很抱歉,您会收到误报。我的问题在某种程度上与WSL2有关-我在Linux发行版的WSL2中启动了我的ZK和Kafka Broker,但是错误地尝试从Windows中运行的Java App访问该Broker!

当我尝试从Linux本身运行该Java应用程序时。有用 !