搭建环境
1. 安装JDK
1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html下载Server JRE.
1.2 安装完成后需要添加以下的环境变量(右键点击“我的电脑” -> "高级系统设置" -> "环境变量" ):
JAVA_HOME: C:Javajdk1.8.0_31(这个是默认安装路径,如果安装过程中更改了安装目录,把更改后的路径填上就行了)
PATH: 在现有的值后面添加"; %JAVA_HOME%in"
1.3 打开cmd运行 "java -version" 查看当前系统Java的版本:

2. 安装Zookeeper
Kafka的运行依赖于Zookeeper,所以在运行Kafka之前我们需要安装并运行Zookeeper
2.1 下载安装文件: http://zookeeper.apache.org/releases.html
2.2 解压文件(本文解压到 D:softzookeeperzookeeper-3.3.6)
2.3 打开D:softzookeeperzookeeper-3.3.6conf,把zoo_sample.cfg重命名成zoo.cfg
2.4 从文本编辑器里打开zoo.cfg
调整配置如下:
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. dataDir=D:\tmp\data\zookeeper #日志位置 dataLogDir=D:\tmp\logs\zookeeper # the port at which the clients will connect clientPort=2181
2.5 运行Zookeeper: 打开bin目录cmd然后执行
zkserver
3. 安装并运行Kafka
3.1 下载安装文件: http://kafka.apache.org/downloads.html
3.2 解压文件(本文解压到 D:softkafkakafka_2.10-0.10.1.1)
3.3 打开D:softkafkakafka_2.10-0.10.1.1config
3.4 从文本编辑器里打开 server.properties
3.5 把 log.dirs的值改成 “log.dirs=D:softkafkakafka_2.10-0.10.1.1kafka-logs”
3.6 打开cmd
3.7 进入kafka文件目录: cd D:softkafkakafka_2.10-0.10.1.1in
3.8 输入并执行以打开kafka:
D:softkafkakafka_2.10-0.10.1.1binwindows>kafka-server-start.bat ....configserver.properties
结果:
4. 创建topics
4.1 打开cmd 并进入G:kafka_2.11-0.10.0.1inwindows
4.2 创建一个topic:
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看创建结果:
D:softkafkakafka_2.10-0.10.1.1inwindows>kafka-topics.bat --list --zookeeper localhost:2181
5. 打开一个Producer:
D:softkafkakafka_2.10-0.10.1.1inwindows>kafka-console-producer.bat --broker-list localhost:9092 --topic test
6. 打开一个Consumer:
D:softkafkakafka_2.10-0.10.1.1inwindows>kafka-console-consumer.bat --zookeeper localhost:2181 --topic test
然后就可以在Producer控制台窗口输入消息了。在消息输入过后,很快Consumer窗口就会显示出Producer发送的消息:

至此,Kafka运行环境的搭建就完成了:-)