Kafka - 代理:消息大小太大

Kafka - Broker: Message size too large

当我尝试发送超过 1 Mb 大小的邮件时,出现 Message size too large 异常。当我尝试生成消息时,错误出现在我的客户端应用程序中。经过一番谷歌搜索后,我发现应该更改设置以增加最大消息大小。好吧,我在 /kafka/config/server.properties 文件中这样做了。我添加了接下来的 2 个设置:

message.max.bytes=15728640
replica.fetch.max.bytes=15728640

此外,我在 /kafka/config/consumer.properties 文件中添加了 fetch.message.max.bytes=15728640。所有其他设置保持默认。

我重启了 kafka 服务器,但我仍然遇到同样的错误。

P.S Kafka 版本是1.1.0.

您的配置正确,但您还需要在生产者端设置 max.request.size

props.put(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, 15728640);

max.request.size The maximum size of a request in bytes. This setting will limit the number of record batches the producer will send in a single request to avoid sending huge requests. This is also effectively a cap on the maximum record batch size.

在 Broker 端,您已经配置了应该起作用的以下参数

message.max.bytes The largest record batch size allowed by Kafka.

replica.fetch.max.bytes The number of bytes of messages to attempt to fetch for each partition. This is not an absolute maximum if the first record batch in the first non-empty partition of the fetch is larger than this value, the record batch will still be returned to ensure that progress can be made. The maximum record batch size accepted by the broker is defined via message.max.bytes (broker config) or max.message.bytes (topic config).

在主题端 max.message.bytes 如果您已经在代理端设置 message.max.bytes 则不需要

max.message.bytes - this is the largest size of the message the broker will allow being appended to the topic. This size is validated pre-compression. (Defaults to broker's message.max.bytes.)

参考 https://kafka.apache.org/documentation/

我认为您可能还需要设置 batch.size

我遇到了类似的问题。我用 max.message.bytes=4096 创建了一个主题,我用 kafka-producer-perf-test.sh 进行测试。我也设置了 max.request.size=4096,但它似乎工作正常。

如果我只发送一条消息,没问题。

./kafka/bin/kafka-producer-perf-test.sh --producer-props bootstrap.servers=localhost:9092 max.request.size=4096 --record-size 4008 --topic test-4K-1RF-topic-0 --num-records 1 --throughput 1

如果我只发送不止一条消息,我将收到 MESSAGE_TOO_LARGE 错误。

./kafka/bin/kafka-producer-perf-test.sh --producer-props bootstrap.servers=localhost:9092 max.request.size=4096 --record-size 4008 --topic test-4K-1RF-topic-0 --num-records 2 --throughput 1

所以我想这与批量大小有关。然后,我发现 batch.size

下面的代码片段现在可以使用了!

./kafka/bin/kafka-producer-perf-test.sh --producer-props bootstrap.servers=localhost:9092 batch.size=4096 --record-size 4008 --topic test-4K-1RF-topic-0 --num-records 200 --throughput 10

编辑:我认为max.request.size是为了限制producer发送给Kafka broker的包的大小。一个包可以包含多个批次。批量大小受 batch.size.

限制