【问题标题】:Random partitioner does not distribute messages between Kafka topic partitions随机分区器不会在 Kafka 主题分区之间分发消息
【发布时间】:2019-08-18 09:36:32
【问题描述】:

我在 Kafka 中创建了一个包含 9 个分区的主题,将其命名为“测试”,并使用 Confluent.Kafka 客户端库将两个简单的 C# (.NET Core) 应用程序组合在一起:生产者和消费者。我只是调整了examples from the documentation

我正在运行消费者应用程序的两个实例和生产者应用程序的一个实例。我认为在此处粘贴消费者代码没有多大意义,这是一个微不足道的“获取消息,在屏幕上打印”应用程序,但是,它也打印消息来自的分区号。

这是生产者应用:

    static async Task Main(string[] args)
    {
        var random = new Random();

        var config = new ProducerConfig {
            BootstrapServers = "10.0.0.5:9092",
            Partitioner = Partitioner.ConsistentRandom
        };

        int counter = 0;
        while (true)
        {
            using (var p = new ProducerBuilder<string, string>(config).Build())
            {
                try
                {
                    p.BeginProduce(
                        "test",
                        new Message<string, string>
                        {
                            //Key = random.Next().ToString(),
                            Value = $"test {++counter}"
                        });

                    if (counter % 10 == 0)
                        p.Flush();
                }
                catch (ProduceException<Null, string> e)
                {
                    Console.WriteLine($"Delivery failed: {e.Error.Reason}");
                }
            }
        }
    }

问题:如果未设置消息的Key 属性,则所有消息都会发送到7 号分区,这意味着我的消费者实例之一处于空闲状态。我必须手动随机化密钥以便在分区之间分配它们(参见注释掉的行)。 (从文档中复制的原始代码使用 Null 作为键的类型,这也将所有消息发送到了第 7 个分区。)

这是为什么呢?根据ProducerConfig.Partitioner 属性的文档,如果未指定密钥,consistent_random 选项应确保随机分布。我尝试使用 Partioner.Random 选项,无论密钥如何,它都应该使用随机分布,但这没有帮助。

这是预期的行为,是我做错了什么,还是遇到了错误?

我正在使用 Confluent.Kafka NuGet 的 1.0.0-RC2 版本。

Partitioner 配置的完整文档:

// Summary:
//     Partitioner: `random` - random distribution, `consistent` - CRC32 hash of key
//     (Empty and NULL keys are mapped to single partition), `consistent_random` - CRC32
//     hash of key (Empty and NULL keys are randomly partitioned), `murmur2` - Java
//     Producer compatible Murmur2 hash of key (NULL keys are mapped to single partition),
//     `murmur2_random` - Java Producer compatible Murmur2 hash of key (NULL keys are
//     randomly partitioned. This is functionally equivalent to the default partitioner
//     in the Java Producer.). default: consistent_random importance: high

【问题讨论】:

  • 我认为项目中不会有错误。您可以添加指向 Partitioner 配置文档的链接吗?
  • @JRibkr 是的,我也不认为这是一个错误,肯定会发现一些如此明显的东西。我从库本身的 XML 文档中复制了文档,如在 Visual Studio 中所见,我不知道是否有在线版本可用。谷歌搜索只产生了这个源文件:github.com/confluentinc/confluent-kafka-dotnet/blob/master/src/…

标签: c# apache-kafka confluent-platform


【解决方案1】:

我遇到了同样的问题。 似乎在启动客户端时,第一条消息将始终进入同一个分区。 如果您对所有消息使用相同的客户端,Partioner.Random 将起作用

【讨论】:

  • 我遇到了同样的问题,使用ProducerBuilder&lt;Null, string&gt;,设置Partitioner = Partitioner.Random没有任何效果。所有消息都生成到单个分区。 @kamilk 或@Ran Mansoor,你有任何可以分享的工作代码吗?
猜你喜欢
  • 2016-04-18
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 2016-01-21
  • 2017-10-17
  • 2016-10-15
  • 1970-01-01
相关资源
最近更新 更多