1、去官网下载zk和kafka
下载地址
kafka版本是 kafka_2.12-2.3.0.tgz,下载地址如下
http://archive.apache.org/dist/kafka/2.3.0/kafka_2.12-2.3.0.tgz
zk版本是apache-zookeeper-3.5.5-bin.tar.gz,下载地址如下:
https://archive.apache.org/dist/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz
操作步骤:
#下载kafka到linux上
wget http://archive.apache.org/dist/kafka/2.3.0/kafka_2.12-2.3.0.tgz
#下载zookeeper到linux上
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz
2、部署zookeeper
#1、解压
tar -zxvf apache-zookeeper-3.5.5-bin.tar.gz
#2、移动到zookeeper目录,方便管理
mv apache-zookeeper-3.5.5-bin zookeeper
#3、cd到/data/zookeeper/conf目录
#4、刚开始没有zoo.cfg目录,复制一份
cp zoo_sample.cfg zoo.cfg
#5、修改以下内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper/data
dataLogDir=/data/zookeeper/log
clientPort=2181
#6、然后在/data/zookeeper/目录里面创建data和log两个文件夹
cd /data/zookeeper
mkdir data log
#7、启动zookeeper
cd /data/zookeeper/bin
./zkServer.sh start
3、部署kafka
#1、解压
tar -zxvf kafka_2.12-2.3.0.tgz
#移动到kafka目录
mv kafka_2.12-2.3.0 kafka
#2、更改配置文件
cd config
vim server.properties
新增或者修改如下配置
host.name=阿里云内网IP
port=9092
advertised.host.name=阿里云外网IP
zookeeper.connect=部署zk机器的IP:2181
listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://localhost:9092
#3、启动kafka
cd /data/kafka_2.12-2.3.0
./bin/kafka-server-start.sh config/server.properties &
#4、注意:使用exit退出,直接关闭的话,kafka进程就么没有了
exit
4、kafka的使用
创建kafka主题:
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
显示kafka所有主题:
bin/kafka-topics.sh -list -zookeeper localhost:2181
创建kafka生产者:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
生产者创建后,可以输入内容
创建kafka消费者:
./kafka-console-consumer.sh --bootstrap-server 127.0.0.1:9092 --topic test --from-beginning
另起一个窗口,创建消费者,可以收到生产者发送的主题,如图
解释:
–zookeeper:后面接的是你配置的zookeeper地址