1個(gè)partition只能被同組的一個(gè)consumer消費(fèi),同組的consumer則起到均衡效果
消費(fèi)者多于partition
topic: test 只有一個(gè)partition
創(chuàng)建一個(gè)topic——test,
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
在g2組中啟動(dòng)兩個(gè)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
消費(fèi)者數(shù)量為2大于partition數(shù)量1,此時(shí)partition和消費(fèi)者進(jìn)程對(duì)應(yīng)關(guān)系如下:
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
消費(fèi)者consumer-1-fd7b120f-fd21-4e07-8c23-87b71c1ee8a5無(wú)對(duì)應(yīng)的partition。
用圖表示為

如上圖,向test發(fā)送消息:1,2, 3,4,5,6,7,8,9
只有C1能接收到消息,C2則不能接收到消息,即同一個(gè)partition內(nèi)的消息只能被同一個(gè)組中的一個(gè)consumer消費(fèi)。當(dāng)消費(fèi)者數(shù)量多于partition的數(shù)量時(shí),多余的消費(fèi)者空閑。
也就是說(shuō)如果只有一個(gè)partition你在同一組啟動(dòng)多少個(gè)consumer都沒(méi)用,partition的數(shù)量決定了此topic在同一組中被可被均衡的程度,例如partition=4,則可在同一組中被最多4個(gè)consumer均衡消費(fèi)。
消費(fèi)者少于和等于partition
topic:test2包含3個(gè)partition
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic test2
開(kāi)始時(shí),在g3組中啟動(dòng)2個(gè)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
則對(duì)應(yīng)關(guān)系如下:
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對(duì)應(yīng)了2個(gè)partition
用圖表示為:

消費(fèi)者數(shù)量2小于partition的數(shù)量3,此時(shí),向test2發(fā)送消息1,2,3,4,5,6,7,8,9
C1接收到1,3,4,6,7,9
C2接收到2,5,8
此時(shí)P1、P2對(duì)對(duì)應(yīng)C1,即多個(gè)partition對(duì)應(yīng)一個(gè)消費(fèi)者,C1接收到消息量是C2的兩倍
然后,在g3組中再啟動(dòng)一個(gè)消費(fèi)者,使得消費(fèi)者數(shù)量為3等于topic2中partition的數(shù)量
3.bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test2 --from-beginning --consumer.config config/consumer_g3.properties
對(duì)應(yīng)關(guān)系如下:
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
此時(shí),partition和消費(fèi)者是一對(duì)一關(guān)系,向test2發(fā)送消息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的所有消息,即消息在同一個(gè)組之間的消費(fèi)者之間均分了!
多個(gè)消費(fèi)者組
啟動(dòng)g4組,僅包含一個(gè)消費(fèi)者C1,消費(fèi)topic2的消息,此時(shí)消費(fèi)端有兩個(gè)消費(fèi)者組
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test2 --from-beginning --consumer.config config/consumer_g4.properties --delete-consumer-offsets
g4組的C1的對(duì)應(yīng)了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
用圖表示為

如上圖,向test2發(fā)送消息1,2,3,4,5,6,7,8,9
那么g3組各個(gè)消費(fèi)者及g4組的消費(fèi)者接收到的消息是怎樣地呢?歡迎思考??!
答案:
消息被g3組的消費(fèi)者均分,g4組的消費(fèi)者在接收到了所有的消息。
g3組:
C1接收到了:2,5,8
C2接收到了:3,6,9
C3接收到了:1,4,7
g4組:
C1接收到了:1,2,3,4,5,6,7,8,9
啟動(dòng)多個(gè)組,則會(huì)使同一個(gè)消息被消費(fèi)多次