kafka quick start

話不多說,直接動手。

Step1:下載地址

kafka2.1.0版本下載地址
下載后解壓

> tar -xzf kafka_2.11-2.1.0.tgz
> cd kafka_2.11-2.1.0

Step2:啟動服務(wù)

kafka依賴于zookeeper,如果你沒有zookeeper,可以使用下面的命令,快速啟動一個zookeeper節(jié)點

> bin/zookeeper-server-start.sh config/zookeeper.properties

現(xiàn)在開始啟動kafka服務(wù)

> bin/kafka-server-start.sh config/server.properties

Step3:創(chuàng)建一個topic

創(chuàng)建一個名字為“test_topic”的topic

> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test_topic

然后,查看自己創(chuàng)建的topic

> bin/kafka-topics.sh --list --zookeeper localhost:2181

輸出: test_topic

Step4:作為生產(chǎn)者發(fā)送一些消息

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

Step5:啟動一個消費者,消費消息

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

Step6:啟動多個kafka代理

1.拷貝幾份配置文件

> cp config/server.properties config/server1.properties
> cp config/server.properties config/server2.properties

2.修改配置文件的內(nèi)容(broker_id, 端口號,日志文件路徑)
編輯server1.properties

> vim config/server1.properties
broker_id=1
listeners=PLAINTEXT://9093
log.dirs=/tmp/kafka-logs-1

編輯server2.properties

> vim config/server2.properties
broker_id=2
listeners=PLAINTEXT://9094
log.dirs=/tmp/kafka-logs-2

borker_id是每個節(jié)點在kafka集群里面的唯一標(biāo)識,listeners標(biāo)識節(jié)點監(jiān)聽的端口號,默認是9092,log.dirs標(biāo)識日志輸出路徑。
3.啟動kafka節(jié)點

> bin/kafka-server-start.sh config/server1.properties &
> bin/kafka-server-start.sh config/server2.properties &

4.創(chuàng)建多個實例驗證一下

> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic

完成第四步,我們已經(jīng)完成了一個kafka集群。
5.可以查看各個節(jié)點的狀態(tài)

> bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test_topic
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容