hostname | zookeeper | Broker | Producer | Consume |
---|---|---|---|---|
centosone | ~ | ~ | ||
centostwo | ~ | ~ | ||
centosthree | ~ | ~ | ~ | |
centosfour | ~ |
kafka中的角色: Producer, Broker, Consume, zookeeper.
节点规划如上图所示
在centosone中进行配置, 配置好通过发送到其他的两台节点上。
1. 解压安装包:
[root@centosone software]# tar -zxvf kafka_2.11-1.0.1.tgz -C ../module/
2. 配置环境变量。
[root@centosone software]# vi /etc/profile
向profile文件中添加如下内容:
#KAFKA_HOME
export KAFKA_HOME=/opt/module/kafka_2.11-1.0.1/
export PATH=$PATH:$KAFKA_HOME/bin
3. 修改配置文件config/server.properties
[root@centostwo config]# pwd
/opt/module/kafka_2.11-1.0.1/config
修改server.properties文件。
[root@centostwo config]# vi server.properties
在server.properties文件中修改如下内容:
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=0 // 注意: 每台节点上的broker.id的值是不一样的。 在centosone是0,centostwo是1, centosthree是2
zookeeper.connect=centostwo:2181,centosthree:2181,centosfour:2181 // 将zookeeper.connect的值改为相对应的真实的zookeeper地址。
log.dirs=/opt/module/kafka_2.11-1.0.1/data // 改成你想要的路径。
4. 在centosone中可以写个shell脚本来启动, 就不用写很长的启动代码
[root@centosone kafka_2.11-1.0.1]# pwd
/opt/module/kafka_2.11-1.0.1
[root@centosone kafka_2.11-1.0.1]# vi startkafka.sh
向startkafka.sh中写入下面的命令:
nohup kafka-server-start.sh config/server.properties > /opt/module/kafka_2.11-1.0.1/kafka.log 2>&1 &
wq保存即可。
5. 在centosone中修改完配置文件之后就分发给其他节点上去。分发给centostwo,centosthree节点中。
scp -r 文件名 主机名:路径.
发送到centostwo节点上
[root@centosone module]# scp -r kafka_2.11-1.0.1/ centostwo:`pwd`
发送到centosthree节点上
[root@centosone module]# scp -r kafka_2.11-1.0.1/ centosthree:`pwd`;
6. 在centostwo中/opt/module/kafka_2.11-1.0.1下进入config目录下, 修改server.properties
[root@centostwo config]# vi server.properties
在server.properties文件中把broker.id改为1.
broker.id=1
7. 在centosthree中/opt/module/kafka_2.11-1.0.1下进入config目录下,修改server.properties
[root@centosthree config]# pwd
/opt/module/kafka_2.11-1.0.1/config
[root@centosthree config]# vi server.properties
在server.properties文件中把broker.id改为2.
broker.id=2
8. 启动kafka集群
先在centosone中启动kafka
[root@centosone kafka_2.11-1.0.1]# pwd
/opt/module/kafka_2.11-1.0.1
[root@centosone kafka_2.11-1.0.1]# ./startkafka.sh
在centostwo中启动kafka
[root@centostwo kafka_2.11-1.0.1]# pwd
/opt/module/kafka_2.11-1.0.1
[root@centostwo kafka_2.11-1.0.1]# ./startkafka.sh
在centosthree中启动kafka
[root@centosthree kafka_2.11-1.0.1]# pwd
/opt/module/kafka_2.11-1.0.
[root@centosthree kafka_2.11-1.0.1]# ./startkafka.sh
9. 查看后台进程
在centosone中查看后台进程
[root@centosone kafka_2.11-1.0.1]# jps
7588 Jps
7116 Kafka
在centostwo中查看后台进程
[root@centostwo kafka_2.11-1.0.1]# jps
7110 QuorumPeerMain
7165 Kafka
7566 Jps
在centosthree中查看后台进程
[root@centosthree kafka_2.11-1.0.1]# jps
7353 -- process information unavailable
7115 QuorumPeerMain
7597 Jps
7199 Kafka
10. Kafka命令行操作
查看当前服务器中的所有topic
kafka-topics.sh --zookeeper centostwo,centosthree,centosfour --list
创建topic
kafka.topics.sh --zookeeper centostwo,centosthree,centosfour --create --topic zhangshaojun --partitions 2 --replication-factor 2
删除topic
kafka.topics.sh --zookeeper centostwo,centosthree,centosfour --delete --topic zhangshaojun
11.模拟生产者和消费者
创建生产者
这生产者不用与zookeeper产生关系,只需要向kafka集群产生关系
格式: kafka-console-producer.sh --borker-list kafka集群主机名 --topic 主题名
创建生产者
[root@centosone config]# kafka-console-producer.sh --broker-list centosone:9092 centostwo:9092 centosthree:9092 --topic shaojun
注意: 这个写的是kafka集群的地址。 和端口号。 端口号是9092
创建消费者
注意: 在kafka0.9之前是消费者向zookeeper里获取数据, 0.9之后就是向kafka集群里获取数据,也就是在Borker里获取数据。
格式: kafka-console-consumer.sh --bootstrap-server kafka集群的主机名:9092 --topic 主题名
kafka-console-consumer.sh --zookeeper zookeeper集群的主机名 --topic 主题名
创建消费者, 向zookeeper里获取数据
[root@centosthree data]# kafka-console-consumer.sh --zookeeper centostwo,centosthree,centosfour --topic shaojun
创建消费者,向kafka集群里获取数据
[root@centostwo data]# kafka-console-consumer.sh --bootstrap-server centosone:9092 centostwo:9092 centosthree:9092 --topic shaojun
[root@centostwo data]# kafka-console-consumer.sh --bootstrap-server centosone:9092 centostwo:9092 centosthree:9092 --topic shaojun --from-beginning : 表示把 生产者前几次生产的消息也给在消费者中显示出来。