【问题标题】:Kafka consumer poll forever卡夫卡消费者民意调查永远
【发布时间】:2020-06-26 00:05:54
【问题描述】:

我想弄清楚为什么consumer.poll 在我的测试中永远挂起。

在调试模式下,消费者似乎无法在无限循环中找到组协调器。

我的测试代码:

final String BROKER_PORT = "9092";
final String HOST = "localhost";
final String BOOTSTRAP_SERVERS = HOST + ":" + BROKER_PORT;
final String ZK_PORT = "2181";
final Integer ZK_PORT_INT = Integer.valueOf(ZK_PORT);
final String ZK_HOST = HOST + ":" + ZK_PORT;

    final String topic = "test-topic-10";

    //start zookeeper
    String path = new File(".").getCanonicalPath();
    zookeeper = new TestingServer(ZK_PORT_INT, new File(path));
    Thread.sleep(5_000);

    //start broker
    final File logDirectory = Files.createTempDir();
    logDirectory.deleteOnExit();
    final Properties p = new Properties();
    p.put("zookeeper.connect", zookeeper.getConnectString());
    p.put("broker.id", "1");
    p.put("num.partitions", "1");
    p.put("host.name", HOST);
    p.put("port", BROKER_PORT);
    p.put("log.dir", logDirectory.getAbsolutePath());
    p.put("auto.create.topics.enable", "true");
    p.put("delete.topic.enable", "true");
    p.put("log.cleaner.dedupe.buffer.size", 2 * 1024 * 1024L + "");
    new KafkaServerStartable(new KafkaConfig(p)).startup();

    //send one record with producer
    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    KafkaProducer<String, String> producer = new KafkaProducer<>(props);
    producer.send(new ProducerRecord<>(topic, "key", "val"));

    // Try to poll record with consumer
    Properties properties = new Properties();
    properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS);
    properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
    KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
    consumer.subscribe(Collections.singletonList(topic));
    System.out.println("POLL!");
    ConsumerRecords<String, String> records = consumer.poll(Duration.ofDays(1));
    System.out.println(records);

【问题讨论】:

  • 使用Duration.ofDays(1),您是在告诉客户“最多等待一天才能收到消息”
  • @cricket_007 我知道,这是一个示例代码。实际上我正在使用 WakUpException
  • 如果您可以发布与 KafkaServerStartable 相关但无法正常工作的代码,那就太好了
  • @VaibhavGupta 它已经发布了。上面的代码consumer.poll 操作永远挂了,因为消费者找不到协调器

标签: java apache-kafka apache-zookeeper


【解决方案1】:

将以下属性添加到您的消费者并重试

 properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

【讨论】:

  • 如果解决方案有效,请告诉我,我会向您解释
  • 谢谢,您的解决方案有效!我想是因为消费者是在生产者发送之后启动的,对吧?但它只有在我在 docker 上运行 zookeeper 和 kafka 代理时才有效。使用TestingServerKafkaServerStartable 相关代码仍然无法正常工作
  • 如果对您有帮助,请将答案标记为已接受。您的假设是正确的,此属性的默认值是最新的并且消费者之前已启动。
  • 感谢您的帮助并支持您的回答,但直到我能够使用 TestinServerKafkaServerStartable 轮询消息后,问题才得以解决。我仍然需要在 docker 中启动 Zookeeper 和 Kafka 代理
  • 好的,我看看能不能提供更多帮助。
猜你喜欢
  • 1970-01-01
  • 2020-05-25
  • 1970-01-01
  • 2019-07-03
  • 2018-05-05
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
相关资源
最近更新 更多