安装
如果还没有安装Java, 可以先安装Java: brew cask install java
然后安装zookeeper和kafka。
bash-3.2$ brew install kafka
==> Downloading https://homebrew.bintray.com/bottles/kafka-2.3.1.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/0d/0d3bdac93bd2602f0a7ee444fa77081362da52ea54e3d66488904a67128b7afd?__gda__=
######################################################################## 100.0%
==> Pouring kafka-2.3.1.catalina.bottle.tar.gz
==> Caveats
To have launchd start kafka now and restart at login:
brew services start kafka
Or, if you don't want/need a background service you can just run:
zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties & kafka-server-start /usr/local/etc/kafka/server.properties
==> Summary
🍺 /usr/local/Cellar/kafka/2.3.1: 171 files, 59.5MB
bash-3.2$ brew install zookeeper
Error: Another active Homebrew update process is already in progress.
Please wait for it to finish or terminate it to continue.
==> Downloading https://homebrew.bintray.com/bottles/zookeeper-3.4.14.catalina.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/85/854225ed94e18cdf9a08b992a658e851d4c4d77d826e8ae243488e65b38af84c?__gda__=
######################################################################## 100.0%
==> Pouring zookeeper-3.4.14.catalina.bottle.tar.gz
==> Caveats
To have launchd start zookeeper now and restart at login:
brew services start zookeeper
Or, if you don't want/need a background service you can just run:
zkServer start
==> Summary
🍺 /usr/local/Cellar/zookeeper/3.4.14: 430 files, 36.5MB
修改 /usr/local/etc/kafka/server.properties, 找到 listeners=PLAINTEXT://:9092 那一行,把注释取消掉。
然后修改为:
############################# 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
启动
如果想以服务的方式启动,那么可以:
$ brew services start zookeeper
$ brew services start kafka
如果只是临时启动,可以:
$ zkServer start
$ kafka-server-start
/usr/local/etc/kafka/server.properties
创建Topic
$ kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
产生消息
$ kafka-console-producer --broker-list localhost:9092 --topic test
>HELLO Kafka
消费
简单方式:
$ kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
如果使用消费组:
kafka-console-consumer --bootstrap-server localhost:9092 --topic test --group test-consumer1 --from-beginning