Kafka 溫故(四):Kafka的安裝

來源:互聯網
上載者:User

標籤:預設   配置   gre   自己的   啟用   inf   .sh   over   apach   

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的容錯機制還是不錯的

Kafka 溫故(四):Kafka的安裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.