记录一下在 Mac 上安装和测试 kafka 的步骤。
MacOS 上可以方便的使用 brew 进行安装。
安装
如果还没有安装Java, 可以先安装Java: brew cask install java
然后安装zookeeper和kafka。
1 | brew install kafka |
修改 /usr/local/etc/kafka/server.properties, 找到 listeners=PLAINTEXT://:9092 那一行,把注释取消掉。
然后修改为:
1 | ############################# Socket Server Settings ############################# |
启动
如果想以服务的方式启动,那么可以:
1 | $ brew services start zookeeper |
如果只是临时启动,可以:
1 | $ zkServer start |
创建Topic
1 | $ kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test |
产生消息
1 | $ kafka-console-producer --broker-list localhost:9092 --topic test |
消费
简单方式:
1 | $ kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning |
如果使用消费组:
1 | kafka-console-consumer --bootstrap-server localhost:9092 --topic test --group test-consumer1 --from-beginning |
参考文档
- https://medium.com/@Ankitthakur/apache-kafka-installation-on-mac-using-homebrew-a367cdefd273
- https://medium.com/@at_ishikawa/getting-started-with-kafka-on-mac-f6aa8924fcda
- https://kafka.apache.org/quickstart
- https://stackoverflow.com/questions/38549867/how-to-set-group-name-when-consuming-messages-in-kafka-using-command-line
