天天看點

Kafka系列之broker-list,bootstrap-server以及zookeeper

文章目錄

    • broker-list
    • bootstrap-servers vs zookeeper

我剛學kafka的時候,對這幾個概念有時候會混淆,尤其是配置的時候經常搞不清楚它們的差別。這篇文章打算做一個梳理。

broker-list

broker指的是kafka的服務端,可以是一個伺服器也可以是一個叢集。producer和consumer都相當于這個服務端的用戶端。

broker-list指定叢集中的一個或者多個伺服器,一般我們再使用console producer的時候,這個參數是必備參數,另外一個必備的參數是topic,如下示例:

C:\kafka\kafka_2.12-1.1.1
λ .\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test20190713
>this is a test
>
           

本地主機如果要模拟多個broker,方法是複制多個server.properties,然後修改裡面的端口, broker.id等配置模拟多個broker叢集。

比如模拟三個broker的情況,首先把config 目錄下的 server.properties 複制兩份,分别命名為 server-1.properties 和 server-2.properties,然後修改這兩個檔案的下列屬性,

server-1.properties:

broker.id=1
listeners=PLAINTEXT://:9093
log.dirs=C:/kafka/broker1
           

server-2.properties:

broker.id=2
listeners=PLAINTEXT://:9094
log.dirs=C:/kafka/broker2
           

broker.id 用來唯一辨別每一個 broker,每個broker都有一個唯一的id值用來區分彼此。Kafka在啟動時會在zookeeper中/brokers/ids路徑下建立一個與目前broker的id為名稱的虛節點,Kafka的健康狀态檢查就依賴于此節點。

我們可以打開一個zk的用戶端,通過ls指令來檢視下這個路徑下的内容:

λ .\bin\windows\zookeeper-shell.bat localhost:2181
Connecting to localhost:2181
Welcome to ZooKeeper!
JLine support is disabled
ls
WATCHER::

WatchedEvent state:SyncConnected type:None path:null

ls /brokers/ids
[0]
           

可以看到我們預設啟動的這個broker.id為0的節點。

bootstrap-servers vs zookeeper

bootstrap-servers指的是目标叢集的伺服器位址,這個和broker-list功能是一樣的,隻不過我們在console producer要求用後者。

以前我們使用console consumer測試消息收發時會這樣寫:

C:\kafka\kafka_2.12-1.1.1
λ .\bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic test20190713
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
           

這樣可以接收到生産者控制台發送的消息。

現在我們也可以這樣寫,

C:\kafka\kafka_2.12-1.1.1
λ .\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test20190713
           

你可以自己測試下,也是可以收到消息的。

前者是老版本的用法,0.8以前的kafka,消費的進度(offset)是寫在zk中的,是以consumer需要知道zk的位址。後來的版本都統一由broker管理,是以就用bootstrap-server了。

Kafka系列之broker-list,bootstrap-server以及zookeeper
Kafka系列之broker-list,bootstrap-server以及zookeeper

bootstrap-server還可以自動發現其它的broker。

Kafka系列之broker-list,bootstrap-server以及zookeeper

歡迎大家關注我的公衆号