KAFKA集羣搭建(自帶zookeeper)

1. KAFKA下載地址:http://kafka.apache.org/downloadsnode

    KAFKA-快速上手-官方網站:http://kafka.apache.org/quickstartlinux

 

2. 下載完成後解壓,修改config目錄下的配置文件:server.properties 、zookeeper.properties
apache

1)修改server.properties:bootstrap

     ------------------------------------------------- Server Basics -------------------------------------------------vim

      設置broker.id,確保每一個broker的id是惟一的,能夠默認master節點的爲0,其餘依次爲一、2...服務器

     ------------------------------------------------- Socket Server Settings -------------------------------------------------測試

      設置listeners,listeners = PLAINTEXT://172.16.106.153:9092fetch

     ------------------------------------------------- Log Basics -------------------------------------------------網站

      設置num.partitions,分區數量與broker數量一致ui

    ------------------------------------------------- Zookeeper -------------------------------------------------

      設置zookeeper集羣地址,zookeeper.connect = xxx.xxx.xxx.xxx:2181,xxx.xxx.xxx.xxx2181    (最重要的)

 

2)修改zookeeper.properties:

  設置鏈接參數,在最後添加配置

  tickTime=2000
  initLimit=8
  syncLimit=3

     設置broker.id的服務器地址,server.X和IP要與broker.id所在的服務器地址一致

  server.0=xxx.xxx.xxx.xxx:2888:3888
  server.1=xxx.xxx.xxx.xxx:2889:3889

 

3)添加zookeeper數據目錄的ID文件,在zookeeper數據目錄(默認dataDir=/tmp/zookeeper)添加myid文件,寫入broker.id的值。到/tmp/zookeeper目錄下執行:

      第一臺機器:echo 0 > myid

      第二臺機器:echo 1 > myid

 

3. 啓動kafka

  啓動時,先啓動zookeeper,再啓動kafka;關閉反之,先關閉kafka,再關閉zookeeper

  啓動zookeeper:  bin/zookeeper-server-start.sh config/zookeeper.properties &

  啓動kafka:     bin/kafka-server-start.sh config/server.properties &

 

4. 測試kafka集羣是否部署成功

1)第一臺機器上建立一個TOPIC

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

2)查看TOPIC列表,確認是否建立成功

  bin/kafka-topics.sh --list --zookeeper xxx.xxx.xxx.xxx:2181

3)建立生成者

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

4)在全部服務器上建立消費者

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

5)在第一臺機器上輸入消息,回車發送,CTRL+C終止,查看其餘機器的消費者是否能接受到消息

 

###那些年踩過的坑.....

1. WARN: Connection to node 1 could not be established. Broker may not be available.

  建立消費者時出的錯,重啓zookeeper和kafka

 

2. ERROR org.apache.kafka.common.errors.InvalidReplicationFactorException: replication factor: 1 larger than available brokers: 0

  kafka未啓動,先中止kafka和zookeeper,確保進程關閉,而後再從新啓動。

  bin/kafka-server-stop.sh       

  bin/zookeeper-server-stop.sh

  bin/zookeeper-server-start.sh config/zookeeper.properties &

  bin/kafka-server-start.sh config/server.properties &

 

3. WARN Error while fetching metadata with correlation id 0

  啓動生產者進程報錯,沒有綁定Kafka啓動監聽的host信息

  vim  config/server.properties

  listeners=PLAINTEXT://localhost:9092  ,若此處配置的IP,則在執行命令時使用IP:bin/kafka-console-consumer.sh --bootstrap-server xxx.xxx.xxx.xxx:9092 --from-beginning --topic test

 

4. UnknowHostException:主機名:主機名

  linux沒法解析域名致使,能夠將主機名對應的ip寫入到hosts文件中保存,或者直接刪除 /etc/Hostname 文件

  vim /tec/hosts

  IP    主機名  (如:172.10.1.1      name)

相關文章
相關標籤/搜索