Kafka 生产者连接到本地主机而不是真实 IP [英] Kafka producer is connecting to localhost instead of the real IP

查看:26
本文介绍了Kafka 生产者连接到本地主机而不是真实 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我想知道为什么 kafka 生产者总是连接到本地主机,但是代理 ip 不是本地主机.那么,有什么帮助吗?有什么想法吗?

Please help I'm wondering why the kafka producer always connect to the localhost however there the broker ip is not the localhost. So, is there any help ? any ideas ?

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) throws Exception{
        String bootstrapServers = "192.168.199.137:9092";

        Properties properties = new Properties();
        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());

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

        System.out.println("kafka IP   " + properties.getProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG));


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


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

    }

}

这里是 pom 内容

<?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>KafkaBeginnersCourse</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

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

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.28</version>
            <!--scope>test</scope-->
        </dependency>
    </dependencies>
</project>

这是输出控制台的列表

[kafka-producer-network-thread | producer-1] INFO org.apache.kafka.clients.Metadata - [Producer clientId=producer-1] Cluster ID: 0TPD87gWR0G18RLKk4gPow
[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] 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] 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] 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] 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] Connection to node 1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.

推荐答案

server.properties 文件中的 advertised.listeners 设置似乎有问题@cricket_007 已经说过了.让我试着详细解释一下.

It seems there is a problem in your advertised.listeners setting in server.properties file as @cricket_007 has already said. Let me try to explain it in detail.

当您的生产者尝试连接 Kafka 代理时,代理会向客户端发送广告主机名以供使用,然后生产者使用此地址连接到代理.所以通常的交流是这样的:

When your producer try to connect Kafka broker, broker sends advertised hostname to client to use, then producer connect to broker with using this address. So normally communication is like this:

但在您的情况下,生产者和经纪人之间的通信是这样的:

But in your case communication between producer and broker is like this:

P.S:您可以假设图像中的公共 IP 作为您的案例的私有 IP.

P.S: You can assume public IP in images as private IP for your case.

因此,您应该像这样在 server.properties 中设置广告侦听器:

As a result you should set your advertised listeners in server.properties like this:

advertised.listeners=PLAINTEXT://192.168.199.137:9092

图片参考:https://www.udemy.com/course/kafka-集群设置/

这篇关于Kafka 生产者连接到本地主机而不是真实 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆