話不多說,直接動手。
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