1. 客戶端認證
如果kafka客戶端是認證的,那么可以使用userId和clientId兩種認證方式。如果沒有認證只能使用clientId限流。
bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'producer_byte_rate=1048576,consumer_byte_rate=1024' \ --entity-type clients --entity-name clientA
對clientId=clientA的客戶端添加限流設置。producer_byte_rate表示每秒最多能寫入到消息量,單位為byte/sec。consumer_byte_rate表示每秒最多能消費的消息了,單位也為byte/sec。設置后立即生效。
2. Producer & Consumer設置
在producer和consumer的配置中需要加入client.id配置:
Producer:
props.put(ProducerConfig.CLIENT_ID_CONFIG, "clientA");
Consumer:
props.put(ConsumerConfig.CLIENT_ID_CONFIG, "clientA");
限流效果:
3. 查看限流信息
zookeeper:
[zk: localhost:2181(CONNECTED) 5] get /config/clients/clientA {"version":1,"config":{"producer_byte_rate":"1048576","consumer_byte_rate":"1024"}}