一、啟動ZooKeeper
打開一個新終端并鍵入以下命令
bin/zookeeper-server-start.sh config/zookeeper.properties
二鬓照、要啟動Kafka Broker熔酷,請鍵入以下命令
bin/kafka-server-start.sh config/server.properties
啟動Kafka Broker后,在ZooKeeper終端上鍵入命令 jps 豺裆,您將看到以下響應(yīng)
821 QuorumPeerMain
928 Kafka
931 Jps
現(xiàn)在你可以看到兩個守護(hù)進(jìn)程運行在終端上拒秘,QuorumPeerMain是ZooKeeper守護(hù)進(jìn)程号显,另一個是Kafka守護(hù)進(jìn)程。
三躺酒、創(chuàng)建Kafka主題
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic Hello-Kafka
四押蚤、主題列表
要獲取Kafka服務(wù)器中的主題列表,可以使用以下命令
bin/kafka-topics.sh --list --zookeeper localhost:2181
輸出
Hello-Kafka
五羹应、啟動生產(chǎn)者以發(fā)送消息
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic Hello-Kafka
六揽碘、啟動消費者以接收消息
bin/kafka-console-consumer.sh --zookeeper localhost:2181 —topic Hello-Kafka --from-beginning
七、單節(jié)點多代理配置
創(chuàng)建多個Kafka Brokers园匹,我們在配置/ server.properties中已有一個Kafka代理實例雳刺。 現(xiàn)在我們需要多個代理實例,因此將現(xiàn)有的server.prop-erties文件復(fù)制到兩個新的配置文件中裸违,并將其重命名為server-one.properties和server-two.properties掖桦。 然后編輯這兩個新文件并分配以下更改
config / server-one.properties
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=1
# The port the socket server listens on
port=9093
# A comma seperated list of directories under which to store log files
log.dirs=/tmp/kafka-logs-1
創(chuàng)建主題
讓我們?yōu)榇酥黝}將復(fù)制因子值指定為三個,因為我們有三個不同的代理運行累颂。 如果您有兩個代理滞详,那么分配的副本值將是兩個。
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 -partitions 1 --topic Multibrokerapplication
Describe 命令用于檢查哪個代理正在偵聽當(dāng)前創(chuàng)建的主題
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic Multibrokerappli-cation
輸出
bin/kafka-topics.sh --describe --zookeeper localhost:2181
--topic Multibrokerappli-cation
Topic:Multibrokerapplication PartitionCount:1
ReplicationFactor:3 Configs:
Topic:Multibrokerapplication Partition:0 Leader:0
Replicas:0,2,1 Isr:0,2,1
八紊馏、修改主題
bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic Hello-kafka --parti-tions 2