kafka集群搭建和操作
kafka集群搭建
环境:
192.168.28.131:zookeeper,kafka 192.168.28.132:zookeeper,kafka 192.168.28.133:zookeeper,kafka
jdk1.8
zookeeper集群搭建
第一步,下载kafka和zookeeper包(注意:在kafka2.8以后可以不需要zookeeper)
wget -c https://dlcdn.apache.org/kafka/3.1.0/kafka_2.12-3.1.0.tgz --no-check-certificate wget -c https://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
第二步,编辑zoo.cfg配置文件
tickTime=2000 initLimit=5 syncLimit=2 dataDir=/data/zookeeper/data clientPort=2181 server.1=192.168.28.131:2888:3888 server.2=192.168.28.132:2888:3888 server.3=192.168.28.131:2888:3888
第三补,添加myid
cat /data/zookeeper/data/myid 3
第四步,在另外两台机器重复第二步和第三步
第五步,查看zookeeper集群状态(3个节点都看一下)
[root@node3 data]# /opt/zookeeper/bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Mode: leader
kafka集群部署
第一步,编辑kafka配置文件(3台机器都要配置),注意红色字体根据需要修改
[root@node3 config]# cat server.properties broker.id=131 port=9092 host.name=192.168.28.131 num.network.threads=3 num.io.threads=8 socket.send.buffer.bytes=102400 socket.receive.buffer.bytes=102400 socket.request.max.bytes=104857600 log.dirs=/data/kafka-log num.partitions=9 num.recovery.threads.per.data.dir=1 log.retention.hours=72 log.segment.bytes=1073741824 log.retention.check.interval.ms=300000 log.cleaner.enable=true log.cleanup.policy=delete zookeeper.connect=192.168.28.133:2181,192.168.28.132:2181,192.168.28.131:2181 zookeeper.connection.timeout.ms=6000 delete.topic.enable=true broker.list=192.168.28.133:9092,192.168.28.132:9092,192.168.28.131:9092
第二步,3台机器启动kafka服务
./kafka-server-start.sh -daemon ../config/server.properties
第三步,查看是否ok
/opt/zookeeper/bin/zkCli.sh ..... [zk: localhost:2181(CONNECTED) 0] ls /brokers/ids [132, 133, 131]
日常操作
查看topic
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
创建topic
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --topic test2 --replication-factor 2 --partitions 2
有了主题了,开始创建生产者
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test1
创建消费者接收消息,其中 --from-beginning代表从头开始接收消息,也就是说在没有开启消费者之前的消息,也可以收到
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test1 --from-beginning
查看group
./kafka-consumer-groups.sh --list --zookeeper IP:2181