即使设置了配置值,EmbeddedKafka也会引发RecordTooLargeException

时间:2018-07-13 09:50:10

标签: scala apache-kafka embedded-kafka

我正在尝试将kafka的默认邮件大小从1MB增加到10MB。我正在使用EmbeddedKafka和ScalaTest测试我的新配置,但无法正常工作。

使用此answer,我相应地增加了配置值:

经纪人:

  • message.max.bytes
  • replica.fetch.max.bytes

消费者:

  • max.partition.fetch.bytes

制作人:

  • max.request.size

我的代码:

  val broker = s"localhost:${kafkaConfig.kafkaPort}"
  val maxSize: String = (ConsumerConfig.DEFAULT_MAX_PARTITION_FETCH_BYTES * 10).toString // 10MiB

  val embeddedBrokerConfig = Map(
    "message.max.bytes" -> maxSize,
    "replica.fetch.max.bytes" -> maxSize
  )

  val embeddedConsumerConfig = Map[String, String](
    ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG -> broker,
    ConsumerConfig.GROUP_ID_CONFIG -> consumerGroup,
    ConsumerConfig.AUTO_OFFSET_RESET_CONFIG -> "earliest",
    ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG -> "false",
    ConsumerConfig.MAX_PARTITION_FETCH_BYTES_CONFIG -> maxSize
  )

  val embeddedProducerConfig = Map[String, String](
    ProducerConfig.BOOTSTRAP_SERVERS_CONFIG -> broker,
    ProducerConfig.MAX_REQUEST_SIZE_CONFIG -> maxSize
  )

  val bigKafkaConfig =
    EmbeddedKafkaConfig(
      kafkaConfig.kafkaPort,
      kafkaConfig.zooKeeperPort,
      customBrokerProperties = embeddedBrokerConfig,
      customConsumerProperties = embeddedConsumerConfig,
      customProducerProperties = embeddedProducerConfig
    )

  val bigMessage =  ("H" * 999999).getBytes()

  EmbeddedKafka.publishToKafka(inTopic, bigMessage)(bigKafkaConfig, valueSerializer)

当我使用仅999999字节(小于1MB)的消息运行此代码时,出现此错误:

Caused by: 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.
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.valueOrError(FutureRecordMetadata.java:94)
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:77)
at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:29)
at net.manub.embeddedkafka.EmbeddedKafkaSupport.$anonfun$publishToKafka$7(EmbeddedKafka.scala:276)
at scala.util.Try$.apply(Try.scala:209)
at net.manub.embeddedkafka.EmbeddedKafkaSupport.publishToKafka(EmbeddedKafka.scala:276)

这是EmbeddedKafka中的错误吗?还是我配置了错误的应用程序?

1 个答案:

答案 0 :(得分:0)

我发现了问题。这是由于EmbeddedKafka的配置。

在使用默认配置进行测试之前,有一个beforeAll语句启动EmbeddedKafka。 EmbeddedKafka.start需要更新的代理设置,EmbeddedKafka启动后将其传递给发布,则无济于事。