“I am a cage, in search of a bird.”
― Franz Kafka
Kafka 適合的作業系統是 Linux 和 Solaris,並且在這兩個作業系統有過很多測試,在 Windows 上目前仍然有部分功能未支援。
今天的範例是在 mac os Catalina v10.15.4環境下安裝 Kafka
linux 環境安裝 Kafka、Zookeeper,到官網下載 Kafka官方載點、Zookeeper官方載點
$ wget https://dlcdn.apache.org/kafka/2.8.0/kafka-2.8.0-src.tgz
$ tar -zxf kafka-2.8.0-src.tgz
$ wget https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
$ tar -zxf apache-zookeeper-3.7.0-bin.tar.gz
mac 環境快速安裝首選是用 Homebrew
brew update
$ brew search kafka
==> Formulae
kafka ✔ librdkafka akka
==> Casks
kakapo
brew install kafka
brew info kafka
查看版本號、安裝路徑、如何啟動$ brew info kafka
kafka: stable 2.8.0 (bottled)
Open-source distributed event streaming platform
https://kafka.apache.org/
/usr/local/Cellar/kafka/2.8.0 (884 files, 82.4MB) *
Poured from bottle on 2021-05-31 at 17:53:04
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/kafka.rb
License: Apache-2.0
==> Dependencies
Required: openjdk ✘, zookeeper ✔
==> 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 -daemon /usr/local/etc/kafka/zookeeper.properties & kafka-server-start /usr/local/etc/kafka/server.properties
==> Analytics
install: 4,832 (30 days), 15,882 (90 days), 67,331 (365 days)
install-on-request: 4,829 (30 days), 15,876 (90 days), 67,114 (365 days)
build-error: 0 (30 days)
這邊可以蠻多資訊看到安裝的路徑/usr/local/Cellar/kafka/2.8.0
,以及啟動 zookeeper 和 kafka 的指令 zookeeper-server-start -daemon /usr/local/etc/kafka/zookeeper.properties & kafka-server-start /usr/local/etc/kafka/server.properties
,最後可以看到安裝的數量,近一年有六萬多次,比預期的少一些。
brew info kafka
查看版本號、安裝路徑、如何啟動==> kafka: stable 3.4.0
Open-source distributed event streaming platform
https://kafka.apache.org/
/usr/local/Cellar/kafka/3.4.0 (237 files, 122.0MB) *
Built from source on 2023-05-05 at 11:02:17
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/kafka.rb
License: Apache-2.0
==> Dependencies
Required: openjdk ✔, zookeeper ✘
==> Caveats
To 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:
/usr/local/opt/kafka/bin/kafka-server-start /usr/local/etc/kafka/server.properties
==> Analytics
install: 1,959 (30 days), 7,850 (90 days), 12,304 (365 days)
install-on-request: 1,958 (30 days), 7,847 (90 days), 12,302 (365 days)
build-error: 0 (30 days)
這邊可以蠻多資訊看到安裝的路徑/usr/local/Cellar/kafka/3.4.0
,以及啟動 openjdk 和 kafka 的指令 /usr/local/opt/kafka/bin/kafka-server-start /usr/local/etc/kafka/server.properties
,可以看到跟v2.8.0版本不同之處是已經不需要 zookeeper
改用 openjdk
、啟動指令也不同了。
brew services list
=> 會列出已安裝的清單,可以查看到目前 kafka 和 zookeeper 是停止狀態$ brew services list
Name Status User File
kafka none
zookeeper none
dataDir=/usr/local/var/lib/zookeeper
# zookeeper 開放給 zookeeper 連結的 port
clientPort=2181
# 因為不是 production 這邊將每個 IP 可以連結數量限制先關閉
maxClientCnxns=0
# 預設是將 adminserver 關閉,為的是避免 port 重複的衝突
admin.enableServer=false
# admin.serverPort=8080
listeners=PLAINTEXT://localhost:9092
# broker 的 ID,每一個 broker 的 ID 必須是唯一的、不能和其他重複。
broker.id=0
# broker 監聽的 host 和 port
listeners=PLAINTEXT://localhost:9092
# 提供給 producers 和 consumers 的 hostname 和 port,這邊如果沒有設定會直接讀取上面的 listeners,如果 listeners 也沒有設定會取用 java.net.InetAddress.getCanonicalHostName() 的回傳值
#advertised.listeners=PLAINTEXT://your.host.name:9092
# broker server 接收 requests 和傳送 responses 的 threads 數量
num.network.threads=3
# broker server 處理 requests 的 threads 數量(包含磁碟I/O)
num.io.threads=8
# 指定 Socket 伺服器所使用的傳送緩衝區
socket.send.buffer.bytes=102400
# 指定 Socket 伺服器所使用的接收緩衝區
socket.receive.buffer.bytes=102400
# 指定 Socket 伺服器所接受的要求大小上限
socket.request.max.bytes=104857600
############################# Log Basics #############################
# 資料存放路徑,可存放到多個位置,將路徑用逗號分開即可
log.dirs=/usr/local/var/lib/kafka-logs
# 預設新建主題時會創建幾個分區
num.partitions=1
# 復原資料用的 thread 數量
num.recovery.threads.per.data.dir=1
# 預設保存資料一週
log.retention.hours=168
# 每個 log segment 檔案的大小上限
log.segment.bytes=1073741824
# 多久檢查一次 log segment 需不需要被刪除
log.retention.check.interval.ms=300000
# zookeeper 的 port
zookeeper.connect=localhost:2181
cd /usr/local/Cellar/kafka/2.8.0/libexec
cd /usr/local/Cellar/kafka/3.4.0/libexec
kafka-server-start /usr/local/etc/kafka/server.properties
kafka-server-start -daemon /usr/local/etc/kafka/server.properties
ps -eaf | grep kafka/server.properties
,找到PID後zookeeper
cd /usr/local/Cellar/kafka/2.8.0/libexec
cd /usr/local/Cellar/kafka/3.4.0/libexec
zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties
daemon
即可ps aux | grep zookeeper
ctrl+c
即可zookeeper 預設會佔用2183,kafka 佔用9092
$ lsof -n -i:9092 | grep LISTEN
java 13682 username 182u IPv6 0x6b8b3949168d06d5 0t0 TCP 127.0.0.1:XmlIpcRegSvc (LISTEN)
v2.8.0
$ lsof -n -i:2183 | grep LISTEN
java 19634 username 119u IPv6 0xf4aea82af835c653 0t0 TCP *:eforward (LISTEN)
v3.4.0
$ lsof -n -i:2181 | grep LISTEN
java 19634 username 119u IPv6 0xf4aea82af835c653 0t0 TCP *:eforward (LISTEN)