Kafka的原理及安装
1. Kafka原理
2. Kafka安装
\1) 上传到虚拟机,解压:
[root@cent71 module]# tar -zxf kafka_2.11-0.10.2.2.tgz.gz -C /opt/software/
\2) 在安装目录下新建logs文件夹:
[root@cent71 kafka_2.11-0.10.2.2]# mkdir logs
\3) 修改配置文件config/server.properties:
#broker的全局唯一编号,不能重复
broker.id=1
#kafka运行日志存放的路径
log.dirs=/opt/software/kafka_2.11-0.10.2.2/logs
#配置连接Zookeeper集群地址
zookeeper.connect=cent71:2181,cent72:2181,cent73:2181
\4) 分发安装包:
[root@cent71 software]# scp -r kafka_2.11-0.10.2.2/ cent72:/opt/software/
[root@cent71 software]# scp -r kafka_2.11-0.10.2.2/ cent72:/opt/software/
\5) 修改broker.id:
Cent72上:
broker.id=1
cent73上:
broker.id=2
\6) 启动集群(启动前先启动zookeeper):
[root@cent71 kafka_2.11-0.10.2.2]# bin/kafka-server-start.sh config/server.properties
[root@cent72 kafka_2.11-0.10.2.2]# bin/kafka-server-start.sh config/server.properties
[root@cent73 kafka_2.11-0.10.2.2]# bin/kafka-server-start.sh config/server.properties
\7) 关闭集群:
[root@cent71 kafka_2.11-0.10.2.2]# bin/kafka-server-stop.sh
[root@cent72 kafka_2.11-0.10.2.2]# bin/kafka-server-stop.sh
[root@cent73 kafka_2.11-0.10.2.2]# bin/kafka-server-stop.sh
3. Kafka测试
\1) 在主节点上创建一个topic,名为test,并设置备份数是3,分区数是3:
[root@cent71 kafka_2.11-0.10.2.2]# bin/kafka-topics.sh -zookeeper cent71:2181,cent72:2181,cent73:2181 -topic test -replication-factor 3 -partitions 3 --create
\2) 查看当前topic:
[root@cent71 kafka_2.11-0.10.2.2]# bin/kafka-topics.sh -zookeeper cent71:2181,cent72:2181,cent73:2181 --list
test
\3) 在主节点上创建一个producer:
[root@cent71 kafka_2.11-0.10.2.2]# bin/kafka-console-producer.sh --broker-list cent71:9092 --topic test
\4) 在从节点上创建一个consumer:
[root@cent72 kafka_2.11-0.10.2.2]# bin/kafka-console-consumer.sh --zookeeper cent71:2181,cent72:2181,cent73:2181 --topic test --from-beginning
\5) 在主节点上输入消息,在从节点上查看:
[root@cent71 kafka_2.11-0.10.2.2]# bin/kafka-console-producer.sh --broker-list cent71:9092 --topic test
^[[Bh
hige
[root@cent72 kafka_2.11-0.10.2.2]# bin/kafka-console-consumer.sh --zookeeper cent71:2181,cent72:2181,cent73:2181 --topic test --from-beginning
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
h
hige
\6) 删除topic:
[root@cent71 kafka_2.11-0.10.2.2]# bin/kafka-topics.sh -zookeeper cent71:2181,cent72:2181,cent73:2181 -topic test --delete