【问题标题】:Kafka: The message when serialized is larger than the maximum request size you have configured with the max.request.size configurationKafka:序列化时的消息大于您使用 max.request.size 配置配置的最大请求大小
【发布时间】:2019-05-05 10:14:04
【问题描述】:

出现以下错误(Kafka 2.1.0):

2018-12-03 21:22:37.873 错误 37645 --- [nio-8080-exec-1] o.s.k.support.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

Server.properties:

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

ProducerConfig(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");

ConsumerConfig (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 错误,这些更改已修复,但这个新错误仍然没有运气。

【问题讨论】:

  • 您的配置看起来不错。我猜也许您没有将更改部署到所有经纪人? Can you check the broker config using bin/kafka-configs.sh 以确保您的配置在所有代理上都正确?
  • 另加max.partition.fetch.bytes
  • max.partition.fetch.bytes 是一个软限制。来自文档:If the first record batch in the first non-empty partition of the fetch is larger than this limit, the batch will still be returned to ensure that the consumer can make progress.
  • 您可能需要将 kaka.max.partition.fetch.bytes 而不是 max.partition.fetch.bytes 添加到客户端属性

标签: apache-kafka kafka-consumer-api kafka-producer-api spring-kafka


【解决方案1】:

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 属性),我明白了

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

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

【讨论】:

    【解决方案2】:

    如果 Kafka 属性是服务器上的文件,您可以更改消息大小。

    对于默认的sever.property 文件

    #/usr/local/kafka/config
    #message.max.bytes=26214400
    

    producer.properties->

    # the maximum size of a request in bytes
    # max.request.size=26214400
    

    同样适用于消费者

    【讨论】:

      【解决方案3】:

      你应该这样设置生产者的配置

      Props.put(ConsumerConfig.FETCH_MAX_BYTES_CONFIG, "41943040");
      

      【讨论】:

      • 这是 Props.put(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, "41943040");
      猜你喜欢
      • 2022-01-25
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2020-10-06
      • 1970-01-01
      • 2018-10-16
      • 1970-01-01
      相关资源
      最近更新 更多