1

我正在尝试将 kafka 的默认消息大小从 1MB 增加到 10MB。我正在使用 EmbeddedKafka 和 ScalaTest 测试我的新配置,但它不起作用。

使用这个答案,我相应地增加了配置值:

经纪人:

  • 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 中的错误吗?还是我错误地配置了我的应用程序?

4

1 回答 1

0

我发现了这个问题。这是由于EmbeddedKafka.

beforeAll在我使用默认配置进行测试之前,有一个启动 EmbeddedKafka 的声明。EmbeddedKafka.start需要更新的代理设置,在启动后将它们传递给发布EmbeddedKafka什么都不做。

于 2018-07-16T16:13:46.863 回答