请帮助我想知道为什么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.
2条答案
按热度按时间7vhp5slm1#
bootstrap.servers
发现所有节点时,您可以只提供一个地址,但最好提供更多地址。您的客户机实际上连接到返回的
advertised.listeners
所有的经纪人。听起来像是你作为听众被送回了localhost。
m2xkgtsf2#
你的身体好像有问题
advertised.listeners
正在设置server.properties
文件@cricket\u007已经说过了。我来详细解释一下。当生产者尝试连接kafka代理时,代理将播发的主机名发送给客户端使用,然后生产者使用此地址连接到代理。所以通常的交流是这样的:
但在你的例子中,制片人和经纪人之间的沟通是这样的:
p、 s:您可以将图像中的公共ip作为您的案例的私有ip。
因此,您应该在server.properties中设置播发的侦听器,如下所示:
图像参考:https://www.udemy.com/course/kafka-cluster-setup/