環境
Java:jdk1.8
操作系統:CentOS
ZK:3.4.10
Kafka:2.12-2.0.0
ZK 安裝參考:分佈式ZooKeeper-3.4.10集羣安裝
Kafka配置
# broker的全局唯一編號,不能重複
broker.id=0
# 監聽
listeners=PLAINTEXT://:9092
# 日誌目錄
log.dirs=/data/kafka/logs
# 配置zookeeper的連接(如果不是本機,需要該爲ip或主機名)
zookeeper.connect=172.18.20.14:2181,172.18.11.126:2181,172.18.11.128:2181
啓動Zookeeper
啓動Kafka
./bin/kafka-server-start.sh ./config/server.properties 1>/dev/null 2>&1 &
如果需要啓動多個broker,配置文件中broker.id listeners log.dirs 必須修改。
# broker的全局唯一編號,不能重複
broker.id=1
# 監聽
listeners=PLAINTEXT://:9092
# 日誌目錄
log.dirs=/data/kafka/logs
# 配置zookeeper的連接(如果不是本機,需要該爲ip或主機名)
zookeeper.connect=172.18.20.14:2181,172.18.11.126:2181,172.18.11.128:2181
分別在172.18.20.14 ,172.18.11.126 ,172.18.11.128 啓動3個broker。
創建topic(指定副本數量爲3)
./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 1 --topic test
./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 10 --topic test1
查看所有的topic信息
./bin/kafka-topics.sh --list --zookeeper localhost:2181
查看某個topic的詳細信息
./bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test
[root@iZ94m4komqtZ kafka_2.12-2.0.0]# ./bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic test1
Topic:test1 PartitionCount:10 ReplicationFactor:3 Configs:
Topic: test1 Partition: 0 Leader: 0 Replicas: 0,1,2 Isr: 0,1,2
Topic: test1 Partition: 1 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0
Topic: test1 Partition: 2 Leader: 2 Replicas: 2,0,1 Isr: 2,0,1
Topic: test1 Partition: 3 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1
Topic: test1 Partition: 4 Leader: 1 Replicas: 1,0,2 Isr: 1,0,2
Topic: test1 Partition: 5 Leader: 2 Replicas: 2,1,0 Isr: 2,1,0
Topic: test1 Partition: 6 Leader: 0 Replicas: 0,1,2 Isr: 0,1,2
Topic: test1 Partition: 7 Leader: 1 Replicas: 1,2,0 Isr: 1,2,0
Topic: test1 Partition: 8 Leader: 2 Replicas: 2,0,1 Isr: 2,0,1
Topic: test1 Partition: 9 Leader: 0 Replicas: 0,2,1 Isr: 0,2,1
- “leader”:該節點負責該分區的所有的讀和寫,每個節點的leader都是隨機選擇的。
- “replicas”:備份的節點列表,無論該節點是否是leader或者目前是否還活着,只是顯示。
- “isr”:“同步備份”的節點列表,也就是活着的節點並且正在同步leader
其中Replicas
和Isr
中的1,2,0
就對應着3個broker
他們的broker.id
屬性。
Topic信息修改
bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic test --config max.message.bytes=128000
bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic test --delete-config max.message.bytes
bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic test --partitions 10
bin/kafka-topics.sh --zookeeper localhost:2181 --alter --topic test --partitions 3 ## Kafka分區數量只允許增加,不允許減少
啓動生產者
./bin/kafka-console-producer.sh --broker-list 172.18.20.14:9092,172.18.11.126:9092,172.18.11.128:9092 --topic test
啓動消費者
./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --from-beginning --topic test
啓動消費組
./bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test1 --group testg
查看消費組名稱信息
#查看group 列表
./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list
#看指定group
./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group new-consumer-test
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET LAG CONSUMER-ID HOST CLIENT-ID
test1 3 1 1 0 new-consumer-cl-80a33e58-df25-4bce-ae93-bf37ecd6555b /172.18.11.126 new-consumer-cl
test1 0 0 0 0 new-consumer-cl-80a33e58-df25-4bce-ae93-bf37ecd6555b /172.18.11.126 new-consumer-cl
test1 4 1 1 0 new-consumer-cl-80a33e58-df25-4bce-ae93-bf37ecd6555b /172.18.11.126 new-consumer-cl
test1 1 1 1 0 new-consumer-cl-80a33e58-df25-4bce-ae93-bf37ecd6555b /172.18.11.126 new-consumer-cl
test1 2 1 1 0 new-consumer-cl-80a33e58-df25-4bce-ae93-bf37ecd6555b /172.18.11.126 new-consumer-cl
test1 7 1 1 0 new-consumer-cl-bc9a3300-8682-42c4-8e64-fb06d2981d43 /172.18.11.128 new-consumer-cl
test1 8 1 1 0 new-consumer-cl-bc9a3300-8682-42c4-8e64-fb06d2981d43 /172.18.11.128 new-consumer-cl
test1 9 1 1 0 new-consumer-cl-bc9a3300-8682-42c4-8e64-fb06d2981d43 /172.18.11.128 new-consumer-cl
test1 6 0 0 0 new-consumer-cl-bc9a3300-8682-42c4-8e64-fb06d2981d43 /172.18.11.128 new-consumer-cl
test1 5 1 1 0 new-consumer-cl-bc9a3300-8682-42c4-8e64-fb06d2981d43 /172.18.11.128 new-consumer-cl
#查看分組成員
./bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group new-consumer-test --members
topic到group質檢是發佈訂閱的通信方式,即一條topic會被所有的group消費,屬於一對多模式;group到consumer是點對點通信方式,屬於一對一模式。
不使用group的話,啓動10個consumer消費一個topic,這10個consumer都能得到topic的所有數據,相當於這個topic中的任一條消息被消費10次。
使用group的話,連接時帶上groupid,topic的消息會分發到10個consumer上,每條消息只被消費1次。
容錯測試
kill其中一個broker,消息隊列仍然正常。