gpt4 book ai didi

java - Kafka 生产者连接到本地主机而不是真实 IP

转载 作者:行者123 更新时间:2023-12-01 19:07:56 25 4
gpt4 key购买 nike

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

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.

最佳答案

正如 @cricket_007 所说,server.properties 文件中的 advertished.listeners 设置似乎存在问题。让我尝试详细解释一下。

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

enter image description here

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

enter image description here

P.S:您可以将图像中的公共(public) IP 假设为您案例的私有(private) IP。

因此,您应该在 server.properties 中设置广告监听器,如下所示:

advertised.listeners=PLAINTEXT://192.168.199.137:9092

图片引用:https://www.udemy.com/course/kafka-cluster-setup/

关于java - Kafka 生产者连接到本地主机而不是真实 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59867685/

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