1. 程式人生 > 其它 >Idea執行Kafka.scala踩坑

Idea執行Kafka.scala踩坑

未指定 server.properties

直接執行時,報錯資訊如下:

> Task :core:Kafka.main() FAILED
USAGE: java [options] KafkaServer server.properties [--override property=value]*
Option               Description                                         
------               -----------                                         
--override <String>  Optional property that should override values set in
                       server.properties file        

解決方案:Run -> Edit Configurations... ,開啟如下圖所示對話方塊,在 Program Arguments 一欄填入 config\server.properties

未列印執行日誌

Kafka程式確實啟動了,但是不知道什麼地方出錯了?

> Task :core:Kafka.main()
log4j:WARN No appenders could be found for logger (kafka.server.KafkaConfig).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

> Task :core:Kafka.main() FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':core:Kafka.main()'.
> Process 'command 'D:/Common/Java/jdk1.8.0_131/bin/java.exe'' finished with non-zero exit value 1

將 config 目錄下的 log4j.properties 檔案複製到 core/src/main/resources 目錄下,這樣可以讓 Kafka 在執行時能夠輸出日誌資訊:

Connection refused

現在執行時能看到日誌了:

[2021-09-07 09:52:16,487] INFO Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2021-09-07 09:52:17,503] WARN Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn)
java.net.ConnectException: Connection refused: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
	at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
	at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1141)
[2021-09-07 09:52:17,613] INFO Opening socket connection to server 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2021-09-07 09:52:18,630] WARN Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn)
java.net.ConnectException: Connection refused: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
	at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
	at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1141)

2181 是 ZooKeeper 的埠,我們知道 Kafka 是需要用到 ZooKeeper 的,所以我們需要先安裝和啟動 ZooKeeper

因為我當前的 Kafka 版本是 1.0.0,我們看到它依賴的 zookeeper 是 3.4.10,所以我決定搭建 ZooKeeper 服務時也用 3.4.10 版本:

個人比較喜歡用Docker來本地測試:Docker安裝zookeeper