天天看點

Kafka進階(6)-Kafka配額限速機制Kafka進階(6)-Kafka配額限速機制(Quotas)

Kafka進階(6)-Kafka配額限速機制(Quotas)

文章目錄

  • Kafka進階(6)-Kafka配額限速機制(Quotas)
    • 限制producer端速率
    • 限制consumer端速率
    • 取消Kafka的Quota配置

生産者和消費者以極高的速度生産/消費大量資料或産生請求,進而占用broker上的全部資源,造成網絡IO飽和。有了配額(Quotas)就可以避免這些問題。Kafka支援配額管理,進而可以對Producer和Consumer的produce&fetch操作進行流量限制,防止個别業務壓爆伺服器。

限制producer端速率

為所有client id設定預設值,以下為所有producer程式設定其TPS不超過1MB/s,即1048576/s,指令如下:

bin/kafka-configs.sh --zookeeper node1.itcast.cn:2181 --alter --add-config 'producer_byte_rate=1048576' --entity-type clients --entity-default
           

運作基準測試,觀察生産消息的速率

bin/kafka-producer-perf-test.sh --topic test --num-records 50000 --throughput -1 --record-size 1000 --producer-props bootstrap.servers=node1.itcast.cn:9092,node2.itcast.cn:9092,node3.itcast.cn:9092 acks=1
           

結果:

50000 records sent, 1108.156028 records/sec (1.06 MB/sec)

限制consumer端速率

對consumer限速與producer類似,隻不過參數名不一樣。

為指定的topic進行限速,以下為所有consumer程式設定topic速率不超過1MB/s,即1048576/s。指令如下:

bin/kafka-configs.sh --zookeeper node1.itcast.cn:2181 --alter --add-config 'consumer_byte_rate=1048576' --entity-type clients --entity-default
           

運作基準測試:

bin/kafka-consumer-perf-test.sh --broker-list node1.itcast.cn:9092,node2.itcast.cn:9092,node3.itcast.cn:9092 --topic test --fetch-size 1048576 --messages 50000
           

結果為:

MB.sec:1.0743

取消Kafka的Quota配置

使用以下指令,删除Kafka的Quota配置

bin/kafka-configs.sh --zookeeper node1.itcast.cn:2181 --alter --delete-config 'producer_byte_rate' --entity-type clients --entity-default
bin/kafka-configs.sh --zookeeper node1.itcast.cn:2181 --alter --delete-config 'consumer_byte_rate' --entity-type clients --entity-default