在 Linux 环境下使用 Kafka 时,“丢消息”通常不是 Kafka 本身的 bug,而是配置、架构或使用方式导致。下面按常见原因 → 原理 → 解决方式系统说明。
acks=0(发完就不管)acks=all
producer.send() 是异步的,异常在 Callback 里producer.send(record, (meta, ex) -> {
if (ex != null) {
// 记录 / 重试
}
});
retries 太小 / 超时retries=Integer.MAX_VALUE
retry.backoff.ms=100
min.insync.replicas 配置过低acks=all
min.insync.replicas=1
min.insync.replicas=2
replication.factor=3
unclean.leader.election.enable=false
log.flush.interval.messages=10000
log.flush.interval.ms=1000
consumer.commitSync();
process(record);
process(record);
consumer.commitSync();
enable.auto.commit=trueenable.auto.commit=false
log.retention.hours=168
CooperativeStickyAssignorulimit -n
* soft nofile 100000
* hard nofile 100000
netstat -s | grep -i drop
dmesg | grep drop
如果你愿意,可以告诉我:
我可以帮你精准定位是哪一类丢消息。