官方源:https://kafka.apache.org/downloads.html,不出所料的奇慢无比
清华源:https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/
安装kafka
以2.8.1为例:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.8.1/kafka_2.12-2.8.1.tgz --no-check-certificate
tar -zxf kafka_2.12-2.8.1.tgz
然后进入目录:
cd kafka_2.12-2.8.1/
先后台运行zookeeper(这个kafka自带):
nohup ./bin/zookeeper-server-start.sh config/zookeeper.properties &
然后后台运行kafka:
./bin/kafka-server-start.sh -daemon config/server.properties
创建topic
kafka按照--topic topicname
中的topicname
将消息分类,所有的生产者与消费者都基于topicname
./bin/kafka-topics.sh --create --topic mytesttopic --replication-factor 1 --partitions 1 --bootstrap-server localhost:9092
然后显示Created topic mytesttopic.
启动生产者与消费者
新建一个窗口,启动生产者:
./bin/kafka-console-producer.sh --topic mytesttopic --broker-list localhost:9092
新建一个窗口,启动消费者:
./bin/kafka-console-consumer.sh --topic mytesttopic --from-beginning --bootstrap-server localhost:9092
然后可以在生产者这里输入任何信息,消费者这里就可以接收到了
查看已有的topic
bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --list