在 MacOS 上安装 Kafka

记录一下在 Mac 上安装和测试 kafka 的步骤。

MacOS 上可以方便的使用 brew 进行安装。

安装

如果还没有安装Java, 可以先安装Java: brew cask install java

然后安装zookeeperkafka

1
2
brew install kafka
brew install zookeeper

修改 /usr/local/etc/kafka/server.properties, 找到 listeners=PLAINTEXT://:9092 那一行,把注释取消掉。
然后修改为:

1
2
3
4
5
6
7
8
############################# Socket Server Settings #############################
# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://localhost:9092

启动

如果想以服务的方式启动,那么可以:

1
2
$ brew services start zookeeper
$ brew services start kafka

如果只是临时启动,可以:

1
2
$ zkServer start
$ kafka-server-start /usr/local/etc/kafka/server.properties

创建Topic

1
$ kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

产生消息

1
2
$ kafka-console-producer --broker-list localhost:9092 --topic test
>HELLO Kafka

消费

简单方式:

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

参考文档

  1. https://medium.com/@Ankitthakur/apache-kafka-installation-on-mac-using-homebrew-a367cdefd273
  2. https://medium.com/@at_ishikawa/getting-started-with-kafka-on-mac-f6aa8924fcda
  3. https://kafka.apache.org/quickstart
  4. https://stackoverflow.com/questions/38549867/how-to-set-group-name-when-consuming-messages-in-kafka-using-command-line