kafka消费者不返回任何记录

xkftehaa  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(391)

我正试着和Kafka搞个小游戏。但是,在java中创建使用者时,该使用者不会得到任何消息。即使当我使用相同的url/主题启动kafka-console-consumer.sh时,我也会收到消息。有人知道我会做错什么吗?此代码由get api调用。

public List<KafkaTextMessage> receiveMessages() {
    log.info("Retrieving messages from kafka");
    val props = new Properties();
    // See https://kafka.apache.org/documentation/#consumerconfigs
    props.put("bootstrap.servers", "my-cluster-kafka-bootstrap:9092");
    //props.put("client.id", "my-topic consumer");
    props.put("group.id", "test");
    props.put("enable.auto.commit", "false");
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    ImmutableList.Builder<KafkaTextMessage> builder;
    try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {
        consumer.subscribe(Collections.singletonList(TEXT_MESSAGE_TOPIC));
        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
        builder = ImmutableList.builder();
        for (ConsumerRecord<String, String> record : records) {
            builder.add(new KafkaTextMessage(record.value()));
            log.info("We got at position: {} key:{} value: {}", record.offset(), record.key(), record.value());
            consumer.commitSync();
        }
    }
    return builder.build();
}
mgdq6dx1

mgdq6dx11#

尝试添加 auto.offset.reset=earliest 你的消费者财产。默认值设置为 latest . 我这么说是因为我看到你的 group.id 设置为 test ,可能已在以前的测试中使用的值。

相关问题