java api call cloud server kafka can not connect

Problem Description:

使用阿里云服务器,安装kafka,在学习过程中 使用java api 通过公网ip 连接kafka过程中 ,生产者和消费者无法连接 kafka broker 并且Java 报错

Caused by: org.apachekafka.common.errors.TimeoutException: Topic first not present in metadata after 60000 ms
insert image description here

public class KafkaCustomProducer {
    
    
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
    
    
        //配置
        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "39.105.151.222:9092");
        //value序列化类型
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        //创建kafka生产者对象
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer(properties);
//        //有回调
        kafkaProducer.send(new ProducerRecord<>("first", "hello"), (metadata, exception) -> {
    
    
            if (exception == null) {
    
    
                System.out.println("回调完成" + "分区" + metadata.partition() + "主题" + metadata.topic());
            }});
        kafkaProducer.close();

}}


Cause Analysis:

After checking the code, there is no error, and the port 9092 has been accessed through the packet capture code, and a tcp connection has been established. Finally, the problem is located in kafka, which does not listen to the message of the public network ip: port 9092.

Finally, we locate the problem in the files kafka configin the directory and on the configuration ofserver.properties
advertised.listenerslisteners


solution:

advertised.listeners:
It is similar to listeners. This parameter is also used to publish listeners to clients, but this parameter is mainly used in external environments. For example, machines on the cloud are usually equipped with multiple network cards (private network card and public network card .For this kind of machine, the user can set this parameter to bind the public network IP for external clients, and configure listeners to bind the private network IP for inter-broker communication. listeners: The
default
network card is bound, and the default network card is usually It is bound to the private network IP. In the case of a single network card, you only need to configure and change the parameters. In actual use scenarios, for machines equipped with multiple network cards, it needs to be used with advertised.listeners. If you do not configure advertised.listeners cloud The machines on the network are prone to the problem that clients cannot obtain data.

broker.id=0
#此处是内网地址
listeners=PLAINTEXT://172.31.172.28:9092
#此处填写公网IP地址
advertised.listeners=PLAINTEXT://39.105.151.122:9092

Run the java api at this time

insert image description here

Supongo que te gusta

Origin blog.csdn.net/qq_46645840/article/details/128547287
Recomendado
Clasificación