RabbitMQ .NET客户端的CreateConnection有时在第一次通话后就很慢,虽然已经在听消息了

时间:2019-03-20 15:51:30

标签: c# .net rabbitmq

我在.NET Core 2.1应用程序中使用RabbitMQ.Client 5.1.0 NuGet软件包。它打开到同一RabbitMQ服务器的多个连接以侦听队列消息。我发现对ConnectionFactory.CreateConnection()的第一次呼叫总是非常快(大约100毫秒),但是在已经收听的情况下进行的后续呼叫有时可能需要3到10秒。

一些简单的代码可以重现该问题:

        for (var i = 1; i <= 10; ++i)
        {
            var factory = new ConnectionFactory
            {
                HostName = hostName,
                UserName = userName,
                Password = password
            };

            var stopwatch = Stopwatch.StartNew();
            var connection = factory.CreateConnection();
            Console.WriteLine($"[{i}] CreateConnection took {stopwatch.ElapsedMilliseconds:N0} ms");

            var channel = connection.CreateModel();
            channel.BasicQos(0, 200, false);

            var consumer = new EventingBasicConsumer(channel);

            channel.BasicConsume(queueName, false, consumer);
        }

(真正的应用程序在单独的线程上启动每个连接,但这似乎没有什么区别。)

典型输出:

[1] CreateConnection took 109 ms
[2] CreateConnection took 38 ms
[3] CreateConnection took 61 ms
[4] CreateConnection took 362 ms
[5] CreateConnection took 390 ms
[6] CreateConnection took 1,261 ms
[7] CreateConnection took 3,043 ms
[8] CreateConnection took 1,047 ms
[9] CreateConnection took 381 ms
[10] CreateConnection took 376 ms

我做错什么了吗,或者这看起来像是RabbitMQ客户端库的问题?

1 个答案:

答案 0 :(得分:0)

请注意,建立连接不是免费的,并且会受到服务器以及网络负载的影响。必须建立TCP套接字并完成AMQP握手。我建议进行数据包捕获以确认创建时间,并将其与TCP连接设置相关联。

这可能没关系,但是我看到的Stopwatch类的所有示例在从当前实例读取属性之前都调用Stop方法。


注意: RabbitMQ团队监视rabbitmq-users mailing list,并且有时仅在StackOverflow上回答问题。