一、目标
- 搭建一个3个broker的kafka集群。
- 验证kafka集群的高可用性
二、配置多集群
由于只有一台机器,于是只能通过启动3个端口,进而启动3个broker
- 目录文件: /usr/local/etc/kafka/server.properties
再复制两份。server-1.properties、server-2.properties,修改端口号、borker.id 、消息保存目录 log.dirs
加上server.properties中的broker.id=0,我们就有了3个broker
# server.properties
broker.id=0
listeners=PLAINTEXT://:9092
log.dirs=/usr/local/var/lib/kafka-logs
# server1.properties
broker.id=1
listeners=PLAINTEXT://:9093
log.dirs=/usr/local/var/lib/kafka-logs-1
# server2.properties
broker.id=2
listeners=PLAINTEXT://:9094
log.dirs=/usr/local/var/lib/kafka-logs-2
三、启动 [zookeeper-server-start]
# 启动 zk
zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties &
# 启动 broker0
kafka-server-start /usr/local/etc/kafka/server.properties &
# 启动 broker1
kafka-server-start /usr/local/etc/kafka/server-1.properties &
# 启动 broker2
kafka-server-start /usr/local/etc/kafka/server-2.properties &
四、创建 Topic [kafka-topics --create]
kafka-topics --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic testCluster
- 复制因子 replication-factor 为3,表明有3个副本
- partitions 为1,表明该主题只有一个分区Partition
4.1 查看 Topic 的状态 [kafka-topics --describe]
kafka-topics --describe --zookeeper localhost:2181 --topic testCluster
$ kafka-topics --describe --zookeeper localhost:2181 --topic testCluster
Topic:testCluster PartitionCount:1 ReplicationFactor:3 Configs:
Topic: testCluster Partition: 0 Leader: 2 Replicas: 2,1,0 Isr: 2,1,0
PartitionCount:分区数量1
ReplicationFactor: 副本复制因子3,也就是有3个副本
Partition: 分区下标
Leader:分区leader 位于第二个broker上
Replicas:有3个副本2,1,0 ,从副本只进行数据的同步复制
Isr:In-Sync Replica 同步的副本,表明3个副本都是同步副本,也就是在broker奔溃的时候,能上位。
4.2 检测高可用
- 找到 broker2的进程号
ps -ef | grep kafka
- 杀掉进程 ,模拟Leader挂掉的情况
kill -9 进程号
- 查看topic现状:
$ kafka-topics --describe --zookeeper localhost:2181 --topic testCluster
Topic:testCluster PartitionCount:1 ReplicationFactor:3 Configs:
Topic: testCluster Partition: 0 Leader: 1 Replicas: 2,1,0 Isr: 1,0
对比上方可以知道,Leader目前变成了1,ISR也即是同步状态的副本,只有1跟0这俩副本。
- 消费者能够看到warn警告,但是生产者继续生产消息,消费者依然能够接收到,这就是高可用了。
[2020-07-23 19:48:20,126] WARN [Consumer clientId=consumer-1, groupId=console-consumer-29596] Connection to node 2 (localhost/127.0.0.1:9094) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
- 重新启动 broker2
kafka-server-start /usr/local/etc/kafka/server-2.properties &
- 再查看目前 Topic的状态
$ kafka-topics --describe --zookeeper localhost:2181 --topic testCluster
Topic:testCluster PartitionCount:1 ReplicationFactor:3 Configs:
Topic: testCluster Partition: 0 Leader: 1 Replicas: 2,1,0 Isr: 1,0,2
可以看到,重新启动后,Leader依旧还是1,ISR(In-Sync-Replica)同步副本已经出现了2,说明目前3个副本都是同步的。
4.3 查看分区目录信息
从文件 /usr/local/etc/kafka/server.properties 内容中,可以看到,消息的保存路径是在:
log.dirs=/usr/local/var/lib/kafka-logs
五、生产消息 [kafka-console-producer]
kafka-console-producer --broker-list localhost:9092 --topic testCluster
六、消费消息 [kafka-console-consumer]
kafka-console-consumer --bootstrap-server localhost:9092 --topic testCluster --from-beginning
== 番外篇 ==
下一章节:【Kafka】消费者组 Consumer Group(三)
上一章节:【Kafka】MacOS入门安装使用 (一)