kafka局部调优
kafka常用命令
cd /opt/kafka/kafka/bin/
./zookeeper-server-start.sh -daemon /opt/kafka/kafka/config/zookeeper.properties
./kafka-server-start.sh -daemon /opt/kafka/kafka/config/server.properties
./kafka-topics.sh
./kafka-topics.sh
./kafka-topics.sh
./kafka-topics.sh
./kafka-topics.sh
./kafka-console-producer.sh
./kafka-console-consumer.sh
硬件
服务器台数=2*(生产者峰值(20M/s)*副本数/100)+1
磁盘:kafka顺序读写(固态和机械差不多) 100g*2个副本*3天/0.7 =1T
内存:kafka内存=堆内存(10-15g)+页缓存(segment=1g 默认)
(分区数*1g*25%)/服务器台数
jmap -heap 2321 查看内存堆栈信息
CPU:num.io.threads=8(总cpu 50% 写磁盘的线程数)
num.replica.fetchers=1(1/3*总cpu50% 副本拉取线程数 )
num.network.threads=3(2/3*总cpu50% 数据传输线程数 )
总CPU32建议kafka设置24个给系统预留8个
生产者参数
read-only(必须重启) per-broker(动态针对单个broker节点)
cluster-wide(动态针对集群的节点)
val ioproperties = new Properties()
ioproperties.put(ProducerConfig.BATCH_SIZE_CONFIG,"16384")
ioproperties.put(ProducerConfig.LINGER_MS_CONFIG,"70")
ioproperties.put(ProducerConfig.COMPRESSION_TYPE_CONFIG,"snappy")
ioproperties.put(ProducerConfig.BUFFER_MEMORY_CONFIG,"33554432")
ioproperties.put(ProducerConfig.ACKS_CONFIG,"-1")
ioproperties.put(ProducerConfig.RETRIES_CONFIG,"5")
ioproperties.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG,"true")
boker
replica.lag.time.max.ms=30s Leader未收到Follower的消息时间
auto.leader.rebalance.enable=false 自动平衡,建议关闭
leader.imbalance.per.broker.percentage=10%
leader.imbalance.check.interval.seconds=300s 负载均衡检查时间
log.segment.bytes=1g log日志大小
log.index.interval.bytes=4K 每4k大小数据创建一个索引
log.retention.hours 默认7天 log.retention.check.interval.ms 检查周期5分钟
log.cleanup.policy=delete 最大时间戳作为该文件的最大时间戳
log.cleanup.policy=compact
num.io.threads=8
num.replica.fetchers=1
num.network.threads
log.flush.interval.message
log.flush.interval.ms
消费者
enable.auto.commit=true
auto.commit.interval.ms=5s
auto.offset.reset=5s earliest:自动将偏移量重置为最早的偏移量 latest:最新的偏移量
none:如果未找到先前偏移量,则消费者抛出异常
offsets.topic.num.partitions=50
heartbeat.interval.ms=3s
session.timeout.ms=45s
max.poll.interval.ms=5m
fetch.max.bytes=52428800(50m)
max.poll.records=500
kafka总体调优
吞吐量
生产者吞吐:
ioproperties.put(ProducerConfig.BATCH_SIZE_CONFIG,"16384")
ioproperties.put(ProducerConfig.LINGER_MS_CONFIG,"70")
zstdioproperties.put(ProducerConfig.COMPRESSION_TYPE_CONFIG,"snappy")
ioproperties.put(ProducerConfig.BUFFER_MEMORY_CONFIG,"33554432")
增加分区
消费者吞吐:
fetch.max.bytes=52428800(50m)
max.poll.records=500
精确一次
生产者:ack=-1、幂等性+事务
broker:分区副本数>=2 ISR里应答>=2
消费者:事务+手动offset 消费者输出必须支持事务
设置分区
(1)创建一个分区压测。 目标吞吐量/min(生产者,消费者)(一般3-10个)
单条日志过大(1M以上)
message.max.bytes=1m
max.request.size=1m
replica.fetch.max.bytes=1m
fetch.max.bytes=50m
压测
生产者压测脚本;kafka-producer-perf-test.sh
./kafka-producer-perf-test.sh
batch.size=16384 linger.ms=0 compression.type=snappy
buffer.memory=67108864
消费者压测脚本;kafka-consumer-perf-test.sh
./kafka-consumer-perf-test.sh
bootstrap-server=localhost:9092
fetch.max.bytes=50m