Windows下搭建Apache Kafka2.3.1集羣模式

前面我已經介紹如何搭建Apache Kafka2.3.1的單機版,今天給大家帶來的是如何在Windows下搭建Apache Kafka2.3.1集羣模式。

前言

在這裏插入圖片描述

  • Producer API允許程序發佈數據流到一個到多個Kafka topic。
  • Consumer API允許程序訂閱一個到多個topic,並且進行消費。
  • Streams API允許程序作爲一個數據流處理,將一個或多個topic中輸入的數據進行消費,並生產數據流到一個或多個topics中。
  • Connector API,可以通過Connector管理Kafka和另一個系統之間的數據複製,比如去捕獲關係型數據庫中的任意改變到一個表中。

我們已經認識了kafka集羣的幾個組成部分,接下我們開始搭建集羣。

對於Kafka集羣而言,單個Broker節點就是一個集羣,因此除了啓動更多的Broker實例外,沒有什麼大的變化。但是爲了更好地理解它,讓我們將集羣擴展到三個節點。

1. Zookeeper的啓動

Kafka集羣的是把狀態保存到了Zookeeper中。首先我們啓動Zoookeeper節點。

bin\windows\zookeeper-server-start.bat config/zookeeper.properties

這裏我啓動仍然是一個單機節點的Zookeeper。其實Zookeeper集羣的配置也是比較簡單。這裏我們就囉嗦了。

2.準備三份Kafka配置文件

在這裏插入圖片描述
現在我們修改這三個配置文件:

config/server.properties:
    broker.id=0
    listeners=PLAINTEXT://:9092
    log.dirs=/tmp/kafka-logs

config/server-1.properties:
    broker.id=1
    listeners=PLAINTEXT://:9093
    log.dirs=/tmp/kafka-logs-1
 
config/server-2.properties:
    broker.id=2
    listeners=PLAINTEXT://:9094
    log.dirs=/tmp/kafka-logs-2

其中:

  • broker.id: 是每一個borker在集羣的唯一標識,和zookeeper的myid性質一樣
  • listeners:是每一個broker的監聽端口,默認是9092
  • log.dir 是每一個broker的消息存放的目錄

3.啓動三個Kafka節點

3.1啓動9092

 bin\windows\kafka-server-start.bat config/server.properties 

3.2啓動9093

 bin\windows\kafka-server-start.bat config/server-1.properties 

3.3啓動9094

 bin\windows\kafka-server-start.bat config/server-2.properties 

這是收三個節點都已經啓動
在這裏插入圖片描述

3.4 在Zookeeper的節點中查看Kafka集羣的ids

這裏我們使用zookeeper-shell.bat來連接

E:\tools\kafka_2.12-2.3.1\bin>windows\zookeeper-shell.bat /
Connecting to /
Welcome to ZooKeeper!
WATCHER::

WatchedEvent state:SyncConnected type:None path:null

ls /
[cluster, controller_epoch, controller, brokers, zookeeper, kafka-manager, admin
, isr_change_notification, consumers, log_dir_event_notification, latest_produce
r_id_block, config]
ls /brokers
[ids, topics, seqid]
ls /brokers/ids
[0, 1, 2]

我們可以看到kafka集羣的數據已經註冊到了Zookeeper中。

4.創建一個Topic使用3個副本

Now create a new topic with a replication factor of three:

bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 1 --topic my-replicated-topic 

但現在我們已經搭建好了kafka集羣,我們怎麼樣知道每一個Broker在做什麼呢?,接下我們使用describe topics查看集羣狀態

bin\windows\kafka-topics.bat --describe --bootstrap-server localhost:9092 --topic my-replicated-topic
Topic:my-replicated-topic       PartitionCount:10       ReplicationFactor:1
Configs:segment.bytes=1073741824
        Topic: my-replicated-topic      Partition: 0    Leader: 0       Replicas
: 0     Isr: 0
        Topic: my-replicated-topic      Partition: 1    Leader: 1       Replicas
: 1     Isr: 1
        Topic: my-replicated-topic      Partition: 2    Leader: 2       Replicas
: 2     Isr: 2
        Topic: my-replicated-topic      Partition: 3    Leader: 0       Replicas
: 0     Isr: 0
        Topic: my-replicated-topic      Partition: 4    Leader: 1       Replicas
: 1     Isr: 1
        Topic: my-replicated-topic      Partition: 5    Leader: 2       Replicas
: 2     Isr: 2
        Topic: my-replicated-topic      Partition: 6    Leader: 0       Replicas
: 0     Isr: 0
        Topic: my-replicated-topic      Partition: 7    Leader: 1       Replicas
: 1     Isr: 1
        Topic: my-replicated-topic      Partition: 8    Leader: 2       Replicas
: 2     Isr: 2
        Topic: my-replicated-topic      Partition: 9    Leader: 0       Replicas
: 0     Isr: 0

下面是對輸出的解釋。

第一個行顯示所有partitions的一個總結,以下每一行給出一個partition中的信息,如果我們只有一個partition,則只顯示一行。

  • leader 是在給出的所有partitons中負責讀寫的節點,每個節點都有可能成爲leader

  • replicas 顯示給定partiton所有副本所存儲節點的節點列表,不管該節點是否是leader或者是否存活。

  • isr 副本都已同步的的節點集合,這個集合中的所有節點都是存活狀態,並且跟leader同步。

讓我們發送一些信息給我們的新topic:

bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic my-replicated-topic
...
my test message 1
my test message 2
^C

現在我們來消費這些消息:

bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C

讓我們來測試一下容錯性。 Broker 1 現在是 leader,讓我們來殺了它:
在Linux上用

> ps aux | grep server-1.properties
7564 ttys002    0:15.91 /System/Library/Frameworks/JavaVM.framework/Versions/1.8/Home/bin/java...
> kill -9 7564

注意:我們使用過的Windows,
在 Windows 上用:

> wmic process where "caption = 'java.exe' and commandline like '%server-1.properties%'" get processid
ProcessId
6016
> taskkill /pid 6016 /f

leader已經切換到一個從節點,而且節點1也不在同步副本集中了:

bin\windows\kafka-topics.bat --describe --zookeeper localhost:2181 --topic my-replicated-topic

不過,即便原先寫入消息的leader已經不在,這些消息仍可用於消費:

bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C

到這裏kafka集羣就已經搭建完成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章