英文:
WARN org.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Bootstrap broker 127.0.0.1:9092 (id: -1 rack: null) disconnected
问题
以下是翻译好的内容:
我正在尝试使用 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 中的 Consumer 命令
kafka-console-consumer --bootstrap-server 127.0.0.1:9092 --topic first_topic --group my-first-app
有人能帮我解决这个问题吗?
英文:
I am trying to make Kafka producer using java program. but when I run program I got some warnings, there is no any error but producer is not sending data and warning is like below.
[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 file
<?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 File
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();
}
}
Consumer Command in Shell
kafka-console-consumer --bootstrap-server 127.0.0.1:9092 --topic first_topic --group my-first-app
So can anyone assist me to solve this problem???
答案1
得分: 4
你的日志显示:
> Broker 可能不可用。
请在 config/server.properties
文件中更新 property listener 的正确 IP/主机名。
listeners=PLAINTEXT://X.X.X.X:9092
并且在你的生产者配置中使用相同的 IP 和端口配置:
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG
请确保如果你在服务器外部运行生产者,使用 iptables/防火墙配置,保持端口号在服务器外部是可访问的。
英文:
Your log says:
> Broker may not be available.
Update your config/server.properties
file with proper IP/hostname for property listener.
listeners=PLAINTEXT://X.X.X.X:9092
And use the same IP and Port configuration with your producer config for
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG
Make sure, you keep your port number is accessible outside from server if you are running your producer outside of server using iptable/firewall config.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论