Kafka with multi brokers
Prerequisite
# this will install java 1.8, zookeeper, and kafka
brew install kafka
啟動 zookeeper
brew services start zookeeper
開啟三個 Brokers
分别修改 server1.properties, server2.properties
修改位置 | server.properties | server1.properties | server2.properties |
---|---|---|---|
broker.id | broker.id=0 | broker.id=1 | broker.id=2 |
listeners | listeners=PLAINTEXT://:9092 | listeners=PLAINTEXT://:9093 | listeners=PLAINTEXT://:9094 |
log.dir | log.dir=/usr/local/var/lib/kafka-logs-0 | log.dir=/usr/local/var/lib/kafka-logs-1 | log.dir=/usr/local/var/lib/kafka-logs-2 |
kafka-server-start /usr/local/etc/kafka/server.properties &
kafka-server-start /usr/local/etc/kafka/server1.properties &
kafka-server-start /usr/local/etc/kafka/server2.properties &
開啟一個兩個副本的 Topic
kafka-topics –create –zookeeper localhost:2181 –replication-factor 3 –partitions 1 –topic mytopic
Consumer & Producer API
- Shell script
使用 brew kafka 提供的 shell script - Python
- pykafka
- kafka-python
- Scala
- Scala-Shell
- Native-App
Shell Script
開啟兩個命令列
./bin/kafka-console-producer --broker-list localhost:9092 --topic test-kafka
./bin/kafka-console-consumer --bootstrap-server localhost:9092 --topic test-kafka --from-beginning
應該要能看到訊息傳送過去