1. 到官网下载tgz文件:https://kafka.apache.org/downloads
2. 创建存放kafka的文件夹:sudo mkdir /opt/module
3. 解压下载的文件:
sudo tar -xvf kafka_2.12-2.8.0.tgz -C /opt/module cd /opt/module mv kafka_2.12-2.8.0 kafka cd kafka
4. 使用默认配置启动zookeeper, 默认使用的端口是localhost:2181,(若2181端口被占用杀掉进程sudo fuser -k 2181/tcp)
sudo bin/zookeeper-server-start.sh config/zookeeper.properties # 能看到一堆打印信息,其中有如下信息,显示启动成功: [2021-08-10 20:37:18,482] INFO binding to port 0.0.0.0/0.0.0.0:2181
用bin/zookeeper-shell.sh连接ZK,显示如下信息即是成功:
bin/zookeeper-shell.sh localhost:2181
Connecting to localhost:2181 Welcome to ZooKeeper! JLine support is disabled WATCHER:: WatchedEvent state:SyncConnected type:None path:null
停止ZK命令:
sudo bin/zookeeper-server-stop.sh
5. 使用默认参数启动kafka,默认使用端口localhost:9092:
# 调试的时候去掉-daemon,这样日志能够显示出来 sudo bin/kafka-server-start.sh config/server.properties # 连接ZK成功会打印下面的日志: [2021-08-10 20:52:06,499] INFO starting (kafka.server.KafkaServer) [2021-08-10 20:52:06,499] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer) [2021-08-10 20:52:06,572] INFO Socket connection established, initiating session, client: /127.0.0.1:56418, server: localhost/127.0.0.1:2181 (org.apache.zookeeper.ClientCnxn) # 调试稳定后使用此命令后台运行 # sudo bin/kafka-server-start.sh -daemon config/server.properties # 停止kafka sudo bin/kafka-server-stop.sh
6. 新建Topic,Name是first
bin/kafka-topics.sh --zookeeper localhost:2181 --create --replication-factor 1 --partitions 1 --topic first # 创建成功提示: # Created topic first. # 查询创建的topic bin/kafka-topics.sh --list --zookeeper localhost:2181
7. 生产者,消费者:
# 生产者准备生产 bin/kafka-console-producer.sh --broker-list localhost:9092 --topic first >ii >aaa > # 消费者从头开始消费 bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic first ii aaa # 删除topic first bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic first
参考:
https://kafka.apache.org/quickstart 官方快速指导
https://www.w3cschool.cn/apache_kafka/apache_kafka_basic_operations.html Apache Kafka 基本操作
https://xuzheng.blog.csdn.net/article/details/106873063 Kafka的学习和使用
https://support.huaweicloud.com/devg-kafka/Kafka-java-demo.html Kafka Java客户端接入示例