当前位置:网站首页>第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为例,格式大概如下
边栏推荐
- Codeforces Round #801 (Div. 2)A~C
- QT simulates mouse events and realizes clicking, double clicking, moving and dragging
- input 只能输入数字,限定输入
- 860. Lemonade change
- OneForAll安装使用
- 解决Intel12代酷睿CPU单线程只给小核运行的问题
- SF smart logistics Campus Technology Challenge (no T4)
- (lightoj - 1323) billiard balls (thinking)
- 新手必会的静态站点生成器——Gridsome
- Codeforces Round #801 (Div. 2)A~C
猜你喜欢

Log statistics (double pointer)

605. Planting flowers

读取和保存zarr文件

Tree of life (tree DP)

力扣——第298场周赛

Browser print margin, default / borderless, full 1 page A4

解决Intel12代酷睿CPU单线程只给小核运行的问题

Pytorch extract skeleton (differentiable)

1605. Sum the feasible matrix for a given row and column

简单尝试DeepFaceLab(DeepFake)的新AMP模型
随机推荐
分享一个在树莓派运行dash应用的实例。
Codeforces Round #799 (Div. 4)A~H
Codeforces - 1526C1&&C2 - Potions
AcWing:第58场周赛
Flask框架配置loguru日志库
409. Longest palindrome
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Market trend report, technical innovation and market forecast of double-sided foam tape in China
QT按钮点击切换QLineEdit焦点(含代码)
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
Kubernetes cluster deployment
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
Research Report on market supply and demand and strategy of China's four seasons tent industry
Advancedinstaller安装包自定义操作打开文件
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
It is forbidden to trigger onchange in antd upload beforeupload
Codeforces Round #802(Div. 2)A~D
AcWing——第55场周赛
Some problems encountered in installing pytorch in windows11 CONDA
Raspberry pie 4B installation opencv3.4.0