kafka服务器默认能接收的最大消息是,Kafka收发oversize数据

Kafka 默认消息大小限制为 1M,超过此限制会导致 RecordTooLargeException。解决方法包括:调整 Producer 的 max.request.size,设置大于消息大小;增加 server.properties 中的 message.max.bytes 以允许更大消息;增大 Consumer 的 max.partition.fetch.bytes,以便消费大消息。还需注意 replica.fetch.max.bytes 参数,确保其大于 max.message.bytes,以避免 replication 问题。通过 topic-level 的 max.message.bytes 可灵活处理不同 topic 的 oversize 消息,而无需重启集群。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Kafka 整个消息管道的默认口径是 1M,换句话说,默认 Producer 只能向 Kafka 发送大小不超过 1M 的消息,Kafka 内部也只能处理大小不超过 1M 的消息,Consumer 也只能消费大小不超过 1M 的消息。

如果发送 2M(为了方便计算,以下 1M=1000K)大小的数据,client 会报异常

1

Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.RecordTooLargeException: The message is 2000037 bytes when serialized which is larger than the maximum request size you have configured with the max.request.size configuration.

根据提示,在 Producer config 中设置 max.request.size 为 2M+(注意必须大于 2M,因为消息本身的 metadata 也会占用空间,比如上文日志中,一条包含 2M 数据的消息的大小是 2M+37 byte),但服务器返回异常

1

Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.RecordTooLargeException: The request included a message larger than the max message size the server will accept.

服务器无法接收这么大的消息。那么修改 Kafka 的 server.properties,添加 message.max.bytes=2M,然后重启 Kafka 集群。发送成功。

然后尝试消费消息,报错

1

Exception in thread "main" org.apache.kafka.common.errors.RecordTooLargeException: There are some messages at [Partition=Offset]: {test-1=6863354} whose size is larger than the fetch size 1048576 and hence cannot be ever returned. Increase the fetch size on the client (using max.partition.fetch.bytes), or decrease the maximum message size the broker will allow (using message.max.bytes).

根据提示,在 Consumer config 中设置 max.partition.fetch.bytes 为 2M。消费成功。

但还没完,这时发现 Kafka 的 bytes_out 异常的高,几乎逼近带宽极限,但这时并没有客户端在往 Kafka 里发送数据,查看 Kafka 日志,发现有异常

1

[2016-10-25 03:15:08,361] ERROR [ReplicaFetcherThread-0-1], Replication is failing due to a message that is greater than replica.fetch.max.bytes for partition [test,1]. This generally occurs when the max.message.bytes has been overridden to exceed this value and a suitably large message has also been sent. To fix this problem increase replica.fetch.max.bytes in your broker config to be equal or larger than your settings for max.message.bytes, both at a broker and topic level. (kafka.server.ReplicaFetcherThread)

原因是修改 message.max.bytes 之后,Kafka broker 已经能接收 2M 的数据,但 replication fetcher 尝试备份时失败了,因为 replica.fetch.max.bytes 控制了最大的备份消息 size 是 1M。由于 replication fetcher 会无限的重试备份,因此 bytes_out 会急剧升高。把 replica.fetch.max.bytes 设置成 2M,并重启 broker 之后,问题解决。

由于可以通过 topic-level 参数 max.message.bytes 来控制每个 topic 所能接收的最大消息 size,所以为了避免重启,可以将 replica.fetch.max.bytes 设置为一个比较大的值(比如 10M),以后如果有某个 topic 需要收发 oversize 的消息,只需要修改该 topic 的 max.message.bytes 就行,不需要重启集群。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值