解决 Kafka 报错 RecordTooLargeException 超大消息发送拒绝修复
排查 Kafka 生产端抛出 RecordTooLargeException: The message is ... bytes and the largest record batch allowed is ...,分析全链路体积参数。
问题现象
生产端发送单条大报文或图片数据时,Kafka 发送直接失败:
org.apache.kafka.common.errors.RecordTooLargeException: The message is 2097152 bytes and the largest record batch allowed by the broker by default is 1048576 bytes.
触发根因
- Broker 端的
message.max.bytes限制为默认 1MB; - 生产端客户端的
max.request.size限制同样为 1MB; - Topic 级别的
max.message.bytes配置覆盖限制了最大批次。
解决步骤
调整 Broker 配置文件 server.properties
增加最大允许消息长度至 10MB:
message.max.bytes=10485760
修改 Producer 客户端配置
放宽客户端发送限制:
max.request.size=10485760
动态调整目标 Topic 的配置
执行管理脚本更新:
kafka-configs.sh --bootstrap-server localhost:9092 --alter --entity-type topics --entity-name big-events --add-config max.message.bytes=10485760
验证确认
重新发送 2MB 报文:
成功落盘并生成对应 Partition Offset,无任何超限拦截。


