本文最后更新于 2023年9月8日 下午
1. 下载、解压
1 2 3 4 5 6 7 8 9 10
| cd /opt wget http://mirrors.estointernet.in/apache/kafka/3.2.0/kafka_2.13-3.2.0.tgz mkdir kafka tar -zxvf kafka_2.13-3.2.0.tgz --strip-component=1 -C kafka
useradd -r -s /bin/bash -d /opt/kafka/ kafka chown -R kafka:kafka /opt/kafka/
echo 'export PATH=$PATH:/opt/kafka/bin' >> /etc/profile.d/dev_libs.sh . /etc/profile.d/dev_libs.sh
|
2. 基本配置
下面两种方式二选一,无特别要求就选 Kafka With
KRaft,也即是官方推荐的方式
2.1 Kafka With KRaft
在 Kafka 2.8.0 之后的版本中,已经不再使用 ZooKeeper
作为默认的协调器,并且在命令行工具中也不再支持--zookeeper
选项,改为使用内置的协调器(称为
KRaft)来管理主题、分区和其他元数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| su - kafka
KAFKA_CLUSTER_ID=`/opt/kafka/bin/kafka-storage.sh random-uuid`
/opt/kafka/bin/kafka-storage.sh format -t $KAFKA_CLUSTER_ID -c /opt/kafka/config/kraft/server.properties
/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/kraft/server.properties
|
创建 systemd 服务文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [Unit] Description=kafka After=network.target
[Service] Type=simple User=kafka ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/kraft/server.properties ExecStop=/opt/kafka/bin/kafka-server-stop.sh Restart=on-abnormal
[Install] WantedBy=multi-user.target
|
启动
1 2 3 4
| systemctl daemon-reload systemctl enable --now kafka_no-zk.service systemctl restart kafka_no-zk.service systemctl status kafka_no-zk.service
|
2.2 Kafka With ZooKeeper
1 2 3 4 5 6 7 8 9
| dataDir=/opt/kafka/data/zookeeper
log.dirs=/opt/kafka/data/kafka delete.topic.enable = true
chown -R kafka:kafka /opt/kafka/
|
创建 systemd 服务文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [Unit] Description=zookeeper After=network.target
[Service] Type=simple User=kafka ExecStart=/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh Restart=on-abnormal
[Install] WantedBy=multi-user.target
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| [Unit] Requires=zookeeper.service After=zookeeper.service Description=kafka After=network.target
[Service] Type=simple User=kafka ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties ExecStop=/opt/kafka/bin/kafka-server-stop.sh Restart=on-abnormal
[Install] WantedBy=multi-user.target
|
启动
1 2 3 4 5 6 7 8 9 10 11
| systemctl daemon-reload
systemctl enable --now zookeeper.service
systemctl status zookeeper.service
systemctl enable --now kafka.service
systemctl status kafka.service
|
3. 测试
1 2 3 4 5 6 7 8 9
| kafka-topics.sh --create --bootstrap-server localhost:9092 --topic testTopic
kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic testTopic
echo "Hello, World" | kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic cat /etc/os-release | kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic testTopic --from-beginning
|