简体   繁体   English

多个 RabbitMQ 消费者 DotNet Core

[英]Multiple RabbitMQ Consumers DotNet Core

We have an application that needs to have multiple consumers for the same query.我们有一个应用程序需要为同一个查询提供多个消费者。 The Data is of such nature what the "Competing Consumer" is not a problem as the messages can get processed in any order and independent.数据具有这样的性质,“竞争消费者”不是问题,因为消息可以按任何顺序独立处理。 My question is, as RabbitMQ does its own Thread Management etc, what is the best approach to create multiple consumers picking messages of the same queue?我的问题是,由于 RabbitMQ 有自己的线程管理等,创建多个消费者选择同一队列的消息的最佳方法是什么? Looking at this basic example of a simple consumer:查看这个简单消费者的基本示例:

var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
    channel.QueueDeclare(queue: "hello",
                            durable: false,
                            exclusive: false,
                            autoDelete: false,
                            arguments: null);

    var consumer = new EventingBasicConsumer(channel);
    consumer.Received += (model, ea) =>
    {
        var body = ea.Body.ToArray();
        var message = Encoding.UTF8.GetString(body);
        Console.WriteLine(" [x] Received {0}", message);
    };
    channel.BasicConsume(queue: "hello",
                            autoAck: true,
                            consumer: consumer);

    Console.WriteLine(" Press [enter] to exit.");
    Console.ReadLine();
}

I believe multiple instances of channel.BasicConsume(...) needs to be created, but what is the best way to do that?我相信需要创建channel.BasicConsume(...)多个实例,但最好的方法是什么? With a TaskScheduler or Multiple Threads or what?使用TaskScheduler或多线程还是什么?

I can introduce service layers and tidy everything up but just need to know what is the safest way to go about it.我可以介绍服务层并整理所有内容,但只需要知道最安全的方法是什么。

All the reaseach I found so far points not not creating multiple consumers but in our case its a requirement.到目前为止,我发现的所有研究点都不是创建多个消费者,而是在我们的案例中这是一个要求。

一种简单的方法是,你可以有两个这样的控制台应用程序,然后一起运行它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM