Kafka系列之消息重新消费
  TEZNKK3IfmPf 2023年11月13日 44 0

需求来源,在review前人留下的屎山代码时发现如下截图所示的代码片段:

Kafka系列之消息重新消费

 

也就是说代码是空实现的。

于是有此需求:消息重新消费。

调研

实现方案

  1. 修改偏移量,即offset,可通过脚本实现
  2. 新增group,需通过代码实现

Kafka的偏移量的保存方式,根据不同版本号有3种方式:保存在zookeeper中、保存在kafka的自带_consumer_offset这个topic中、保存在自定义的存储系统中。

版本

首先需要知道Kafka什么版本,找到Kafka的安装目录。可以通过find命令:find / -name '*kafka*'
根据命令输出得知安装目录为:/usr/local/kafka 进入libs目录,发现很多kafka_2.11-2.2.0.*文件。其中,2.11为scala版本,2.2.0为kafka版本。

脚本

使用Kafka自带的bin目录下的kafka-consumer-groups.sh脚本设置消费者组(consumer group)的位移, 这是0.11.0.0版本提供的新功能且只适用于新版本consumer。在此版本之前,如果要为已有的consumer group调整位移必须要手动编写Java程序调用KafkaConsumer#seek方法。

使用此脚本修改consumer group的位移,有个前提:consumer group必须是inactive的,即不能是处于正在工作中的状态。

格式:
./kafka-consumer-groups.sh --bootstrap-server ip:9092 --group <> --topic <topic> --reset-offsets <offset_option>

如:./kafka-consumer-groups.sh --bootstrap-server 172.100.200.200:9092 --group collect_data_business_service --topic topic_event:0 --reset-offsets --to-offset 195725 --execute

topic
指定topic的作用域:

  • --all-topics:为consumer group下所有topic的所有分区调整位移
  • --topic t1 --topic t2:为指定的若干个topic的所有分区调整位移
  • --topic t1:0,1,2:为指定的topic的某个分区调整位移

reset-offsets
确定位移重设策略

  • --to-earliest:把位移调整到分区当前最小位移
  • --to-latest:把位移调整到分区当前最新位移
  • --to-current:把位移调整到分区当前位移
  • --to-offset <offset>:把位移调整到指定位移处
  • --shift-by N:把位移调整到当前位移 + N处,N为负数表示向前移动
  • --to-datetime <datetime>:把位移调整到大于给定时间的最早位移处,datetime格式yyyy-MM-ddTHH:mm:ss.xxx
  • --by-duration <duration>:把位移调整到距离当前时间指定间隔的位移处,duration格式是PnDTnHnMnS,`\如PT0H5M0S
  • --from-file <file>:从CSV文件中读取调整策略

常见报错:

  • Error: Executing consumer group command failed due to org.apache.kafka.common.errors.TimeoutException: Timed out waiting for a node assignment.
    原因:命令指定的IP有误,或端口不对,或未开放等,连接超时
  • Error: Assignments can only be reset if the group ‘collect_data_business_service’ is inactive, but the current state is Stable.
    原因:在修改offset时,需要停止消费者应用程序,如杀掉Java进程,停止container容器等
  • WARN New offset (0) is lower than earliest offset for topic partition topic_event-0. Value will be set to 195551 (kafka.admin.ConsumerGroupCommand$)
    原因:

代码

@Override
public void afterPropertiesSet() throws Exception {
    Properties p = new Properties();
    p.setProperty("bootstrap.servers", "172.100.200.200:9092");
    p.setProperty("group.id", "collect_data_business_service");
    p.setProperty("auto.offset.reset", "earliest");
    p.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    p.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

    KafkaConsumer<String, String> consumer = new KafkaConsumer<>(p);
    consumer.subscribe(Collections.singleton("topic_event"));
    consumer.poll(Duration.ofSeconds(1));
    consumer.seekToBeginning(consumer.assignment());
    while (true) {
        ConsumerRecords<String, String> message = consumer.poll(Duration.ofSeconds(1));
        log.info("topic_event---collect_data_business_service--消费消息:" + message);
        CollectDataV2Content content = JsonUtil.jsonToBean(message.toString(), CollectDataV2Content.class);
        service.saveContent(content);
    }
}

配置:

  • auto.offset.reset:只能配置为:latest, earliest, none,应用启动时会检查此项配置,不正确会报错
  • key.deserializer:必须配置,否则报错:Caused by: org.apache.kafka.common.config.ConfigException: Missing required configuration "key.deserializer" which has no default value.
  • value.deserializer:同上
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月15日   31   0   0 bootstrap
  TEZNKK3IfmPf   2023年11月15日   28   0   0 apachejava
TEZNKK3IfmPf