在 Ubuntu 上测试 Kafka,通常分为 安装、启动、创建 topic、收发消息 这几个步骤。下面给你一个最常用、可操作的完整流程(基于 Kafka 自带脚本)。
sudo apt update
sudo apt install openjdk-11-jdk -y
java -version
wget https://downloads.apache.org/kafka/3.6.1/kafka_2.13-3.6.1.tgz
tar -xzf kafka_2.13-3.6.1.tgz
cd kafka_2.13-3.6.1
版本号可换,建议用 3.x 稳定版。
Kafka 依赖 Zookeeper(旧版本) 或 KRaft(新版本)
下面分别给你两种测试方式。
bin/zookeeper-server-start.sh config/zookeeper.properties
新终端窗口继续操作
bin/kafka-server-start.sh config/server.properties
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c config/kraft/server.properties
bin/kafka-server-start.sh config/kraft/server.properties
bin/kafka-topics.sh --create \
--topic test-topic \
--bootstrap-server localhost:9092 \
--partitions 1 \
--replication-factor 1
查看 topic:
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
bin/kafka-console-producer.sh \
--topic test-topic \
--bootstrap-server localhost:9092
输入几条消息:
hello kafka
this is a test
按 Ctrl+C 退出。
bin/kafka-console-consumer.sh \
--topic test-topic \
--from-beginning \
--bootstrap-server localhost:9092
✅ 如果看到刚才发的消息,说明 Kafka 测试成功!
# 查看 broker 是否运行
jps
应看到:
Kafka
QuorumPeerMain # 如果用 Zookeeper
sudo netstat -tulnp | grep 9092
检查:
config/server.properties
确认:
listeners=PLAINTEXT://localhost:9092
你可以告诉我:
我可以给你 Docker 一键测试方案 或 生产级配置示例。