1. 程式人生 > >Kafka之叢集部署

Kafka之叢集部署

Kafka支援多種叢集方式:

  • 單節點單broker叢集
  • 單節點多broker叢集
  • 多節點多broker叢集

一、單節點單broker叢集

在上篇中,我們在單臺機器上部署了Kafka,現在將其設定為單節點單broker叢集。架構如圖所示:

①修改配置檔案:

zookeeper.properties檔案(先手動建立zookeeper目錄)

dataDir=/opt/kafka/clusters/oneNodeoneBroker/zookeeper

server.propertis檔案(先手動建立logs目錄)

broker.id=0
port=9092
log.dirs=/opt/kafka/clusters/oneNodeoneBroker/logs
num.partitions=2
zookeeper.connect=192.168.37.137:2181

produce.properties

metadata.broker.list=192.168.37.137:9092

consumer.properties

metadata.broker.list=192.168.37.137:9092
group.id=test-consumer-group

②啟動

先啟動zookeeper

./zookeeper-server-start.sh -daemon ../config/zookeeper.properties

啟動kafka broker

./kafka-server-start.sh -daemon ../config/server.properties

③使用

建立一個名為oneNodeOneBroker的topic,單partitoin且只有一個副本:

sh kafka-topics.sh --create --zookeeper 192.168.37.137:2181 --replication-factor 1 --partitions 1 --topic oneNodeOneBroker

檢視topic列表:

 sh kafka-topics.sh --list --zookeeper 192.168.37.137:2181

單機連通性測試:

①啟動生產者

sh kafka-console-producer.sh --broker-list 192.168.37.137:9092 --topic oneNodeOneBroker

②啟動消費者

 sh kafka-console-consumer.sh --bootstrap-server 192.168.37.137:9092 --topic oneNodeOneBroker --from-beginning

在producer端輸入字串並回車,consumer端顯示則表示成功。

③關閉

停止kafka broker

sh kafka-server-stop.sh

停止zookeeper

sh zookeeper-server-stop.sh

 二、單節點多broker叢集

 

①修改配置檔案:

zookeeper.properties檔案(先手動建立zookeeper目錄)

dataDir=/opt/kafka/clusters/oneNodeoneBroker/zookeeper

server-1.propertis檔案(先手動建立logs/1目錄)

broker.id=1
port=9093
log.dirs=/opt/kafka/clusters/oneNodemanyBroker/logs/1
zookeeper.connect=192.168.37.137:2181

server-2.propertis檔案(先手動建立logs/2目錄)

broker.id=2
port=9094
log.dirs=/opt/kafka/clusters/oneNodemanyBroker/logs/2
zookeeper.connect=192.168.37.137:2181

produce.properties

metadata.broker.list=192.168.37.137:9093,192.168.37.137:9094

consumer.properties

metadata.broker.list=192.168.37.137:9093,192.168.37.137:9094
group.id=test-consumer-group

②啟動

先啟動zookeeper

./zookeeper-server-start.sh -daemon ../config/zookeeper.properties

啟動kafka broker

./kafka-server-start.sh -daemon ../config/server-1.properties
./kafka-server-start.sh -daemon ../config/server-2.properties

③使用

建立一個名為oneNodeManyBroker的topic,單partitoin且只有一個副本:

sh kafka-topics.sh --create --zookeeper 192.168.37.137:2181 --replication-factor 1 --partitions 1 --topic oneNodeManyBroker

檢視topic列表:

 sh kafka-topics.sh --list --zookeeper 192.168.37.137:2181

單機連通性測試:

①啟動生產者

sh kafka-console-producer.sh --broker-list 192.168.37.137:9093,192.168.37.137:9094 --topic oneNodeManyBroker

②啟動消費者

 sh kafka-console-consumer.sh --bootstrap-server 192.168.37.137:9093,192.168.37.137:9094 --topic oneNodeManyBroker --from-beginning

在producer端輸入字串並回車,consumer端顯示則表示成功。

③關閉

停止kafka broker

sh kafka-server-stop.sh

停止zookeeper

sh zookeeper-server-stop.sh

多節點多broker叢集 

在多節點多broker叢集中,每個節點都需要安裝Kafka,且所有的broker都連線到同一個ZooKeeper