用于RabbitMQ的ASP.NET CORE WebApi消息消耗性能问题

时间:2018-01-04 20:28:22

标签: performance asp.net-core rabbitmq

我在我的asp.net核心webApi中使用.net客户端用于rabbitMQ。我连接到交换机,声明队列然后开始通过一个频道消费它。这里的问题是,当我发布一个"大"与此特定队列同时消息的数量,消耗性能保持非常低(大约每秒4条消息)。

我已经搜索了一些可以改善消费性能的配置,但它们都没有真正有效,我开始认为我可能误解了在这种情况下正确使用rabbitMQ。我试图增加打开的频道和消费者实例的数量,但它有所改善,但没有达到我对RabbitMQ的预期。我也尝试增加预取数量,但没有明确的改进。

以下是我负责消费的代码(在我修改以改善性能之前):

Sub MakeFolders()
Dim Rng As Range
Dim maxRows, maxCols, r, c As Integer
Set Rng = Selection
maxRows = Rng.Rows.Count
maxCols = Rng.Columns.Count
For c = 1 To maxCols
r = 1
Do While r <= maxRows
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))
On Error Resume Next
End If
r = r + 1
Loop
Next c
End Sub

修改后(更多频道,更多消费者和预取计数):

public void Receive(string hostName, string queueName, string type, string routingKey, string exchange)
{
    //RabbitMQ configuration
    var userName = _configuration["Messaging:UserName"];
    var password = _configuration["Messaging:Password"];
    var prefetchQuantity = _configuration["Messaging:PrefetchQuantity"];

    var factory = new ConnectionFactory() { HostName = hostName, UserName = userName, Password = password };
    var connection = factory.CreateConnection();

    var channel = connection.CreateModel();

    channel.BasicQos(0, Convert.ToUInt16(prefetchQuantity), false);

    //Declaring the exchange
    channel.ExchangeDeclare(exchange, ExchangeType.Direct);

    //Declaring the queue and creating its routing key bindings
    channel.QueueDeclare(queueName, true, false, false, null);

    channel.QueueBind(queueName, exchange, routingKey);

    var consumer = new EventingBasicConsumer(channel);

    consumer.Received += (model, e) =>
    {
        try
        {
            mediatorHandler.Handle(model, e, _mediator);

            channel.BasicAck(e.DeliveryTag, false);
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
            channel.BasicNack(e.DeliveryTag, false, true);
        }
    };

    channel.BasicConsume(queue: queueName,
        autoAck: false,
        consumer: consumer);
}

在这种情况下如何改善消费表现?

谢谢!

0 个答案:

没有答案