当前位置:网站首页>第5章 消费者组详解
第5章 消费者组详解
2022-07-06 09:29:00 【留不住斜阳】
5.1 消费者组概念
什么是consumer group?
consumer group是kafka提供的可扩展且具有容错性的消费者机制。既然是一个组,那么组内必然可以有多个消费者或消费者实例(consumer instance),它们共享一个公共的ID,即group ID。组内的所有消费者协调在一起来消费订阅的主题(subscribed topics)的所有分区(partition)。每个分区只能由同一个消费组内的一个consumer来消费。
consumer group三个特征
- consumer group可以有一个或多个consumer instance,consumer instance可以是一个进程,也可以是一个线程
- group.id是一个字符串,唯一标识一个consumer group
- consumer group订阅的topic的每个分区只能分配给某个group下的一个consumer(当然该分区还可以被分配给其他group)
5.2 消费者位置
消费者在消费的过程中需要记录自己消费了多少数据,即消费位置信息。在Kafka中这个位置信息有个专门的术语:位移(offset)。很多消息引擎都把这部分信息保存在服务器端(broker端)。这样做的好处是实现简单,但会有三个主要的问题:
- broker从此变成有状态的,会影响伸缩性;
- 需要引入应答机制(acknowledgement)来确认消费成功。
- 由于要保存很多consumer的offset信息,必然引入复杂的数据结构,造成资源浪费。
而Kafka选择了不同的方式,每个consumer group保存自己的位移信息,那么只需要一个整数表示位置就够了;同时可以引入checkpoint机制定期持久化,简化了应答机制的实现。
5.3 位移管理
5.3.1 自动VS手动
Kafka默认定期自动提交位移(enable.auto.commit = true),当然可以选择手动提交位移实现。另外kafka会定期把group消费情况保存起来,做成一个offset map,如下图所示
5.3.2 位移提交
老版本的位移是提交到zookeeper中,目录结构是/consumers/<group.id>/offsets/<topic>/<partitionId>,但是zookeeper其实并不适合进行大批量的读写操作,尤其是写操作。因此kafka提供了另一种解决方案:增加__consumer_offsets topic,将offset信息写入这个topic,摆脱对zookeeper的依赖(指保存offset这件事情)。__consumer_offsets中的消息保存了每个consumer group某一时刻提交的offset信息。依然以上图中的consumer group为例,格式大概如下
边栏推荐
- Useeffect, triggered when function components are mounted and unloaded
- QT implementation window gradually disappears qpropertyanimation+ progress bar
- Share an example of running dash application in raspberry pie.
- 计算时间差
- (lightoj - 1349) Aladdin and the optimal invitation (greed)
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- input 只能输入数字,限定输入
- Codeforces Round #798 (Div. 2)A~D
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- Some problems encountered in installing pytorch in windows11 CONDA
猜你喜欢
随机推荐
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
去掉input聚焦时的边框
300th weekly match - leetcode
读取和保存zarr文件
Raspberry pie 4B installation opencv3.4.0
(POJ - 3258) River hopper (two points)
(lightoj - 1349) Aladdin and the optimal invitation (greed)
2078. Two houses with different colors and the farthest distance
Research Report on market supply and demand and strategy of Chinese table lamp industry
Local visualization tools are connected to redis of Alibaba cloud CentOS server
< li> dot style list style type
Useeffect, triggered when function components are mounted and unloaded
Browser print margin, default / borderless, full 1 page A4
分享一个在树莓派运行dash应用的实例。
Share an example of running dash application in raspberry pie.
Codeforces - 1526C1&&C2 - Potions
Sanic异步框架真的这么强吗?实践中找真理
Summary of game theory
Pytorch extract skeleton (differentiable)
Sword finger offer II 019 Delete at most one character to get a palindrome







