Prerequisite

# this will install java 1.8, zookeeper, and kafka
brew install kafka

啟動 zookeeper

brew services start zookeeper

開啟三個 Brokers

分别修改 server1.properties, server2.properties

修改位置server.propertiesserver1.propertiesserver2.properties
broker.idbroker.id=0broker.id=1broker.id=2
listenerslisteners=PLAINTEXT://:9092listeners=PLAINTEXT://:9093listeners=PLAINTEXT://:9094
log.dirlog.dir=/usr/local/var/lib/kafka-logs-0log.dir=/usr/local/var/lib/kafka-logs-1log.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

應該要能看到訊息傳送過去

References