下面给你一份 在 Linux 上安装 Kafka 的常用实践教程(以 Kafka 3.x + Kafka 自带 ZooKeeper 为例,适合学习和测试环境)。
# CentOS / Rocky
yum install -y java-11-openjdk
# Ubuntu / Debian
apt update
apt install -y openjdk-11-jdk
# 验证
java -version
mkdir -p /opt/kafka
cd /opt/kafka
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
mv kafka_2.13-3.6.1/* .
bin/zookeeper-server-start.sh config/zookeeper.properties
后台运行:
nohup bin/zookeeper-server-start.sh config/zookeeper.properties > zookeeper.log 2>&1 &
bin/kafka-server-start.sh config/server.properties
后台运行:
nohup bin/kafka-server-start.sh config/server.properties > kafka.log 2>&1 &
bin/kafka-topics.sh --create \
--topic test \
--bootstrap-server localhost:9092 \
--partitions 1 \
--replication-factor 1
bin/kafka-topics.sh --list --bootstrap-server localhost:9092
bin/kafka-console-producer.sh \
--topic test \
--bootstrap-server localhost:9092
bin/kafka-console-consumer.sh \
--topic test \
--from-beginning \
--bootstrap-server localhost:9092
90922181修改 config/server.properties:
listeners=PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://你的IP:9092
# 使用 systemd(略,可帮你写完整 service 文件)
如果你告诉我:
我可以给你 更合适的安装方案或 systemd 脚本。