gpt4 book ai didi

java - 警告 org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap 代理 127.0.0.1 :9092 (id: -1 rack: null) disconnected

转载 作者:行者123 更新时间:2023-12-05 03:49:23 32 4
gpt4 key购买 nike

我正在尝试使用 java 程序制作 Kafka 生产者。但是当我运行程序时我收到了一些警告,没有任何错误但是生产者没有发送数据并且警告如下所示。

[kafka-producer-network-thread | producer-1] WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker 127.0.0.1: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 (/127.0.0.1:9092) could not be established. Broker may not be available.

POM.XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>KafkaProject</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.6.0</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>

</dependencies>

</project>

First_producer.java 文件

package Kafka;

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 First_Producer {
public static void main(String[] args) {


String bootstrapServer = "127.0.0.1:9092";

//create producer properties
Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrapServer);
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

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

//create a producer record
ProducerRecord<String, String> record = new ProducerRecord<String,String>("first_topic","hello_world");

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

}
}


Shell 中的消费者命令

kafka-console-consumer --bootstrap-server 127.0.0.1:9092 --topic first_topic --group my-first-app

那么谁能帮我解决这个问题???

最佳答案

你的日志说:

Broker may not be available.

为属性监听器使用正确的 IP/主机名更新您的 config/server.properties 文件。

listeners=PLAINTEXT://X.X.X.X:9092

并使用与您的生产者配置相同的 IP 和端口配置

ProducerConfig.BOOTSTRAP_SERVERS_CONFIG

如果您使用 iptable/firewall 配置在服务器外部运行生产者,请确保您可以从服务器外部访问您的端口号。

关于java - 警告 org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap 代理 127.0.0.1 :9092 (id: -1 rack: null) disconnected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63985827/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com