首先需要下载安装kafka,jdk为1.8
1.首先通过cmd进入kafka_2.12-1.0.1\bin\windows目录
2.启动zookeeper
zookeeper-server-start ..\..\config\zookeeper.properties
该命令需要指定zookeeper的配置文件位置才能正确启动,启动失败可能出现端口占用,修改配置文件端口号或者kill进程
端口号默认2181
测试连接一下
zookeeper-shell.bat 0.0.0.0:2181
3.启动Kafka
kafka-server-start.bat ..\..\config\server.properties
该命令需要指定kafka的配置文件位置
4.创建一个名为mytest的topic
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic mytest
参数的含义可以自查,当前无视
查看当前的Kafka中的所有Topic
kafka-topics.bat --list--zookeeper localhost:2181
5.查看topic
kafka-topics.bat --describe --zookeeper localhost:2181 --topic mytest
----------------------------------------------------------
Topic:mytest PartitionCount:3 ReplicationFactor:1 Configs:
Topic: mytest Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: mytest Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: mytest Partition: 2 Leader: 0 Replicas: 0 Isr: 0
6.发布/生产消息窗口,端口号默认为9092
kafka-console-producer.bat --broker-list localhost:9092 --topic mytest
7.消费消息窗口
kafka-console-consumer.bat --zookeeper localhost:2181 --from-beginning --topic mytest
8.在生产者producer窗口输入,可以在consumer窗口看到输出
bin\windows>kafka-console-producer.bat --broker-list localhost:9092 --topic mytest
--------------------------------------------------------------------
test
hello
nice to meet you
bin\windows>kafka-console-consumer.bat --zookeeper localhost:2181 --from-beginning --topic mytest
--------------------------------------------------------------------
test
hello
nice to meet you
中文的话会出现乱码,这个可能与你编译器的环境有关
--------------------------------------------------------------------