| commitSync | 阻塞等待提交结果,简单可靠 |
|---|---|
| commitAsync | 异步提交,失败回调可重试 |
| 语义 | 先处理再提交=可能重复;先提交再处理=可能丢失 |
处理完成后同步提交(Java)
处理完成后同步提交(Java)
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));
for (ConsumerRecord<String, String> r : records) {
process(r);
}
consumer.commitSync();
}多数管道选择“先处理后提交 + 下游幂等”,用少量重复换取不丢数据。