需同时使用globalconf的conf->set("enable.auto.commit", "false", errstr);
以及tconf的tconf->set("auto.offset.reset", "earliest", errstr); // 注意,earliest与latest是有区别的,先看官网解释:
auto.offset.reset | C | smallest, earliest, beginning, largest, latest, end, error | largest | Action to take when there is no initial offset in offset store or the desired offset is out of range: 'smallest','earliest' - automatically reset the offset to the smallest offset, 'largest','latest' - automatically reset the offset to the largest offset, 'error' - trigger an error which is retrieved by consuming messages and checking 'message->err'. Type: enum value |
然后是真实含义:
earliest
当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,从头开始消费
latest
当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,消费新产生的该分区下的数据
none
topic各分区都存在已提交的offset时,从offset后开始消费;只要有一个分区不存在已提交的offset,则抛出异常
所以earliest和latest是有区别的。
在我的场景中就配置为本文最上面的两行就行了。