在 kafka 中,当使用事务性生成消息时,消费者偏移量加倍

LKC*_*LKC 4 java apache-kafka spring-kafka

我正在使用 springboot 2、kafk 2.2.0、spring-kafka 2.2.5 制作项目

我使kafka exactly once环境和消息的产生和消耗都很好。

BUTkafka-consumer-groups.sh是这样说的。

TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG
test_topic      0          23              24              1   
test_topic      1          25              26              1   
test_topic      2          21              22              1   
Run Code Online (Sandbox Code Playgroud)

我只向 kafka 发送一条消息,但LOG-END-OFFSET加倍并始终保持 1 条延迟。(在我的 java 应用程序中,生产和消费按预期工作)

我不知道为什么 LOG-END-OFFSET 加倍了。

如果去掉exactly onceconfig,在LOG-END-OFFSETCURRENT-OFFSETcount中没有问题。

这是我的kafkaTemplate设置代码。

@Bean
    @Primary
    public ProducerFactory<String, Object> producerFactory() {
        Map<String, Object> producerProperties = new HashMap<>();
        producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092";
        producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);

        // exactly once producer setup
        producerProperties.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true");

        DefaultKafkaProducerFactory factory = new DefaultKafkaProducerFactory<>(producerProperties, new StringSerializer(), new JsonSerializer<>(KafkaStaticOptions.OBJECT_MAPPER));
        factory.setTransactionIdPrefix("my.transaction.");
        return factory;
    }

    @Bean
    @Primary
    public KafkaTransactionManager<String, Object> kafkaTransactionManager(
        ProducerFactory<String, Object> producerFactory) {
        return new KafkaTransactionManager<>(producerFactory);
    }

    @Bean
    @Primary
    public KafkaTemplate<String, Object> kafkaTemplate(ProducerFactory<String, Object> producerFactory) {
        return new KafkaTemplate<>(producerFactory);
    }
Run Code Online (Sandbox Code Playgroud)

我的生产者代码。

kafkaTemplate.executeInTransaction(kt -> kt.send("test_topic", "test data hahaha"));

Run Code Online (Sandbox Code Playgroud)

我检查了 LOG-END-OFFSET 何时加倍,它是produce transaction commit时间。

我做错了什么配置?

Mic*_*son 5

使用事务时,Kafka在日志中插入“控制批次”以指示消息是否是事务的一部分。

这些批次也被分配了偏移量,所以这就是为什么即使你只发送了一条记录,你也会看到偏移量增加了 2。

如果你想自己检查,你可以使用 DumpLogSegments 工具来显示你的日志内容并查看控制批处理:

./bin/kafka-run-class.sh kafka.tools.DumpLogSegments --files /tmp/kafka-logs/mytopic-0/00000000000000000000.log
Dumping /tmp/kafka-logs/mytopic-0/00000000000000000000.log
Starting offset: 0
baseOffset: 0 lastOffset: 0 count: 1 baseSequence: 0 lastSequence: 0 producerId: 0 producerEpoch: 0 partitionLeaderEpoch: 0 isTransactional: true isControl: false position: 0 CreateTime: 1558083247264 size: 10315 magic: 2 compresscodec: NONE crc: 3531536908 isvalid: true
baseOffset: 1 lastOffset: 1 count: 1 baseSequence: -1 lastSequence: -1 producerId: 0 producerEpoch: 0 partitionLeaderEpoch: 0 isTransactional: true isControl: true position: 10315 CreateTime: 1558083247414 size: 78 magic: 2 compresscodec: NONE crc: 576574952 isvalid: true
Run Code Online (Sandbox Code Playgroud)

我使用事务生产者发送单个记录,您可以看到第二个条目具有isControl: true.