启动zookeeper之后,在三台机器上启动kafka
root@master:/opt/modules/kafka_2.11# bin/kafka-server-start.sh -daemon config/server.properties
创建三个副本,一个分区的topic,名称为test2
root@master:/opt/modules/kafka_2.11# bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 1 --topic test2
查看topic信息
root@master:/opt/modules/kafka_2.11# bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test2
Topic:test2 PartitionCount:1 ReplicationFactor:3 Configs:segment.bytes=1073741824
Topic: test2 Partition: 0 Leader: 0 Replicas: 0,1,2 Isr: 0,1,2
PartitionCount:1 分区数为1
ReplicationFactor:3 副本数为3
Replicas: 0,1,2 代表副本存放的broker.id
Isr: 0,1,2 现在活着的broker的id为0,1,2
Leader: 0 表示现在是id为0的broker负责分区的读写
下面杀死broker.id为0的Leader
停掉kafka
root@master:/opt/modules/kafka_2.11# bin/kafka-server-stop.sh
再次查看topic信息
root@slave01:/opt/modules/kafka_2.11# bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test2
Topic:test2 PartitionCount:1 ReplicationFactor:3 Configs:segment.bytes=1073741824
Topic: test2 Partition: 0 Leader: 1 Replicas: 0,1,2 Isr: 1,2
可以看到,Leader已经变成id为1的机器,活着的只有1,2了
当只留下一个kafka节点时
root@slave02:/opt/modules/kafka_2.11# bin/kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic test2
Topic:test2 PartitionCount:1 ReplicationFactor:3 Configs:segment.bytes=1073741824
Topic: test2 Partition: 0 Leader: 2 Replicas: 0,1,2 Isr: 2
kafka依然运行