Kafka java client 连接异常(org.apache.kafka.common.errors.TimeoutException: Failed to update metadata )...

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1、当kafka 客户端连接如下异常

Exception in thread "main"

java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
    at org.apache.kafka.clients.producer.KafkaProducer$FutureFailure.(KafkaProducer.java:1057)
    at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:764)
    at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:701)
    at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:609)
    at com.KafkaProducerExample.main(KafkaProducerExample.java:53)
Caused by: org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.
212034_OXgy_3247419.png

问题原因:

1. java client 包与kafka server 版本不一致

2、/kafka_2.11-0.9.0.0/config/server.properties 

listerners 需配置ip ,不能配置主机名,因本地Hosts中不存在对应的Ip配置,导致producer 无法连接

Kafka java client 连接异常(org.apache.kafka.common.errors.TimeoutException: Failed to update metadata )..._第1张图片

官网默认提示 设置host.name advertised.host.name 为Ip 时可以通过ip连接,但经过测试,此配置失败。只有通过修改Listerners 后成功。

3、查看zk中得配置信息,

查看配置  get  /brokers/ids/0   当时ip时,可以正常连接。

Kafka java client 连接异常(org.apache.kafka.common.errors.TimeoutException: Failed to update metadata )..._第2张图片

4、java client 连接实例(生成者)

Properties props = new Properties();
props.put("bootstrap.servers", "192.168.2.176:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 10);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

Producer producer = new KafkaProducer<>(props);
for (int i = 0; i < 100; i++) {
    System.out.println(i);
 Future future =  producer.send(new ProducerRecord("kafka1", Integer.toString(i), Integer.toString(i)));
    System.out.println(future.get());
   // System.out.println(future.get());
}

producer.close();

消费者实例,不用配置zk连接也可以消费了

public static void main(String[] args) {
    Properties props = new Properties();
    props.put("bootstrap.servers", "192.168.2.176:9092");
    props.put("group.id", "test");
    props.put("enable.auto.commit", "true");
    props.put("auto.commit.interval.ms", "1000");
    props.put("session.timeout.ms", "30000");
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    KafkaConsumer consumer = new KafkaConsumer(props);
    consumer.subscribe(Arrays.asList("kafka1"));
    while (true) {
        ConsumerRecords records = consumer.poll(100);
        for (ConsumerRecord record : records)
            System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value());
    }
}

依赖版本


    org.apache.kafka
    kafka-clients
    0.9.0.0

转载于:https://my.oschina.net/u/3247419/blog/1189241

你可能感兴趣的:(Kafka java client 连接异常(org.apache.kafka.common.errors.TimeoutException: Failed to update metadata )...)