準備工作:
Kafka依賴JAVA環境運行,請先安裝JDK,參考前面文章:https://blog.csdn.net/qq982782662/article/details/80745364
Kafka支持內置的Zookeeper和引用外部的Zookeeper,這裏使用外部Zookeeper,安裝參考前面文章:
https://blog.csdn.net/qq982782662/article/details/82811747
下載kafka_2.11-2.0.0.tgz,地址:https://www.apache.org/dyn/closer.cgi?path=/kafka/2.0.0/kafka_2.11-2.0.0.tgz
1、將包放到/opt目錄,並解壓
cd /opt;
#解壓
tar -zxvf zookeeper-3.4.13.tar.gz
tar -zxvf kafka_2.11-2.0.0.tgz
#重命名
mv kafka_2.11-2.0.0 kafka
2、修改配置
#創建日誌目錄
mkdir /opt/kafka/logs
#切換到配置目錄
cd /opt/kafka/config/
#編輯配置文件
vim server.properties
#修改日誌路徑
log.dirs=/opt/kafka/logs
#修改zookeeper地址,zookeeper多個的話用逗號分隔
zookeeper.connect=localhost:2181
#保存配置文件
:wq!
3、基本操作命令
3.1啓動
nohup /opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties
3.2停止
/opt/kafka/bin/kafka-server-stop.sh
3.3創建Topic(1個副本,1各分區)
/opt/kafka/bin/kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 1 --topic test
3.4列出所有Topic
/opt/kafka/bin/kafka-topics.sh -list -zookeeper 127.0.0.1:2181
3.5啓動Producer併發送消息,輸入相應的消息,eg:hello kafka;按Ctrl+C結束
/opt/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
3.6啓動Consumer並接收消息
/opt/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning