1. 程式人生 > 其它 >kafka安裝入門

kafka安裝入門

技術標籤:kafka

1、安裝單機版的kafka

1.1、下載kafka壓縮包

下載地址在:https://mirror-hk.koddos.net/apache/kafka/2.7.0/kafka_2.12-2.7.0.tgz

解壓(注意:我這裡在windows下使用,用git base客戶端進行操作)
在這裡插入圖片描述

tar -xzf  kafka_2.12-2.7.0.tgz

1.2、啟動kafka

1.2.1、啟動zookeeper

進入對應的目錄,然後執行:bin/zookeeper-server-start.sh config/zookeeper.properties

列印的日誌資訊:

[2021-01-09 17:12:28,498]
INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig) [2021-01-09 17:12:28,499] WARN config\zookeeper.properties is relative. Prepend .\ to indicate that you're sure! (org.apache.zookeeper.server.quorum.QuorumPeerConfig) [2021-01-09 17:12:28,500] WARN \tmp\zookeeper is relative. Prepend .\ to indicate that you'
re sure! (org.apache.zookeeper.server.quorum.QuorumPeerConfig) [2021-01-09 17:12:28,518] INFO clientPortAddress is 0.0.0.0:2181 (org.apache.zookeeper.server.quorum.QuorumPeerConfig)

1.2.2、啟動kafka

執行指令: bin/kafka-server-start.sh config/server.properties

列印的日誌:

[2021-01-09 17:18:30,404] INFO Registered kafka:type=
kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$) [2021-01-09 17:18:30,858] INFO Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation (org.apache.zookeeper.common.X509Util) [2021-01-09 17:18:30,895] INFO starting (kafka.server.KafkaServer) [2021-01-09 17:18:30,896] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer)

1.2.3、建立一個topic

執行指令:bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic jeffchan0

列印的日誌:

log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Created topic jeffchan0.

我在windows的環境下,這裡有個異常:

log4j:ERROR Could not read configuration file from URL [file:/d/jeffchan/other-meterial/kafka_2.12-2.7.0/bin/…/config/tools-log4j.properties].
java.io.FileNotFoundException: \d\jeffchan\other-meterial\kafka_2.12-2.7.0\bin…\config\tools-log4j.properties (???)

解決方法:直接將config目錄下的tools-log4j.properties檔案的絕對路路徑(每個人的絕對路徑一般不同,你複製你對應的絕對路徑就可以了):

D:\jeffchan\other-meterial\kafka_2.12-2.7.0\config\tools-log4j.properties

開啟:kafka-run-class.sh檔案,將:

$base_dir/config/tools-log4j.properties 替換成絕對路經 D:\jeffchan\other-meterial\kafka_2.12-2.7.0\config\tools-log4j.properties

然後重新啟動kafka

檢視上述建立的topic:

bin/kafka-topics.sh --list --zookeeper localhost:2181
在這裡插入圖片描述

1.2.4、啟動生產者

執行命令(往topic jeffchan0中傳送訊息):

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic jeffchan0

啟動之後,可以往主題 jeffchan0中傳送訊息

在這裡插入圖片描述
這裡傳送了hello jeffchan的訊息

1.2.5、啟動消費者

執行命令:注意–from-beginning表示從頭開始讀取

 bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic jeffchan0 --from-beginning

在這裡插入圖片描述

可以發現,上述生產者釋出的訊息已經被消費者消費了。

參考資料: https://kafka.apachecn.org/quickstart.html