首先确保机器已经安装好Zookeeper,Zookeeper安装参考 Windows单机配置Zookeeper环境
然后确保Zookeeper是正常启动状态
下载Kafka
![](https://i-blog.csdnimg.cn/blog_migrate/16e5df11cfd383bfbee863cd66c1208e.webp?x-image-process=image/format,png)
下载二进制包
下载后解压缩,我这里解压缩到了 D:\WorkSoftware\kafka_2.11_0.10.1.0 目录
![](https://i-blog.csdnimg.cn/blog_migrate/56969c3d062e7f70b815f7eb6ce40ed7.webp?x-image-process=image/format,png)
解压缩
首先建立一个空文件夹 logs
![](https://i-blog.csdnimg.cn/blog_migrate/62f20dd723f417faae10553820746556.webp?x-image-process=image/format,png)
建立logs文件夹
进入config目录,编辑 server.properties 文件
![](https://i-blog.csdnimg.cn/blog_migrate/ea8fcae57dbdfda931a87f2ee7abd933.webp?x-image-process=image/format,png)
编辑server.properties
修改 log.dirs 属性部分的地址
![](https://i-blog.csdnimg.cn/blog_migrate/cf03587a29a3ca09ae1fab99ee38c41e.webp?x-image-process=image/format,png)
修改log.dirs属性
然后在 D:\WorkSoftware\kafka_2.11_0.10.1.0 目录,按住shift+鼠标右键
![](https://i-blog.csdnimg.cn/blog_migrate/520ab37843cc4ea76a313e109dd699a9.webp?x-image-process=image/format,png)
在此处打开命令窗口
然后输入下面命令
.\bin\windows\kafka-server-start.bat .\config\server.properties
![](https://i-blog.csdnimg.cn/blog_migrate/e26defaca4c41708c1828cd753a68c3d.webp?x-image-process=image/format,png)
回车,执行命令
![](https://i-blog.csdnimg.cn/blog_migrate/27233c1c46993b6691996f4c269f15b5.webp?x-image-process=image/format,png)
出现错误
如果出现错误,我这里分析是CLASSPATH没有配置正确,那么,先设置CLASSPATH,然后再执行
SET CLASSPATH=.;
![](https://i-blog.csdnimg.cn/blog_migrate/f3d30bc398d1bad7cd21b7f57d76a3c4.webp?x-image-process=image/format,png)
设置CLASSPATH
![](https://i-blog.csdnimg.cn/blog_migrate/f0bdbc138b5241fa348899b3e3d2228b.webp?x-image-process=image/format,png)
成功
测试一下,首先进入 D:\WorkSoftware\kafka_2.11-0.10.1.0\bin\windows 目录,然后在当前目录打开命令窗口
![](https://i-blog.csdnimg.cn/blog_migrate/70948e55f583196d322c821e2ffc9386.webp?x-image-process=image/format,png)
输入下面命令,创建一个topic
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test.topic
如果还遇到无法加载类等错误,还是先设置CLASSPATH
![](https://i-blog.csdnimg.cn/blog_migrate/dd9b1bcaaab0b9dc6a878fd62f98fc25.webp?x-image-process=image/format,png)
遇到无法加载主类,需要设置CLASSPATH
![](https://i-blog.csdnimg.cn/blog_migrate/c735f2751159c8097b80684d9f9e10dc.webp?x-image-process=image/format,png)
topic创建成功
打开一个Producer
kafka-console-producer.bat --broker-list localhost:9092 --topic test.topic
![](https://i-blog.csdnimg.cn/blog_migrate/e2f8053db4fb11f6771e43215165880a.webp?x-image-process=image/format,png)
执行命令,打开生产者
这三个窗口不关闭的情况下(zookeeper/kafka/producer),再次开启一个命令窗口(还是在kafka目录右键打开),输入下面命令,开启Consumer
kafka-console-consumer.bat --zookeeper localhost:2181 --topic test.topic
![](https://i-blog.csdnimg.cn/blog_migrate/3fef8de28ef7f8881077f4f6bd407ae2.webp?x-image-process=image/format,png)
这样,这里就有4个命令窗口
![](https://i-blog.csdnimg.cn/blog_migrate/2ec72806d6c4bf6a9eddcf529f1d8bf5.webp?x-image-process=image/format,png)
分别是
- Zookeeper
- Kafka
- Producer
- Consumer
下面,我们在3号窗口(Producer)输入一些字符,就会看到4号窗口马上就收到消息
![](https://i-blog.csdnimg.cn/blog_migrate/8241d4b80fb7cc6c88a492093ed52c60.webp?x-image-process=image/format,png)
至此,Kafka的Windows单机环境搭建完毕
作者:sphsyv
链接:http://www.jianshu.com/p/64d25dcf8300
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。