Kafka集群
Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写。Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者在网站中的所有动作流数据。
Kafka 安装配置
1. 安装Kafka master节点
cd /home/meizhangzheng
tar -zxvf kafka_2.13-2.5.0.tgz -C data/
cd data
mv kafka_2.13-2.5.0 kafka-2.5.0
mkdir kafka-logs
2.配置参数
(1)修改 vi server.properties
broker.id=0
log.dirs=/home/meizhangzheng/data/kafka-logs
zookeeper.connect=master:2181,slave1:2181,slave2:2181
(2)修改 .bash_profile
export KAFKA_HOME=/home/meizhangzheng/data/kafka-2.5.0
export PATH=$KAFKA_HOME/bin:$PATH
3. 分发到各个节点 ( 注意目录 )
[meizhangzheng@master data]$ scp -r /home/meizhangzheng/data/kafka-2.5.0/ ~/data/kafka-logs/ meizhangzheng@slave1:~/data
[meizhangzheng@master data]$ scp -r /home/meizhangzheng/data/kafka-2.5.0/ ~/data/kafka-logs/ meizhangzheng@slave2:~/data
[meizhangzheng@master data]$ scp -r ~/.bash_profile meizhangzheng@slave1:~/
[meizhangzheng@master data]$ scp -r ~/.bash_profile meizhangzheng@slave2:~/
cd ~
source .bash_profile
4. 修改其他配置
master 节点: ssh meizhangzheng@slave1;
目录:/home/meizhangzheng/data/kafka-2.5.0/config
(1)修改 vi server.properties
slave1: broker.id=1
slave2: broker.id=2
5. 启动kafka,各个节点都需要启动
(需要先启动zookeeper,集群组件依赖,单机部署时没必要启动)
[meizhangzheng@master ~]$ kafka-server-start.sh ~/data/kafka-2.5.0/config/server.properties
[meizhangzheng@slave1 ~]$ kafka-server-start.sh ~/data/kafka-2.5.0/config/server.properties
[meizhangzheng@slave2 ~]$ kafka-server-start.sh ~/data/kafka-2.5.0/config/server.properties
6. kafka 的使用 (安装kafka-manager 可查看监控信息)
(1)创建topic 备份数3个、分区数3个
kafka-topics.sh --create --zookeeper master:2181,slave1:2181,slave2:2181 --replication-factor 3 --partitions 3 --topic test
Created topic test.
(2) 查看topic
kafka-topics.sh --list --zookeeper master:2181,slave1:2181,slave2:2181
(3) 生产数据
kafka-console-producer.sh --broker-list master:9092,slave1:9092,slave2:9092 --topic test
输入:
a
b
c
d
e
(4) 验证消息生成成功
kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list master:9092,slave1:9092,slave2:9092 --topic test --time -1
结果:
test:0:1
test:1:4
test:2:0
分析:总共5条消息,1条分配到分区0(master)、另外4条被分配到分区1
(5)topic消费
kafka-console-consumer.sh --bootstrap-server master:9092,slave1:9092,slave2:9092 --topic test --from-beginning
显示结果:
[meizhangzheng@master ~]$ kafka-console-consumer.sh --bootstrap-server master:9092,slave1:9092,slave2:9092 --topic test --from-beginning
a
b
d
e
c