${KAFKA_BROKERS}
表示 kafka 的连接地址,${ZOOKEEPER_CONNECT}
表示 zk 的连接地址,需要替换成自己的实际 ip 地址kafka-topics.sh --create --zookeeper ${ZOOKEEPER_CONNECT} --replication-factor 1 --partitions 3 --topic test-topic-update
查看 topic 详情
kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --describe --topic test-topic-update
总共是六个 kafka 节点,三分区一副本,分散在三个不同的 kafka 节点
Topic:test-topic-update PartitionCount:3 ReplicationFactor:1 Configs:segment.bytes=1073741824
Topic: test-topic-update Partition: 0 Leader: 5 Replicas: 5 Isr: 5
Topic: test-topic-update Partition: 1 Leader: 1 Replicas: 1 Isr: 1
Topic: test-topic-update Partition: 2 Leader: 0 Replicas: 0 Isr: 0
分区(Partition)
:
领导者(Leader)
:
副本(Replicas)
:
同步副本集(In-Sync Replicas,ISR)
:
kafka-verifiable-producer.sh --broker-list ${KAFKA_BROKERS} --topic test-topic-update --max-messages 300
--describe
来验证kafka-console-consumer.sh --bootstrap-server ${KAFKA_BROKERS} --topic test-topic-update --group test-topic-update-group
查看消费组内的 topic 消费情况
kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group
目前三百条都被消费了,使用上面的生产数据的命令,再生产300条,模拟 topic 有数据的场景
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test-topic-update-group test-topic-update 2 100 100 0 - - -
test-topic-update-group test-topic-update 0 100 100 0 - - -
test-topic-update-group test-topic-update 1 100 100 0 - - -
生产完数据后,再次查看,返回结果如下
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test-topic-update-group test-topic-update 2 100 200 100 - - -
test-topic-update-group test-topic-update 0 100 200 100 - - -
test-topic-update-group test-topic-update 1 100 200 100 - - -
--alter
就能实现,这里将原来的 3 分区改成 12 分区kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --alter --topic test-topic-update --partitions 12
查看 topic 情况
kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --describe --topic test-topic-update
可以看到,分区已经更新成 12 个了,也可以看出,kafka 在动态增加分区的时候,是均分的,都会按照类似下面的 5-1-0-3-2-4 这样的顺序去均分(当然,前提是分区数和节点数是倍数关系)
Topic:test-topic-update PartitionCount:12 ReplicationFactor:1 Configs:segment.bytes=1073741824
Topic: test-topic-update Partition: 0 Leader: 5 Replicas: 5 Isr: 5
Topic: test-topic-update Partition: 1 Leader: 1 Replicas: 1 Isr: 1
Topic: test-topic-update Partition: 2 Leader: 0 Replicas: 0 Isr: 0
Topic: test-topic-update Partition: 3 Leader: 3 Replicas: 3 Isr: 3
Topic: test-topic-update Partition: 4 Leader: 2 Replicas: 2 Isr: 2
Topic: test-topic-update Partition: 5 Leader: 4 Replicas: 4 Isr: 4
Topic: test-topic-update Partition: 6 Leader: 5 Replicas: 5 Isr: 5
Topic: test-topic-update Partition: 7 Leader: 1 Replicas: 1 Isr: 1
Topic: test-topic-update Partition: 8 Leader: 0 Replicas: 0 Isr: 0
Topic: test-topic-update Partition: 9 Leader: 3 Replicas: 3 Isr: 3
Topic: test-topic-update Partition: 10 Leader: 2 Replicas: 2 Isr: 2
Topic: test-topic-update Partition: 11 Leader: 4 Replicas: 4 Isr: 4
查看消费组内的分区情况
kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group
因为没有新数据进入,也没有消费旧数据,此时还是显示的原先的信息
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test-topic-update-group test-topic-update 2 100 200 100 - - -
test-topic-update-group test-topic-update 0 100 200 100 - - -
test-topic-update-group test-topic-update 1 100 200 100 - - -
将未消费的 300 条数据进行消费
kafka-console-consumer.sh --bootstrap-server ${KAFKA_BROKERS} --topic test-topic-update --group test-topic-update-group --max-messages 300
消费完成后,再次查看消费组的情况
kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group
此时就变成正常的 12 分区了
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test-topic-update-group test-topic-update 0 100 100 0 - - -
test-topic-update-group test-topic-update 7 0 0 0 - - -
test-topic-update-group test-topic-update 5 0 0 0 - - -
test-topic-update-group test-topic-update 1 100 100 0 - - -
test-topic-update-group test-topic-update 6 0 0 0 - - -
test-topic-update-group test-topic-update 2 100 100 0 - - -
test-topic-update-group test-topic-update 3 0 0 0 - - -
test-topic-update-group test-topic-update 10 0 0 0 - - -
test-topic-update-group test-topic-update 9 0 0 0 - - -
test-topic-update-group test-topic-update 8 0 0 0 - - -
test-topic-update-group test-topic-update 11 0 0 0 - - -
test-topic-update-group test-topic-update 4 0 0 0 - - -
这里为了方便验证,我把 topic 删了后重建了,下面这个删除 topic 的命令,大家别随意执行,会删除数据的
kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --delete --topic test-topic-update
kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --alter --topic test-topic-update --partitions 12
未生产新数据的时候,查看消费者组的信息同样是没有更新分区信息
kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group
此时,手动使用命令模拟新数据进来
kafka-verifiable-producer.sh --broker-list ${KAFKA_BROKERS} --topic test-topic-update --max-messages 100
通过命令查看消费者组的情况
kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group
此时显示的是老分区,而且只显示了 8+8+9=25 条数据
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test-topic-update-group test-topic-update 2 100 108 8 - - -
test-topic-update-group test-topic-update 0 100 108 8 - - -
test-topic-update-group test-topic-update 1 100 109 9 - - -
手动消费一下数据试试
kafka-console-consumer.sh --bootstrap-server ${KAFKA_BROKERS} --topic test-topic-update --group test-topic-update-group --max-messages 100
发现返回的信息里面,只显示25条数据
kafka-consumer-groups.sh --bootstrap-server ${KAFKA_BROKERS} --describe --group test-topic-update-group
但是观察消费者组的情况,显示的是都消费了,看起来,应该是和 topic 加入新消费者组的情况一样,不展示,但实际消费数据了(这块是个人的理解,具体的原理需要有兴趣的大佬深究一下,希望能赐教带我飞)
GROUP TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test-topic-update-group test-topic-update 0 108 108 0 - - -
test-topic-update-group test-topic-update 7 9 9 0 - - -
test-topic-update-group test-topic-update 5 8 8 0 - - -
test-topic-update-group test-topic-update 1 109 109 0 - - -
test-topic-update-group test-topic-update 6 9 9 0 - - -
test-topic-update-group test-topic-update 2 108 108 0 - - -
test-topic-update-group test-topic-update 3 8 8 0 - - -
test-topic-update-group test-topic-update 10 8 8 0 - - -
test-topic-update-group test-topic-update 9 8 8 0 - - -
test-topic-update-group test-topic-update 8 8 8 0 - - -
test-topic-update-group test-topic-update 11 8 8 0 - - -
test-topic-update-group test-topic-update 4 9 9 0 - - -
kafka-reassign-partitions.sh
是 Kafka 提供的命令行工具,用于重新分配主题分区的副本。这个工具允许你重新定义主题分区副本的分布,以实现负载均衡、故障恢复或集群扩展等目的之前的 topic 是 1 副本,12 分区,按照之前的 5-1-0-3-2-4 的顺序来分配第一个副本,然后按照 4-3-2-0-1-5 的顺序来分配第二个副本,我这里的 json 文件就命名为:add_rep_test_topic_update.json,大家可以以自己实际来命名
{"version":1, "partitions":[
{"topic":"test-topic-update","partition":0,"replicas":[5,4]},
{"topic":"test-topic-update","partition":1,"replicas":[1,3]},
{"topic":"test-topic-update","partition":2,"replicas":[0,2]},
{"topic":"test-topic-update","partition":3,"replicas":[3,0]},
{"topic":"test-topic-update","partition":4,"replicas":[2,1]},
{"topic":"test-topic-update","partition":5,"replicas":[4,5]},
{"topic":"test-topic-update","partition":6,"replicas":[5,4]},
{"topic":"test-topic-update","partition":7,"replicas":[1,3]},
{"topic":"test-topic-update","partition":8,"replicas":[0,2]},
{"topic":"test-topic-update","partition":9,"replicas":[3,0]},
{"topic":"test-topic-update","partition":10,"replicas":[2,1]},
{"topic":"test-topic-update","partition":11,"replicas":[4,5]}]
}
kafka-reassign-partitions.sh --zookeeper ${ZOOKEEPER_CONNECT} --execute --reassignment-json-file add_rep_test_topic_update.json
kafka-reassign-partitions.sh --zookeeper ${ZOOKEEPER_CONNECT} --verify --reassignment-json-file add_rep_test_topic_update.json
通过命令返回的内容,可以看出都成功了
Reassignment of partition test-topic-update-0 completed successfully
Reassignment of partition test-topic-update-7 completed successfully
Reassignment of partition test-topic-update-5 completed successfully
Reassignment of partition test-topic-update-1 completed successfully
Reassignment of partition test-topic-update-6 completed successfully
Reassignment of partition test-topic-update-2 completed successfully
Reassignment of partition test-topic-update-3 completed successfully
Reassignment of partition test-topic-update-10 completed successfully
Reassignment of partition test-topic-update-9 completed successfully
Reassignment of partition test-topic-update-8 completed successfully
Reassignment of partition test-topic-update-11 completed successfully
Reassignment of partition test-topic-update-4 completed successfully
kafka-topics.sh --bootstrap-server ${KAFKA_BROKERS} --describe --topic test-topic-update
现在的 topic 变成了 12 分区,2 副本的状态了
Topic:test-topic-update PartitionCount:12 ReplicationFactor:2 Configs:segment.bytes=1073741824
Topic: test-topic-update Partition: 0 Leader: 5 Replicas: 5,4 Isr: 5,4
Topic: test-topic-update Partition: 1 Leader: 1 Replicas: 1,3 Isr: 3,1
Topic: test-topic-update Partition: 2 Leader: 0 Replicas: 0,2 Isr: 0,2
Topic: test-topic-update Partition: 3 Leader: 3 Replicas: 3,0 Isr: 3,0
Topic: test-topic-update Partition: 4 Leader: 1 Replicas: 2,1 Isr: 1,2
Topic: test-topic-update Partition: 5 Leader: 4 Replicas: 4,5 Isr: 5,4
Topic: test-topic-update Partition: 6 Leader: 5 Replicas: 5,4 Isr: 4,5
Topic: test-topic-update Partition: 7 Leader: 1 Replicas: 1,3 Isr: 1,3
Topic: test-topic-update Partition: 8 Leader: 0 Replicas: 0,2 Isr: 0,2
Topic: test-topic-update Partition: 9 Leader: 3 Replicas: 3,0 Isr: 0,3
Topic: test-topic-update Partition: 10 Leader: 1 Replicas: 2,1 Isr: 1,2
Topic: test-topic-update Partition: 11 Leader: 4 Replicas: 4,5 Isr: 4,5