Kafka學習筆記 --- 快速開始QuickStart

Step 1: 下載代碼

> tar -xzf kafka_2.11-1.0.0.tgz

> cd kafka_2.11-1.0.0

 

Step 2: 啓動服務器(先保證Zookeeper已經啓動)

> 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自動創建模式

 

Step 4:發送一些消息 
Kafka自帶一個命令行客戶端,它從文件或者標準輸入中獲取輸入,並將其作爲message(消息)發送到Kafka集羣。 
默認情況下將作爲單獨的message發送。運行Producer,然後在控制檯輸入一些消息以發送到服務器。

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

This is a message

This is another message

 

Step 5:啓動一個consumer: 
Kafka還有一個命令行consumer(消費者),將消息轉存到標準的輸出。

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

This is a message

This is another message

上述操作均爲在單節點測試,現在開始在集羣上操作(擴展至三個節點)。

 

Step 6:設置多Agent集羣 
首先爲每個代理創建一個配置文件

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

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

分別編輯幾個節點的配置文件:

config/server-1.properties:

    broker.id=1                         # 這裏的broker.id屬性是集羣中每個節點的名稱,這一名稱是唯一且永久的。我們必須修改端口與日誌目錄。

    listeners=PLAINTEXT://:9093

    log.dir=/tmp/kafka-logs-1

  

config/server-2.properties:

    broker.id=2

    listeners=PLAINTEXT://:9094

    log.dir=/tmp/kafka-logs-2

修改配置文件後分別啓動幾個節點:

> 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

啓動後,並創建Topoc,我們可以通過describe來查看狀態:

> 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事負責給定分區所有讀寫操作的節點。每個節點都是隨機選擇的部分分區的領導者。
  • replicas是複製分區日誌的節點列表,不管這些節點是leader還是僅僅活着。
  • isr是一組同步replicas,是replicas列表的子集,他活着並被指定到leader。

其中leader是主題分區的唯一領導者。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章