1. 程式人生 > >Kafka安裝

Kafka安裝

pan 新的 一個 ica 文件中 standard lead 啟用 ons

Step 1: 下載Kafka

點擊下載最新的版本並解壓.

> tar -xzf kafka_2.9.2-0.8.1.1.tgz
> cd kafka_2.9.2-0.8.1.1

Step 2: 啟動服務
Kafka用到了Zookeeper,所有首先啟動Zookper,下面簡單的啟用一個單實例的Zookkeeper服務。可以在命令的結尾加個&符號,這樣就可以啟動後離開控制臺。

> bin/zookeeper-server-start.sh config/zookeeper.properties &
[2013-04-22 15:01:37,495] INFO Reading configuration from
: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig) ...

啟動Kafka:

> bin/kafka-server-start.sh config/server.properties
[2013-04-22 15:01:47,028] INFO Verifying properties (kafka.utils.VerifiableProperties)
[2013-04-22 15:01:47,051] INFO Property socket.send.buffer.bytes is overridden to 1048576
(kafka.utils.VerifiableProperties) ...

Step 3: 創建 topic

創建一個叫做“test”的topic,它只有一個分區,一個副本。

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

可以通過list命令查看創建的topic:

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

除了手動創建topic,還可以配置broker讓它自動創建topic.

Step 4:發送消息.

Kafka 使用一個簡單的命令行producer,從文件中或者從標準輸入中讀取消息並發送到服務端。默認的每條命令將發送一條消息。
運行producer並在控制臺中輸一些消息,這些消息將被發送到服務端:

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

ctrl+c可以退出發送。

Step 5: 啟動consumer

Kafka also has a command line consumer that will dump out messages to standard output.
Kafka也有一個命令行consumer可以讀取消息並輸出到標準輸出:

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

你在一個終端中運行consumer命令行,另一個終端中運行producer命令行,就可以在一個終端輸入消息,另一個終端讀取消息。
這兩個命令都有自己的可選參數,可以在運行的時候不加任何參數可以看到幫助信息。

Step 6: 搭建一個多個broker的集群

剛才只是啟動了單個broker,現在啟動有3個broker組成的集群,這些broker節點也都是在本機上的:
首先為每個節點編寫配置文件:

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

在拷貝出的新文件中添加以下參數:

config/server-1.properties:
    broker.id=1
    port=9093
    log.dir=/tmp/kafka-logs-1
config/server-2.properties:
    broker.id=2
    port=9094
    log.dir=/tmp/kafka-logs-2

broker.id在集群中唯一的標註一個節點,因為在同一個機器上,所以必須制定不同的端口和日誌文件,避免數據被覆蓋。

We already have Zookeeper and our single node started, so we just need to start the two new nodes:
剛才已經啟動可Zookeeper和一個節點,現在啟動另外兩個節點:

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

創建一個擁有3個副本的topic:

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

現在我們搭建了一個集群,怎麽知道每個節點的信息呢?運行“"describe topics”命令就可以了:

> 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

下面解釋一下這些輸出。第一行是對所有分區的一個描述,然後每個分區都會對應一行,因為我們只有一個分區所以下面就只加了一行。
leader:負責處理消息的讀和寫,leader是從所有節點中隨機選擇的.
replicas:列出了所有的副本節點,不管節點是否在服務中.
isr:是正在服務中的節點.
在我們的例子中,節點1是作為leader運行。
向topic發送消息:

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
...
my test message 1my test message 2^C 

消費這些消息:

> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2
^C

測試一下容錯能力.Broker 1作為leader運行,現在我們kill掉它:

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


另外一個節點被選做了leader,node 1 不再出現在 in-sync 副本列表中:

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

雖然最初負責續寫消息的leader down掉了,但之前的消息還是可以消費的:

> bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2

(來源網絡摘取)

Kafka安裝