kafka(2)--多节点

来源:互联网 发布:java中cgi是什么 编辑:程序博客网 时间:2024/06/09 22:22

多节点集群只是比单节点集群多了一些代理实例,其他并没有什么太大的变化,本文以本机三个节点为例搭建kafka集群。

1. kafka程序准备

下载程序:wget http://mirror.bit.edu.cn/apache/kafka/0.10.2.0/kafka_2.11-0.10.2.0.tgz解压:tar -zxvf kafka_2.11-0.10.2.0.tgzcd kafka_2.11-0.10.2.0

在bin目录中可以看到操作kafka的各种脚本。
2. 启动zookeeper

bin/zookeeper-server-start.sh config/zookeeper.properties

3. 配置kafka文件
因为默认只有一个kafka服务,因此需要为另外两个节点添加配置文件,建议直接在默认配置文件基础上修改即可:

cp config/server.properties config/server-1.propertiescp config/server.properties config/server-2.properties

修改配置文件config/server-1.properties:

config/server-1.properties:    broker.id=1    listeners=PLAINTEXT://:9093    log.dir=/tmp/kafka-logs-1
修改配置文件config/server-2.properties:config/server-2.properties:    broker.id=2    listeners=PLAINTEXT://:9094    log.dir=/tmp/kafka-logs-2

broker.id属性是集群中的每个节点的唯一和永久的名字。需要重写端口和日志目录,因为我们都在同一台机器上运行这些代理,需要防止在同一端口上注册或覆盖彼此的数据。

4. 启动kafka服务

bin/kafka-server-start.sh config/server.propertiesbin/kafka-server-start.sh config/server-1.propertiesbin/kafka-server-start.sh config/server-2.properties

5. 创建topic
创建有三个副本因子的topic :

 bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic myTopic

此时3节点的kafka集群新建完成。

6. 查看kfaka节点信息

bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic myTopicTopic:myTopic   PartitionCount:1    ReplicationFactor:3 Configs:    Topic: myTopic  Partition: 0    Leader: 1   Replicas: 1,2,0 Isr: 1,2,0
  • “Leader”负责指定分区所有读取和写入的节点。
  • “Replicas”是此分区日志的节点列表集合,不管这些节点是否是领导者或者只是还活着(不在in-sync状态)。
  • “ISR”是一组”in-sync” 节点列表的集合。这个列表包括目前活着并跟leader保持同步的replicas,Isr 是Replicas的子集。

节点1是该主题的唯一分区中的leader。
对比单个kafka服务时的信息:

 bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic topic1Topic:topic1    PartitionCount:1    ReplicationFactor:1 Configs:    Topic: topic1   Partition: 0    Leader: 0   Replicas: 0 Isr: 0

从结果看单个 kafka 服务中只有服务0。

7. 启动生产者

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic myTopic...my mesasage1my message2

8. 启动消费者
bin/kafka-console-consumer.sh –bootstrap-server localhost:9092 –from-beginning –topic myTopic

my message1
my message2
此时已经收到生产者发送的消息。

9. 模拟leader节点宕机
可以直接kill -9 模拟kafka的leader节点宕机。
ps aux | grep server-1.properties
找到pid直接kill -9 杀死进程。

10. 查看kafka节点信息

bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic myTopicTopic:myTopic   PartitionCount:1    ReplicationFactor:3 Configs:    Topic: myTopic  Partition: 0    Leader: 2   Replicas: 1,2,0 Isr: 2,0

此时可以看到Leader已经切换为服务2

11. 验证消费消息

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic myTopic...my message1my message2

可以看到Leader切换后消费仍正常。

下一篇: kafka连接导入、导出数据

原创粉丝点击