Kafka在Windows安裝運行

原文出處:http://blog.csdn.net/evankaka/article/details/52421314

一、安裝JDK

過程比較簡單,這裏不做說明。

最後打開cmd輸入如下內容,表示安裝成功


二、安裝zooeleeper

下載安裝包:http://zookeeper.apache.org/releases.html#download

下載後解壓到一個目錄:

1.進入Zookeeper設置目錄,筆者D:\Java\Tool\zookeeper-3.4.6\conf
2. 將“zoo_sample.cfg”重命名爲“zoo.cfg”
3. 在任意文本編輯器(如notepad)中打開zoo.cfg
4. 找到並編輯dataDir=D:\\Java\\Tool\\zookeeper-3.4.6\\tmp
5. 與Java中的做法類似,我們在系統環境變量中添加:
  a. 在系統變量中添加ZOOKEEPER_HOME = D:\Java\Tool\zookeeper-3.4.6
  b. 編輯path系統變量,添加爲路徑%ZOOKEEPER_HOME%\bin;
6. 在zoo.cfg文件中修改默認的Zookeeper端口(默認端口2181)

這是筆者最終的文件內容:

[plain] view plain copy
  1. # The number of milliseconds of each tick  
  2. tickTime=2000  
  3. # The number of ticks that the initial   
  4. # synchronization phase can take  
  5. initLimit=10  
  6. # The number of ticks that can pass between   
  7. # sending a request and getting an acknowledgement  
  8. syncLimit=5  
  9. # the directory where the snapshot is stored.  
  10. # do not use /tmp for storage, /tmp here is just   
  11. # example sakes.  
  12. dataDir=D:\\Java\\Tool\\zookeeper-3.4.6\\tmp  
  13. # the port at which the clients will connect  
  14. clientPort=2181  
  15. # the maximum number of client connections.  
  16. # increase this if you need to handle more clients  
  17. #maxClientCnxns=60  
  18. #  
  19. # Be sure to read the maintenance section of the   
  20. # administrator guide before turning on autopurge.  
  21. #  
  22. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  
  23. #  
  24. # The number of snapshots to retain in dataDir  
  25. #autopurge.snapRetainCount=3  
  26. # Purge task interval in hours  
  27. # Set to "0" to disable auto purge feature  
  28. #autopurge.purgeInterval=1  

7. 打開新的cmd,輸入zkServer,運行Zookeeper。
8. 命令行提示如下:說明本地Zookeeper啓動成功


三、安裝與運行Kafka

下載

http://kafka.apache.org/downloads.html。注意要下載二進制版本的


下載後解壓到任意一個目錄,筆者的是D:\Java\Tool\kafka_2.11-0.10.0.1

1. 進入Kafka配置目錄,D:\Java\Tool\kafka_2.11-0.10.0.1
2. 編輯文件“server.properties”
3. 找到並編輯log.dirs=D:\Java\Tool\kafka_2.11-0.10.0.1\kafka-log,這裏的目錄自己修改成自己喜歡的
4. 找到並編輯zookeeper.connect=localhost:2181。表示本地運行
5. Kafka會按照默認,在9092端口上運行,並連接zookeeper的默認端口:2181。

運行:

重要:請確保在啓動Kafka服務器前,Zookeeper實例已經準備好並開始運行。

1.進入Kafka安裝目錄D:\Java\Tool\kafka_2.11-0.10.0.1
2.按下Shift+右鍵,選擇“打開命令窗口”選項,打開命令行。
3.現在輸入

[plain] view plain copy
  1. .\bin\windows\kafka-server-start.bat .\config\server.properties   

並回車。


四、測試

上面的Zookeeper和kafka一直打開

(1)、創建主題

1.進入Kafka安裝目錄D:\Java\Tool\kafka_2.11-0.10.0.1
2.按下Shift+右鍵,選擇“打開命令窗口”選項,打開命令行。
3.現在輸入

[plain] view plain copy
  1. .\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic linlin  


注意不要關了這個窗口!

(2)創建生產者

1.進入Kafka安裝目錄D:\Java\Tool\kafka_2.11-0.10.0.1
2.按下Shift+右鍵,選擇“打開命令窗口”選項,打開命令行。
3.現在輸入


注意不要關了這個窗口!

(3)創建消費者

1.進入Kafka安裝目錄D:\Java\Tool\kafka_2.11-0.10.0.1
2.按下Shift+右鍵,選擇“打開命令窗口”選項,打開命令行。
3.現在輸入


注意不要關了這個窗口!

然後在第2個窗口中輸入內容,最後記得回車


大功告成!


使用Java編寫的kafka使用實例如下:

http://blog.csdn.net/evankaka/article/details/52494412

發佈了66 篇原創文章 · 獲贊 19 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章