在 Ubuntu 上部署的 Kafka(通常是 Kafka Broker 或客户端)进行流量限速,可以从 Broker 端限流、Topic/Client 级别限流、网卡层限流 三个维度来做。下面按常见场景说明。
# server.properties
quota.producer.default=1048576 # 1 MB/s
quota.consumer.default=1048576 # 1 MB/s
或针对具体 client:
bin/kafka-configs.sh --bootstrap-server localhost:9092 \
--alter --add-config 'producer_byte_rate=1048576,consumer_byte_rate=1048576' \
--entity-type clients --entity-name your-client-id
✅ 单位:bytes/sec
✅ 只限制 Kafka 协议流量
bin/kafka-configs.sh --bootstrap-server localhost:9092 \
--alter --add-config 'leader.replication.throttled.rate=1048576' \
--entity-type topics --entity-name your-topic
或生产/消费限速:
--add-config 'producer_byte_rate=1048576,consumer_byte_rate=1048576'
# server.properties
leader.replication.throttled.rate=1048576
follower.replication.throttled.rate=1048576
bin/kafka-producer-perf-test.sh \
--topic test \
--num-records 100000 \
--record-size 1024 \
--throughput 1000 \
--producer-props bootstrap.servers=localhost:9092
--throughput 1000 = 1000 records/sec
限制 9092 端口出口 10Mbps:
sudo tc qdisc add dev eth0 root tbf rate 10mbit burst 32kbit latency 400ms
解除:
sudo tc qdisc del dev eth0 root
sudo iptables -A INPUT -p tcp --dport 9092 \
-m hashlimit --hashlimit-above 1mb/s --hashlimit-mode srcip \
-j DROP
docker run -d \
--name kafka \
--network=host \
--blkio-weight 500 \
kafka
或使用:
--cpu-quota--memory| 场景 | 推荐方式 |
|---|---|
| 防止消费者打满带宽 | client quota |
| 控制副本同步 | replication throttle |
| 整体机器限速 | tc |
| 压测限流 | perf-test --throughput |
| 多租户隔离 | client / user quota |
如果你能说明:
我可以给你一套可直接执行的配置示例。