WARN org.apache.kafka.clients.NetworkClient – [Producer clientId=producer-1] Bootstrap broker 127.0.0.1:9092 (id: -1 rack: null) disconnected

huangapple go评论87阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;KafkaProject&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

    &lt;dependencies&gt;

        &lt;!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.kafka&lt;/groupId&gt;
            &lt;artifactId&gt;kafka-clients&lt;/artifactId&gt;
            &lt;version&gt;2.6.0&lt;/version&gt;
        &lt;/dependency&gt;


        &lt;!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
            &lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
            &lt;version&gt;1.7.30&lt;/version&gt;
        &lt;/dependency&gt;

    &lt;/dependencies&gt;

&lt;/project&gt;

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 = &quot;127.0.0.1:9092&quot;;

        //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&lt;String, String&gt; producer= new KafkaProducer&lt;String,String&gt;(properties);

        //create a producer record
        ProducerRecord&lt;String, String&gt;  record = new ProducerRecord&lt;String,String&gt;(&quot;first_topic&quot;,&quot;hello_world&quot;);

        //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.

huangapple
  • 本文由 发表于 2020年9月21日 11:30:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63985827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定