以下是在Linux上使用Kafka消息隊列的步驟,以單機部署為例:
安裝Java(Kafka依賴Java 8+):
# Ubuntu/Debian
sudo apt update && sudo apt install openjdk-11-jdk -y
# CentOS/RHEL
sudo yum install java-11-openjdk -y
java -version # 驗證安裝
下載并解壓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 /opt/kafka
cd /opt/kafka
修改config/server.properties
關鍵參數:
broker.id=0
listeners=PLAINTEXT://localhost:9092 # 監聽地址
log.dirs=/tmp/kafka-logs # 日志目錄
zookeeper.connect=localhost:2181 # ZooKeeper地址(單機版可內置)
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
bin/kafka-server-start.sh -daemon config/server.properties
bin/kafka-topics.sh --create --topic test-topic \
--bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092
# 輸入消息后按Enter發送
bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092
# 查看歷史消息
# 停止Kafka
bin/kafka-server-stop.sh
# 停止ZooKeeper
bin/zookeeper-server-stop.sh
sudo firewall-cmd --add-port=9092/tcp
開放防火墻。log.dirs
)、配置多節點集群提升可用性。broker.id
和zookeeper.connect
參數。以上步驟參考自,可根據實際需求調整配置。