已复制
全屏展示
复制代码

Kafka 单机多 broker 部署

· 2 min read

即在一台机器上跑多个 broker,正常线上部署时,一台服务器应该只部署一个 broker。单机多 broker 的目的是为了测试学习 Kafka 时使用

# 解压并进入目录
tar -xzf kafka_2.11-2.3.0.tgz -C /opt/
cd kafka_2.11-2.3.0 


# 准备三个broker的配置文件
cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties
cp config/server.properties config/server-3.properties


# 分别修改三个配置文件
vim config/server-1.properties
broker.id=1
listeners=PLAINTEXT://:9092
log.dirs=/tmp/kafka-logs-1

vim config/server-2.properties
broker.id=2
listeners=PLAINTEXT://:9093
log.dirs=/tmp/kafka-logs-2

vim config/server-3.properties
broker.id=3
listeners=PLAINTEXT://:9094
log.dirs=/tmp/kafka-logs-3


# 启动zookeeper
nohup ./bin/zookeeper-server-start.sh config/zookeeper.properties &


# 启动kafka
nohup ./bin/kafka-server-start.sh config/server-1.properties &
nohup ./bin/kafka-server-start.sh config/server-2.properties &
nohup ./bin/kafka-server-start.sh config/server-3.properties &


# 创建 articleTopic 话题,包含1个分区,3个副本。
./bin/kafka-topics.sh --zookeeper localhost:2181 --create --replication-factor 3 --partitions 1 --topic articleTopic
Created topic articleTopic.


# 查看topic详情
./bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic articleTopic
Topic:articleTopic      PartitionCount:1        ReplicationFactor:3     Configs:
        Topic: articleTopic     Partition: 0    Leader: 2       Replicas: 2,3,1 Isr: 2,3,1
# 第一行是所有分区的摘要,其次,每一行提供一个分区信息,因为我们只有一个分区,所以只有一行。
# 1.Partition:  分区编号。
# 2.Leader:    leader所在broker的id,该节点负责该分区的所有的读和写,每个节点的 leader 都是随机选择的。
# 3.Replicas:  备份的节点列表id,无论该节点是否是 leader 或者目前是否还活着,不管node死活,只是列出信息而已。
# 4.Isr:       同步备份的节点列表id,也就是活着的节点并且正在同步 leader。


# 生产消息,然后消费消息
./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic articleTopic
./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic articleTopic


# 杀掉节点2
ps aux | grep server-2.properties | awk '{print $2}' |xargs kill


# 查看Topic详情,可以看到,Leader从2变成了3,Isr列表中少了2。
./bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic articleTopic
Topic:articleTopic      PartitionCount:1        ReplicationFactor:3     Configs:
        Topic: articleTopic     Partition: 0    Leader: 3       Replicas: 2,3,1 Isr: 3,1


# 节点2死掉以后消费消息,看是否成功。
./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic articleTopic

🔗

文章推荐