首页 > 解决方案 > Kafka:序列化时的消息大于您使用 max.request.size 配置配置的最大请求大小

问题描述

收到以下错误(Kafka 2.1.0):

2018-12-03 21:22:37.873 错误 37645 --- [nio-8080-exec-1] osksupport.LoggingProducerListener:发送带有 key='null' 和 payload='{82, 73, 70 的消息时抛出异常, 70, 36, 96, 19, 0, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1, 0, 68, -84,.. .' to topic recieved_sound: org.apache.kafka.common.errors.RecordTooLargeException: 序列化时消息为 1269892 字节,大于您使用 max.request.size 配置配置的最大请求大小。

我尝试了各种 SO 帖子中的所有建议。

我的 Producer.properties:

max.request.size=41943040
message.max.bytes=41943040
replica.fetch.max.bytes=41943040
fetch.message.max.bytes=41943040

服务器属性:

socket.request.max.bytes=104857600
message.max.bytes=41943040
max.request.size=41943040
replica.fetch.max.bytes=41943040
fetch.message.max.bytes=41943040

生产者配置(Spring Boot):

configProps.put("message.max.bytes", "41943040");
configProps.put("max.request.size", "41943040");
configProps.put("replica.fetch.max.bytes", "41943040");
configProps.put("fetch.message.max.bytes", "41943040");

消费者配置(SpringBoot):

props.put("fetch.message.max.bytes", "41943040");
props.put("message.max.bytes", "41943040");
props.put("max.request.size", "41943040");
props.put("replica.fetch.max.bytes", "41943040");
props.put("fetch.message.max.bytes", "41943040");

我还在最后 2 个文件中将字符串更改为数字。多次启动代理,并创建新主题。最初我遇到了org.apache.kafka.common.errors.RecordTooLargeException: The request included a message larger than the max message size the server will accept错误,这些更改已解决,但这个新错误仍然没有运气。

标签: apache-kafkakafka-consumer-apikafka-producer-apispring-kafka

解决方案


设置断点KafkaProducer.ensureValidRecordSize()以查看发生了什么。

有了这个应用程序

@SpringBootApplication
public class So53605262Application {

    public static void main(String[] args) {
        SpringApplication.run(So53605262Application.class, args);
    }

    @Bean
    public NewTopic topic() {
        return new NewTopic("so53605262", 1, (short) 1);
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate<String, String> template) {
        return args -> template.send("so53605262", new String(new byte[1024 * 1024 * 2]));
    }

}

我明白了

序列化时消息为 2097240 字节,大于您使用 max.request.size 配置配置的最大请求大小。

正如预期的那样;当我添加

spring.kafka.producer.properties.max.request.size=3000000

(这相当于您的配置,但使用 Spring Boot 属性),我得到

请求包含的消息大于服务器将接受的最大消息大小。

如果调试没有帮助,也许您可​​以发布一个完整的小应用程序来展示您所看到的行为。


推荐阅读