在 Debian 上运行 Kafka 时,“调优”通常分为 系统层(OS / 文件系统 / 网络)、JVM 层、Kafka Broker 配置层、Topic / 生产消费层 四个维度。下面给你一份比较实用的调优清单(以 Debian 11/12 + Kafka 3.x 为例)。
/dev/sdb1 /kafka xfs defaults,noatime,nodiratime 0 0
log.dirs=/kafka1,/kafka2,/kafka3ulimit -n 100000
永久生效:
/etc/security/limits.conf
kafka soft nofile 100000
kafka hard nofile 100000
sysctl vm.swappiness=1
net.core.somaxconn = 4096
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1
sysctl -p
export KAFKA_HEAP_OPTS="-Xms6g -Xmx6g"
Kafka 3.x 推荐 G1GC
export KAFKA_JVM_PERFORMANCE_OPTS="
-Xms6g -Xmx6g
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:InitiatingHeapOccupancyPercent=35
"
配置文件:/etc/kafka/server.properties
num.network.threads=8
num.io.threads=16
num.replica.fetchers=4
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.flush.interval.messages=10000
log.flush.interval.ms=1000
高吞吐场景可放宽,依赖 OS page cache
log.retention.hours=72
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
default.replication.factor=3
min.insync.replicas=2
unclean.leader.election.enable=false
kafka-topics.sh --create \
--topic test \
--partitions 12 \
--replication-factor 3
kafka-topics.sh --alter --topic test \
--config cleanup.policy=delete
acks=1
batch.size=16384~131072
linger.ms=5~20
compression.type=lz4
buffer.memory=67108864
fetch.min.bytes=1048576
fetch.max.wait.ms=500
max.poll.records=500
kafka-run-class.sh kafka.tools.JmxTool| 问题 | 可能原因 |
|---|---|
| 吞吐上不去 | 分区太少 |
| 延迟高 | acks=all + min.insync |
| CPU 高 | GC / 压缩 |
| 磁盘 IO 高 | flush 太频繁 |
| OOM | 堆太大 |
如果你愿意,可以告诉我:
我可以给你一份 直接可抄的 server.properties 配置。