在 Linux 上测试 Kafka,通常分为 环境准备 → 启动服务 → 基本功能测试 → 常用测试工具/命令 几个步骤。下面给你一套最常用、最实用的测试方法(以 Kafka 3.x 为例,自带 Zookeeper 或 KRaft 均可)。
kafka_2.13-3.6.1.tgz)java -version
# 解压
tar -xzf kafka_2.13-3.6.1.tgz
cd kafka_2.13-3.6.1
# 启动 Zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties
# 另开一个终端,启动 Kafka
bin/kafka-server-start.sh config/server.properties
✅ 看到 started (kafka.server.KafkaServer) 表示启动成功
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
test message
另开终端:
bin/kafka-console-consumer.sh \
--topic test-topic \
--from-beginning \
--bootstrap-server localhost:9092
✅ 能看到 producer 发的内容,说明 Kafka 正常
bin/kafka-consumer-groups.sh \
--bootstrap-server localhost:9092 \
--list
bin/kafka-topics.sh \
--describe \
--topic test-topic \
--bootstrap-server localhost:9092
bin/kafka-producer-perf-test.sh \
--topic test-topic \
--num-records 100000 \
--record-size 1024 \
--throughput -1 \
--producer-props bootstrap.servers=localhost:9092
bin/kafka-consumer-perf-test.sh \
--topic test-topic \
--bootstrap-server localhost:9092 \
--messages 100000
pip install kafka-python
from kafka import KafkaProducer
p = KafkaProducer(bootstrap_servers='localhost:9092')
p.send('test-topic', b'hello from python')
p.flush()
# 查看日志
tail -f logs/server.log
netstat -an | grep 9092
启动 Zookeeper + Kafka → 建 Topic → console-producer 发消息 → console-consumer 收消息 → 成功即测试通过
如果你愿意,我可以:
你现在是学习测试,还是线上环境验证?