Kafka 官方文檔學習筆記 -- 啓動配置kafka單點/多點集羣 自帶zookeeper管理

官方文檔上quick start部分對kafka在單節點的不同端口上的運行和合作已經描述的非常詳細,但是沒有多節點集羣的配置操作。本文在整合總結官方文檔的啓動部分的基礎上,新增了構建配置多機器kafka集羣,並使用自行安裝的zookeeper管理的詳細方法。

Quick start部分

start the server

啓動zookeeper

kafka自帶單點的zookeeper:
> bin/zookeeper-server-start.sh config/zookeeper.properties

啓動kafka server

(-daemon可以守護進程,就不需要再克隆一個對話來操作了)
> bin/kafka-server-start.sh -daemon config/server.properties

測試進程

輸入 jps 命令查看當前進程:
QuorumPeerMain是zookeeper進程,Kafka是kafka進程

創建topic

創建名爲test的topic,一個分區和一個副本:
> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看已有的topic列表:
> bin/kafka-topics.sh --list --zookeeper localhost:2181

刪除topic

> bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic test

發送消息

通過本機9092端口發送消息,每行爲一個message

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
This is a message
This is another message

啓動consumer

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
This is a message
This is another message

啓動多個broker

單臺主機不同端口的server配置:
(多臺主機需要配置zk和listener ip)

> cp config/server.properties config/server-1.properties
> cp config/server.properties config/server-2.properties

修改這些文件:

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

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

啓動這兩個server(通用):

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

查看某個topic詳細信息

查看一個有三個副本的my-replicated-topic信息:

> bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
Topic:my-replicated-topic   PartitionCount:1    ReplicationFactor:3 Configs:
    Topic: my-replicated-topic  Partition: 0    Leader: 1   Replicas: 1,2,0 Isr: 1,2,0

終結某個server進程

> 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

Connect Standalone

第一個配置文件是常規的kafka連接配置,後兩個每個都是一個要創建的連接器
> bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties

測試:
寫入文件test.txt:
> echo -e "foo\nbar" > test.txt
查看產生的topic:
> more test.sink.txt
繼續寫入:
> echo Another line>> test.txt

多節點kafka集羣配置:

啓動zookeeper

在每臺機器上安裝zookeeper,併到zk安裝目錄下:
> bin/zkServer.sh start

測試:
查看zk狀態:
> bin/zkServer.sh status
這裏會看到有一臺機器是leader,其他是follower

shell訪問zk(最好換成ip地址):
> bin/zkCli.sh -server localhost:2181
每臺主機都要啓動zk

配置kafka

在每個機器上配置config/server.properties文件:
vi config/server.properties
需要修改的部分:

    broker.id=1 # 每個broker唯一的標示符,需unique
    listeners=PLAINTEXT://192.168.48.102:9092 # 本機的ip:port,通常都用9092端口
    host.name=192.168.48.102:9092 # 節點需要綁定的主機名稱。如果沒有設置,服務器會綁定到所有接口
    zookeeper.connect=192.168.48.101:9092,192.168.48.102:9092,192.168.48.103:9092 #不可以有空格!!這裏一定要寫對,kafka這個集羣的所有機器:端口號,用逗號分隔

(想了解其他配置的意義可以參考:https://www.cnblogs.com/LUA123/p/7349145.html 或官方文檔 “>http://kafka.apache.org/documentation/#brokerconfigs)_

啓動kafka

在每臺機器上輸入同樣啓動命令:
> bin/kafka-server-start.sh -daemon config/server.properties

啓動沒有問題的話,可以先jps查看Kafka進程是否已經啓動,再嘗試上文同樣的操作命令來探索kafka集羣的工作方式~

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