Kafka Producer(具有多个实例)写入同一主题

Kafka Producer (with multiple instance) writing to same topic

我有一个用例,其中消息来自一个通道,我们希望将其推送到 Kafka 主题(多个分区)中。在我们的例子中,消息顺序很重要,所以我们必须按照接收消息的顺序将消息推送到主题,如果我们只有一个 producer 和单个分区,这看起来非常简单。在我们的例子中,为了负载平衡和 可扩展性 我们希望 运行 多个实例用于相同的 producer 但问题是如何维护 的顺序消息.

任何想法或解决方案都会很有帮助。

即使我认为只有一个分区,它是否可以复制到多个代理以实现可用性和容错?

we have to push the messages to topic in the order they are received which looks very straight forward if we have only one producer and single partition

您可以在一个生产者的主题中有多个分区,如果您为您的消息提供密钥,则仍然可以保持顺序。由单个生产者生成的具有相同密钥的所有消息总是有序的。


当您说 多个生产者 时,我假设您有多个应用程序实例 运行 并且您没有在同一个 JVM 实例中创建多个生产者.

既然你说了 channel,我想它是一个网络通道,例如数据报通道。在那种情况下,我假设您正在监听某个端口并将接收到的数据发送到 Kafka。

I do not see a point in having multiple producers in the same instance producing to the same topic, so it is better to have a single producer send all the messages and for performance you can tune the producer properties like batch.size, linger.ms etc.

为了实现容错,让另一个实例 运行 处于 HA 模式(故障转移模式),这样如果这个实例死亡,另一个实例会自动恢复。

If it is a network channel, you can run multiple instances and open the socket with the option SO_REUSEADDR in StandardSocketOptions and this way you only one producer will be active at any point and new producer will become active once the active one dies.