文章目录
若文章对你有帮助请点个👍👍👍, 表示对我的鼓励, 非常感谢 ❤️❤️❤️
⚠️ 本文实验的Kafka版本为2.11版本.
消息概述
kafka中的消息指的就是一条ProducerRecord,里面除了携带发送的数据之外,还包含:
- topic 发往的Topic
- partition 发往的分区
- headers 头信息
- key 数据
- value 数据
- timestamp-long 时间戳
Producer生产消息过长
在生产者发送消息的时候,并不是上面所有的信息都算在发送的消息大小.详情见下面代码.
上面的代码会将value序列化成字节数组,参与序列化的有topic,headers,key. 用来验证value是否超出长度的是ensureValidRecordSize(serializedSize)
方法.
ensureValidRecordSize从两个方面验证,一个是maxRequestSize(max.request.size)
,另一个是totalMemorySize(buffer.memory)
, 只有当value的长度同时小于,消息才可以正常发送.
private void ensureValidRecordSize(int size) {
if (size > this.maxRequestSize)
throw new RecordTooLargeException("The message is " + size +
" bytes when serialized which is larger than the maximum request size you have configured with the " +
ProducerConfig.MAX_REQUEST_SIZE_CONFIG +
" configuration.");
if (size > this.totalMemorySize)
throw new RecordTooLargeException("The message is " + size +
" bytes when serialized which is larger than the total memory buffer you have