kafka中partition和消費者對應關係

1個partition只能被同組的一個consumer消費,同組的consumer則起到均衡效果算法

消費者多於partition

topic: test 只有一個partition
建立一個topic——test,bootstrap

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

在g2組中啓動兩個consumer,atom

1. bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning --consumer.config config/consumer_g2.properties
2. bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning --consumer.config config/consumer_g2.properties

消費者數量爲2大於partition數量1,此時partition和消費者進程對應關係以下:spa

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group g2
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test 0 9 9 0 consumer-1-4a2a4aa8-32f4-4904-9c16-1c0bdf7128a2 /127.0.0.1 consumer-1
- - - - - consumer-1-fd7b120f-fd21-4e07-8c23-87b71c1ee8a5 /127.0.0.1 consumer-1

 

消費者consumer-1-fd7b120f-fd21-4e07-8c23-87b71c1ee8a5無對應的partition。
用圖表示爲
code

 
生產者消費者對應關係1.jpg

如上圖,向test發送消息:1,2, 3,4,5,6,7,8,9
只有C1能接收到消息,C2則不能接收到消息, 即同一個partition內的消息只能被同一個組中的一個consumer消費。當消費者數量多於partition的數量時,多餘的消費者空閒。
也就是說若是隻有一個partition你在同一組啓動多少個consumer都沒用,partition的數量決定了此topic在同一組中被可被均衡的程度,例如partition=4,則可在同一組中被最多4個consumer均衡消費。

 

消費者少於和等於partition

topic:test2包含3個partitionorm

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic test2

開始時,在g3組中啓動2個consumer,server

1.bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test2 --from-beginning --consumer.config config/consumer_g3.properties
2.bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test2 --from-beginning --consumer.config config/consumer_g3.properties

則對應關係以下:xml

TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test2 0 8 8 0 consumer-1-8b872ef7-a2f0-4bd3-b2a8-7b26e4d8ab2c /127.0.0.1 consumer-1
test2 1 7 7 0 consumer-1-8b872ef7-a2f0-4bd3-b2a8-7b26e4d8ab2c /127.0.0.1 consumer-1
test2 2 8 8 0 consumer-1-f362847d-1094-4895-ad8b-1e1f1c88936c /127.0.0.1 consumer-1

其中,consumer-1-8b872ef7-a2f0-4bd3-b2a8-7b26e4d8ab2c對應了2個partition
用圖表示爲:
blog

 
生產者消費者對應關係2.jpg

消費者數量2小於partition的數量3,此時,向test2發送消息1,2,3,4,5,6,7,8,9
C1接收到1,3,4,6,7,9
C2接收到2,5,8
此時P一、P2對對應C1,即多個partition對應一個消費者,C1接收到消息量是C2的兩倍
而後,在g3組中再啓動一個消費者,使得消費者數量爲3等於topic2中partition的數量
3.bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test2 --from-beginning --consumer.config config/consumer_g3.properties

對應關係以下:排序

TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test2 0 8 8 0 consumer-1-8b872ef7-a2f0-4bd3-b2a8-7b26e4d8ab2c /127.0.0.1 consumer-1
test2 1 7 7 0 consumer-1-ab472ed5-de11-4e56-863a-67bf3a3cc36a /127.0.0.1 consumer-1
test2 2 8 8 0 consumer-1-f362847d-1094-4895-ad8b-1e1f1c88936c /127.0.0.1 consumer-1

此時,partition和消費者是一對一關係,向test2發送消息1,2,3,4,5,6,7,8,9
C1接收到了:2,5,8
C2接收到了:3,6,9
C3接收到了:1,4,7
C1,C2,C3均分了test2的全部消息,即消息在同一個組之間的消費者之間均分了!

多個消費者組

啓動g4組,僅包含一個消費者C1,消費topic2的消息,此時消費端有兩個消費者組

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test2 --from-beginning --consumer.config config/consumer_g4.properties --delete-consumer-offsets

g4組的C1的對應了test2的全部partition:

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group g4
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test2 0 36 36 0 consumer-1-befc9234-260d-4ad3-b283-b67a2bf446ca /127.0.0.1 consumer-1
test2 1 35 35 0 consumer-1-befc9234-260d-4ad3-b283-b67a2bf446ca /127.0.0.1 consumer-1
test2 2 36 36 0 consumer-1-befc9234-260d-4ad3-b283-b67a2bf446ca /127.0.0.1 consumer-1

用圖表示爲

 
生產者消費者對應關係3.jpg

如上圖,向test2發送消息1,2,3,4,5,6,7,8,9
那麼g3組各個消費者及g4組的消費者接收到的消息是怎樣地呢?歡迎思考!!
答案:
消息被g3組的消費者均分,g4組的消費者在接收到了全部的消息。
g3組:
C1接收到了:2,5,8
C2接收到了:3,6,9
C3接收到了:1,4,7
g4組:
C1接收到了:1,2,3,4,5,6,7,8,9
啓動多個組,則會使同一個消息被消費屢次
 

Consumer Rebalance的算法以下:

  • 將目標Topic下的全部Partirtion排序,存於PT
  • 對某Consumer Group下全部Consumer排序,存CG,第ii個Consumer記爲Ci
  • N=size(PT)/size(CG),向上取整
  • 解除Ci對原來分配的Partition的消費權(i從0開始)
  • 將第i∗Ni+1N1個Partition分配給Ci
 
http://www.jianshu.com/nb/8254699
相關文章
相關標籤/搜索