第1章 消费者详解

tips 学完这一章你可以、
深入学习Kafka数据消费大致流程
如何创建并使用Kafka消费者
Kafka消费者常用配置

1.1 概念入门

1.1.1 消费者和消费组

Kafka消费者是消费组的一部分,当多个消费者形成一个消费组来消费主题时,每个消费者会收到不同分区的消息。假设有一个T1主题,该主题有4个分区;同时我们有一个消费组G1,这个消费组只有一个消费者C1。那么消费者C1将会收到这4个分区的消息,如下所示:

c2135fc8f6f60c13d2a82dd0a8b2ab6d.png

Kafka一个很重要的特性就是,只需写入一次消息,可以支持任意多的应用读取这个消息。换句话说,每个应用都可以读到全量的消息。为了使得每个应用都能读到全量消息,应用需要有不同的消费组。对于上面的例子,假如我们新增了一个新的消费组G2,而这个消费组有两个消费者,那么会是这样的:

1c7b8716b6d5d2bdd1d1480087e0daed.png

1.2 消息接收

见代码库:com.heima.kafka.chapter3.KafkaConsumerAnalysis

1.2.1 必要参数设置

需要更多大厂的面试真题或笔试题可以点我

KafkaConsumer实例中参数众多,后续会深入讲解public static Properties initConfig() { Properties props = new Properties(); // 与KafkaProducer中设置保持一致 props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // 必填参数,该参数和KafkaProducer中的相同,制定连接Kafka集群所需的broker地址清 单,可以设置一个或者多个 props.put("bootstrap.servers", brokerList); // 消费者隶属于的消费组,默认为空,如果设置为空,则会抛出异常,这个参数要设置成具有一 定业务含义的名称 props.put("group.id", groupId); // 指定KafkaConsumer对应的客户端ID,默认为空,如果不设置KafkaConsumer会自动生成 一个非空字符串 props.put("client.id", "consumer.client.id.demo"); return props; }

1.2.2 订阅主题和分区

创建完消费者后我们便可以订阅主题了,只需要通过调用subscribe()方法即可,这个方法接收一个主题列表

KafkaConsumer consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList(topic));12

另外,我们也可以使用正则表达式来匹配多个主题,而且订阅之后如果又有匹配的新主题,那么这个消费组会立即对其进行消费。正则表达式在连接Kafka与其他系统时非常有用。比如订阅所有的测试主题:

consumer.subscribe(Pattern.compile("heima*"));

指定订阅的分区

// 指定订阅的分区 consumer.assign(Arrays.asList(new TopicPartition("topic0701", 0)));

1.2.2 反序列化

// 与KafkaProducer中设置保持一致 props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // 必填参数,

1.2.3 位移提交

对于Kafka中的分区而言,它的每条消息都有唯一的offset,用来表示消息在分区中的位置。当我们调用poll()时,该方法会返回我们没有消费的消息。当消息从broker返回消费者时,broker并不跟踪这些消息是否被消费者接收到;Kafka让消费者自身来管理消费的位移,并向消费者提供更新位移的接口,这种更新位移方式称为提交(commit)。

重复消费

007974438fa062740bad528c5c2e0de9.png

消息丢失

20d1b50548fc0fddfd7c9ae223608d16.png

自动提交
这种方式让消费者来管理位移,应用本身不需要显式操作。当我们将enable.auto.commit设置为true,
那么消费者会在poll方法调用后每隔5秒(由auto.commit.interval.ms指定)提交一次位移。和很多其他操作一样,自动提交也是由poll()方法来驱动的;在调用poll()时,消费者判断是否到达提交时间,如果是则提交上一次poll返回的最大位移。

需要注意到,这种方式可能会导致消息重复消费。假如,某个消费者poll消息后,应用正在处理消息,在3秒后Kafka进行了重平衡,那么由于没有更新位移导致重平衡后这部分消息重复消费。

同步提交

见代码库:com.heima.kafka.chapter3.CheckOffsetAndCommit

public static Properties initConfig() { Properties props = new Properties(); props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList); props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId); props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); // 手动提交开启 props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false); return props; }
while (true) {ConsumerRecords records = consumer.poll(1000); if (records.isEmpty()) { break; }List> partitionRecords = records.records(tp); lastConsumedOffset = partitionRecords.get(partitionRecords.size() - 1).offset();consumer.commitSync();//同步提交消费位移 }12345678

异步提交
手动提交有一个缺点,那就是当发起提交调用时应用会阻塞。当然我们可以减少手动提交的频率,但这个会增加消息重复的概率(和自动提交一样)。另外一个解决办法是,使用异步提交的API。

见代码:com.heima.kafka.chapter3.OffsetCommitAsyncCallback

但是异步提交也有个缺点,那就是如果服务器返回提交失败,异步提交不会进行重试。相比较起来,同步提交会进行重试直到成功或者最后抛出异常给应用。异步提交没有实现重试是因为,如果同时存在多个异步提交,进行重试可能会导致位移覆盖。举个例子,假如我们发起了一个异步提commitA,此时的提交位移为2000,随后又发起了一个异步提交commitB且位移3000;commitA提交失败但commitB提交成功,此时commitA进行重试并成功的话,会将实际上将已经提交的位移从3000回滚到2000,导致消息重复消费。

异步回调

try { while (running.get()) { ConsumerRecords records = consumer.poll(1000); for (ConsumerRecord record : records) { //do some logical processing. }// 异步回调 consumer.commitAsync(new OffsetCommitCallback() { @Override public void onComplete(Map offsets, Exception exception) {if(exception==null){System.out.println(offsets);}else {log.error("fail to commit offsets {}", offsets, exception);}}});}} finally {consumer.close();}1234567891011121314151617181920212223

1.2.4 指定位移消费

到目前为止,我们知道消息的拉取是根据poll()方法中的逻辑来处理的,但是这个方法对于普通开发人
员来说就是个黑盒处理,无法精确掌握其消费的起始位置。
seek()方法正好提供了这个功能,让我们得以追踪以前的消费或者回溯消费。
见代码库:com.heima.kafka.chapter3.SeekDemo

/*** 指定位移消费 */ public class SeekDemo extends ConsumerClientConfig { public static void main(String[] args) { Properties props = initConfig(); KafkaConsumer consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList(topic)); // timeout参数设置多少合适?太短会使分区分配失败,太长又有可能造成一些不必要的等待 consumer.poll(Duration.ofMillis(2000)); // 获取消费者所分配到的分区 Set assignment = consumer.assignment(); System.out.println(assignment); for (TopicPartition tp : assignment) { // 参数partition表示分区,offset表示指定从分区的哪个位置开始消费 consumer.seek(tp, 10); } // consumer.seek(new TopicPartition(topic,0),10); while (true) { ConsumerRecords records = consumer.poll(Duration.ofMillis(1000)); //consume the record. for (ConsumerRecord record : records) { System.out.println(record.offset() + ":" + record.value()); } } }1234567891011121314151617181920212223

增加判断是否分配到了分区,见代码库:com.heima.kafka.chapter3.SeekDemoAssignment

public static void main(String[] args) { Properties props = initConfig();KafkaConsumer consumer = new KafkaConsumer<>(props); consumer.subscribe(Arrays.asList(topic)); long start = System.currentTimeMillis(); Set assignment = new HashSet<>(); while (assignment.size() == 0) { consumer.poll(Duration.ofMillis(100)); assignment = consumer.assignment();  }long end = System.currentTimeMillis();  System.out.println(end - start);  System.out.println(assignment);  for (TopicPartition tp : assignment) {  consumer.seek(tp, 10);  } while (true) {  ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));  //consume the record. for (ConsumerRecord record : records) { System.out.println(record.offset() + ":" + record.value());  }  }  }12345678910111213141516171819202122
  • 指定从分区末尾开始消费 ,见代码库:com.heima.kafka.chapter3.SeekToEnd
// 指定从分区末尾开始消费 Map offsets = consumer.endOffsets(assignment); for (TopicPartition tp : assignment) { consumer.seek(tp, offsets.get(tp)); }12345
  • 演示位移越界操作,修改代码如下:
for (TopicPartition tp : assignment) { //consumer.seek(tp, offsets.get(tp)); consumer.seek(tp, offsets.get(tp) + 1); }
  • 会通过auto.offset.reset参数的默认值将位置重置,效果如下:
INFO [Consumer clientId=consumer-1, groupId=group.heima] Fetch offset 1 is out of range for partition heima-0, resetting offset (org.apache.kafka.clients.consumer.internals.Fetcher:967) INFO [Consumer clientId=consumer-1, groupId=group.heima] Fetch offset 10 is out of range for partition heima-1, resetting offset(org.apache.kafka.clients.consumer.internals.Fetcher:967) INFO [Consumer clientId=consumer-1, groupId=group.heima] Resetting offset forpartition heima-0 to offset 0. (org.apache.kafka.clients.consumer.internals.Fetcher:583) INFO [Consumer clientId=consumer-1, groupId=group.heima] Resetting offset for partition heima-1 to offset 9. (org.apache.kafka.clients.consumer.internals.Fetcher:583)123456789101112

1.2.4 再均衡监听器

再均衡是指分区的所属从一个消费者转移到另外一个消费者的行为,它为消费组具备了高可用性和伸缩性提供了保障,使得我们既方便又安全地删除消费组内的消费者或者往消费组内添加消费者。不过再均衡发生期间,消费者是无法拉取消息的。
见代码库:com.heima.kafka.chapter3.CommitSyncInRebalance

public static void main(String[] args) { Properties props = initConfig(); KafkaConsumer consumer = new KafkaConsumer<>(props); Map currentOffsets = new HashMap<>(); consumer.subscribe(Arrays.asList(topic),newConsumerRebalanceListener() { @Override public void onPartitionsRevoked(Collection partitions) { // 劲量避免重复消费 consumer.commitSync(currentOffsets); }@Override public void onPartitionsAssigned(Collection partitions) { //do nothing. } });  try { while (isRunning.get()) {  ConsumerRecords records = consumer.poll(Duration.ofMillis(1000));  for (ConsumerRecord record : records) { System.out.println(record.offset() + ":" + record.value());  // 异步提交消费位移,在发生再均衡动作之前可以通过再均衡监听器的 onPartitionsRevoked回调执行commitSync方法同步提交位移。 currentOffsets.put(new TopicPartition(record.topic(),  record.partition()),  new OffsetAndMetadata(record.offset() + 1)); }consumer.commitAsync(currentOffsets, null);  } } finally {  consumer.close(); }  }1234567891011121314151617181920212223242526

1.2.5 消费者拦截器

之前章节讲了生产者拦截器,对应的消费者也有相应的拦截器概念,消费者拦截器主要是在消费到消息或者在提交消费位移时进行的一些定制化的操作。

使用场景
对消费消息设置一个有效期的属性,如果某条消息在既定的时间窗口内无法到达,那就视为无效,不需要再被处理。
见代码库:com.heima.kafka.chapter3.ConsumerInterceptorTTL

public ConsumerRecords onConsume(ConsumerRecords records) {System.out.println("before:" + records); long now = System.currentTimeMillis(); Map>> newRecords = new HashMap<>(); for (TopicPartition tp : records.partitions()) { List> tpRecords = records.records(tp); List> newTpRecords = new ArrayList<> (); for (ConsumerRecord record : tpRecords) { if (now - record.timestamp() < EXPIRE_INTERVAL) { newTpRecords.add(record); } } if (!newTpRecords.isEmpty()) {  newRecords.put(tp, newTpRecords);  }  } return new ConsumerRecords<>(newRecords);  }12345678910111213141516171819

实现自定义拦截器之后,需要在KafkaConsumer中配置指定这个拦截器,如下

// 指定消费者拦截器 props.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG,ConsumerInterceptorTTL.class .getName());

效果演示
发送端同时发送两条消息,其中一条修改timestamp的值来使其变得超时,如下:
com.heima.kafka.chapter3.ProducerFastStart

ProducerRecord record = new ProducerRecord<>(topic, "Kafka-demo- 001", "hello, Kafka!"); ProducerRecord record2 = new ProducerRecord<>(topic, 0, System.currentTimeMillis() - 10 * 1000, "Kafka-demo-001", "hello, Kafka!->超时");1234

启动消费端运行如下,只收到了未超时的消息:

8c62ca433acf3fa9671ee3ff1e40fe0d.png

1.2.6 消费者参数补充

fetch.min.bytes
这个参数允许消费者指定从broker读取消息时最小的数据量。当消费者从broker读取消息时,如果数据量小于这个阈值,broker会等待直到有足够的数据,然后才返回给消费者。对于写入量不高的主题来说,这个参数可以减少broker和消费者的压力,因为减少了往返的时间。而对于有大量消费者的主题来说,则可以明显减轻broker压力。

fetch.max.wait.ms
上面的fetch.min.bytes参数指定了消费者读取的最小数据量,而这个参数则指定了消费者读取时最长等待时间,从而避免长时间阻塞。这个参数默认为500ms。

max.partition.fetch.bytes
这个参数指定了每个分区返回的最多字节数,默认为1M。也就是说,KafkaConsumer.poll()返回记录列表时,每个分区的记录字节数最多为1M。如果一个主题有20个分区,同时有5个消费者,那么每个消费者需要4M的空间来处理消息。实际情况中,我们需要设置更多的空间,这样当存在消费者宕机时,其他消费者可以承担更多的分区.

max.poll.records
这个参数控制一个poll()调用返回的记录数,这个可以用来控制应用在拉取循环中的处理数据量。

总结

本章主要讲解了消费者和消费组的概念,以及如何正确的使用KafkaConsumer,其中重点讲解了参数的配置,订阅、反序列化、位移提交、再均衡、拦截器等知识点。

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐