卡夫卡花了太多时间开始阅读

时间:2018-01-29 14:59:01

标签: c# .net apache-kafka kafka-consumer-api confluent-kafka

目前我正在使用Confluent kafka nuget包,但不要使用Confluent平台本身(https://github.com/confluentinc/confluent-kafka-dotnet)。

根据应用程序需求,需要创建使用者,获取第一条消息并关闭使用者。整个周期应该尽快完成(最多1秒)。 我遇到的问题 - 读取第一条消息需要很多poll()周期(数据已经生成并存在于主题中):

使用subscribe()方法时大约8秒(因为重新平衡) 使用assign()(没有重新平衡)时约5秒

我在消费者方面尝试了很多配置(因为生产者没有参与其中),但没有任何帮助。 这是预期的行为吗?也许需要在经纪人方面配置一些东西?

您可以在下面找到我的简单消费者的代码:

class Program
{
    private static Dictionary<string, object> _config =>
        new Dictionary<string, object>
        {
            { "group.id", "test-consumer" },
            { "enable.auto.commit", false },
            { "bootstrap.servers", "192.168.56.102:9092" },
            { "default.topic.config", new Dictionary<string, object>()
                {
                    { "auto.offset.reset", "smallest" }
                }
            }
        };


    static void Main(string[] args)
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        var consumer = new Consumer<Ignore, string>(_config, null, new StringDeserializer(Encoding.UTF8));
        consumer.Assign(new List<TopicPartition> {new TopicPartition("TestQueue", 0)});
        Message<Ignore, string> msg = null;
        bool cancel = false;
        consumer.OnMessage += (sender, message) =>
        {
            msg = message;
            cancel = true;
        };

        while (!cancel)
        {
            consumer.Poll(100);
        }

        consumer.CommitAsync(msg);
        consumer.Dispose();
        sw.Stop();
    }
}

0 个答案:

没有答案