1. 程式人生 > >kafka中partition和消費者對應關係

kafka中partition和消費者對應關係

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

消費者多於partition

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

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

在g2組中啟動兩個consumer,

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和消費者程序對應關係如下:

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。
用圖表示為


生產者消費者對應關係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個partition

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

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

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

則對應關係如下:

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
用圖表示為:


生產者消費者對應關係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
此時P1、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
啟動多個組,則會使同一個訊息被消費多次