kafka生成者发送消息失败报错:RecordTooLargeException
kafka生成者发送消息典型案例
生产者 发送消息失败:Failed to send; nested exception is org.apache.kafka.common.errors.RecordTooLargeException: The message is 1053512 bytes when serialized which is larger than the maximum request size you have configured with the max.request.size configuration.
原因:序列化时,消息为1053512字节,大于您使用max.request.size配置配置的最大请求大小。
因kafka默认生成者发送消息的大小为:1048576 即为1M,当发送的消息大于1M的时候就会拒绝发送,导致生产者发送消息失败。
分析:
当在yml里面配置kafka打印详情日志发现,默认的大小的确为:1048576 即为1M
logging:
level.org.apache.kafka: DEBUG
所以就要在yml里面配置大于1M的配置属性
spring:
kafka:
producer:
properties:
# 根据需求调整(例如设为 2MB) 2MB = 2 * 1024 * 1024
max.request.size: 2097152
重启服务,即可发现日志有打印发送成功:生产者 发送消息成功:SendResult
kafka消费端对应也需要配置下,不然也会发送失败,在kafka配置文件config/server.properties下,新增配置如下:
message.max.bytes=2097152
replica.fetch.max.bytes=2097152
记得重启kafka服务