IDEA使用java程式碼連線單節點Kafka連線失敗問題解決
2018-11-10 17:03:06,060] WARN [Producer clientId=producer-1] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient) [2018-11-10 17:03:06,163] WARN [Producer clientId=producer-1] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient) [2018-11-10 17:03:06,314] WARN [Producer clientId=producer-1] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient) [2018-11-10 17:03:06,517] WARN [Producer clientId=producer-1] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient) [2018-11-10 17:03:06,870] WARN [Producer clientId=producer-1] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient) [2018-11-10 17:03:07,778] WARN [Producer clientId=producer-1] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient) [2018-11-10 17:03:08,835] WARN [Producer clientId=producer-1] Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
單節點kafka進行idea連線除錯時一直顯示這個錯誤,現將我的解決辦法貼出來。
-
首先要在kafka/config這個目錄下找到server.properties這個配置檔案
-
其次將listeners = PLAINTEXT://your.host.name:9092這個屬性配置為你的IP地址 注意此時一定不要用hostname的形式,最好是使用IP地址的形式
-
重新啟動kafka,單節點模式啟動kafka/bin/kafka-server-start.sh -daemon kafka/config/server.properties
-
啟動完成後使用jps命令檢視是否啟動成功 在此時如果發現不能正常啟動kafka,則去/tmp這個目錄下刪除kafka-logs這個目錄,直接用rm -rf命令
-
此時可以重新建立一個主題kafka/bin/kafka-topics.sh --create --zookeeper 192.168.109.190:2181 --replication-factor 1 --partitions 1 --topic test7 同樣注意,此處一定要使用IP地址的形式,踩了無數坑,此處一定要使用IP地址形式!!!
-
先進行生產者測試kafka/bin/kafka-console-producer.sh --broker-list 192.168.109.190:9092 --topic test7 同樣,此處一定要使用IP地址的形式
-
此時檢視消費者是否能夠讀取訊息kafka/bin/kafka-console-consumer.sh --bootstrap-server 192.168.109.190:9092 --topic test7 --from-beginning 同樣,此處一定要使用IP地址的形式
-
在你的IDEA用java程式碼進行測試,經過測試可以使用
綜上所述,在用IDEA連線kafka進行測試時,如果遇到無法連線的問題,一定要注意IP地址,全部使用IP地址的形式進行嘗試。