Mac上安装和测试Kafka

1. 安装

默认会把zookeeper一起安装好

brew install kafka

安装日志:

Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/cask and homebrew/services).
==> New Casks
agi                        cursr                      height                     scenica-player             tsh
ankerwork                  elephas                    overt                      scrolla                    vincelwt-chatgpt
bambu-studio               farcaster                  scene-maestro              spike                      wooshy
==> Updated Casks
Updated 618 casks.
==> Deleted Casks
atlantis           azire           flowsync           privatetunnel      stack              vitalsigns         whist-browser

==> Downloading https://ghcr.io/v2/homebrew/core/zookeeper/manifests/3.7.0_1
Already downloaded: /Users/wlm/Library/Caches/Homebrew/downloads/67cad74753e0a5b976b6a8c3456f0af26b62eb51c7eddc3260be18ad2763578d--zookeeper-3.7.0_1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/zookeeper/blobs/sha256:6345ff0c91566327755a61dd9bc5aa77ea76a41e40803e4d51c6798ba2f8d
Already downloaded: /Users/wlm/Library/Caches/Homebrew/downloads/65c4a8e0f4ce6b008594939bf915d169a8b05caeca50666a84fffebb30525a0e--zookeeper--3.7.0_1.monterey.bottle.tar.gz
==> Downloading https://ghcr.io/v2/homebrew/core/kafka/manifests/3.1.0
Already downloaded: /Users/wlm/Library/Caches/Homebrew/downloads/60ec8363d4d797309494f3301ebbcf1b601ddf24a9de9a5528be0b8616da17b4--kafka-3.1.0.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/kafka/blobs/sha256:4de5988e89839edd0e6e33ce37e4b5c2926295cdc2f7d05c09d5d535751350fd
Already downloaded: /Users/wlm/Library/Caches/Homebrew/downloads/f230499015335ce45bf6f143cad9d9277a7373dc4e68e7ec8a3671f1c8c53b5c--kafka--3.1.0.monterey.bottle.tar.gz
==> Installing dependencies for kafka: zookeeper
==> Installing kafka dependency: zookeeper
==> Pouring zookeeper--3.7.0_1.monterey.bottle.tar.gz
  /usr/local/Cellar/zookeeper/3.7.0_1: 1,084 files, 42.4MB
==> Installing kafka
==> Pouring kafka--3.1.0.monterey.bottle.tar.gz
==> Caveats
To restart kafka after an upgrade:
  brew services restart kafka
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/kafka/bin/kafka-server-start /usr/local/etc/kafka/server.properties
==> Summary
  /usr/local/Cellar/kafka/3.1.0: 198 files, 84.2MB
==> Running `brew cleanup kafka`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> kafka
To restart kafka after an upgrade:
  brew services restart kafka
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/kafka/bin/kafka-server-start /usr/local/etc/kafka/server.properties

Homebrew会将

  • 二进制文件和脚本文件在/usr/local/bin目录下
  • Kafka配置文件在/usr/local/etc/kafka目录下
  • Zookeeper配置文件在/usr/local/etc/zookeeper目录下
  • log.dirs(Kafka的数据目录)被设置为/usr/local/var/lib/kafka-logs。

2. 启动

2.1 启动zookeeper

kafka是基于zookeeper的,启动Kafka前,需先启动zookeeper。

zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties 

报如下的错误:

[2022-12-18 10:45:21,647] INFO binding to port 0.0.0.0/0.0.0.0:2181 (org.apache.zookeeper.server.NIOServerCnxnFactory)
[2022-12-18 10:45:21,647] ERROR Unexpected exception, exiting abnormally (org.apache.zookeeper.server.ZooKeeperServerMain)
java.net.BindException: Address already in use

查看2181被占用情况:

sudo lsof -i :2181

kill掉所有占用2181的进程(的PID),如
 

sudo kill - 9 1713

另外,启动zookeeper还有下面两个:

  • 以服务方式启动(自动)
brew services start
  • 临时启动(手动)(我电脑这个方式不行,原因暂不明)
zkServer start

2.2 启动kafka

新起一个终端

brew services start kafka

另外,启动kafka还有下面这个命令(我电脑这个方式不行,原因暂不明)
 

/usr/local/opt/kafka/bin/kafka-server-start /usr/local/etc/kafka/server.properties

3. 测试

3.1 再起第三个终端,创建topic

kafka-topics --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test

查看topic命令如下:

kafka-topics --describe --bootstrap-server localhost:9092 --topic test

3.2 在第三个终端上启动生产者

kafka-console-producer --broker-list localhost:9092 --topic test

3.3 再起第四个终端,启动消费者

kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning

然后在生产者终端窗口输入消息,回车,即可在消费者终端状况看到收到的消息。

你可能感兴趣的:(BigData,kafka,java,大数据)