官网
安装
-
is recommended 是推荐版本
安装图
配置文件
- 修改conf目录下的server.properties
vim server.properties
进入文件目录
- 修改主机名称、端口号、zookeeper
host.name=hzh
port=9092
zookeeper.connect=hzh:2181
服务器名、端口号
-
配置zookeeper。因为是单节点,所以只需要配置一个就行了
Zookeeper
启动
- 先启动zookeeper
bin/zkServer.sh start
- 启动kafka
bin/kafka-server-start.sh -daemon ./config/server.properties
启动Kafka
测试
- 测试,创建一个topic。
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test1
创建
- 查看
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test1
查看
- 启动console生产者
bin/kafka-console-producer.sh --broker-list hzh:9092 --topic test1
生产者
- 启动console消费者
bin/kafka-console-consumer.sh --bootstrap-server hzh:9092 --topic test1
消费者
可能错误,在部分教程中会用到 --zookeeper
Exception in thread "main" joptsimple.UnrecognizedOptionException: zookeeper is not a recognized option
错误截图
解决原文
这是因为安装的kafka版本过高,2.8+的kafka,已经不需要依赖zookeeper来创建topic,新版本使用 --bootstrap-server 参数。
#创建主题
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic my-topic --partitions 1
#查看主题
bin/kafka-topics.sh --list --bootstrap-server localhost:9092