kafka:消费者提交消费偏移量,提交的是当前消息的 offset ,还是 offset+1 作者:马育民 • 2021-12-03 14:01 • 阅读:10067 # 结论 提交的是 当前消息的 `offset+1` # 源码分析 执行下面代码时: ``` @KafkaListener(clientIdPrefix = "mycon",topics = {"userlog26"},idIsGroup = false) public void onMessage(@Payload String message, @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic, @Header(KafkaHeaders.OFFSET) String offset, Consumer consumer) {consumer // 消费的哪个topic、partition的消息,打印出消息内容 System.out.println(String.format("消费:topic:%s-partition:%s-offset:%s-value:%s", topic,partition,offset,message)); //手动异步比较 consumer.commitAsync(); } ``` 打印的消息中的偏移量是 `1` ### 跟踪如下图 [![](https://www.malaoshi.top/upload/pic/kafka/0DFA343B-2D38-4ed4-9738-449222AF3757.png)](https://www.malaoshi.top/upload/pic/kafka/0DFA343B-2D38-4ed4-9738-449222AF3757.png) 提交的 `offset` 是 `2`,即:当前消息的 `offset+1` # 图解 [![](https://www.malaoshi.top/upload/pic/kafka/Snipaste_2021-12-04_19-30-32.png)](https://gitee.com/65242847/pic/blob/master/kafka/Snipaste_2021-12-04_19-30-32.png) 原文出处:http://malaoshi.top/show_1IX2L6BMYoNY.html