c# - 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? 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? With a TaskScheduler or Multiple Threads or what?
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.
解决方案
一种简单的方法是,您可以拥有两个这样的控制台应用程序,然后将它们一起运行。
推荐阅读
- c# - RichTextBox - 如何突出显示行尾?
- php - 每天在特定时间自动触发 Mysql 查询
- c# - 为什么在某些情况下使用 LineRenderer 绘制的线条似乎很粗?
- javascript - 无法将来自 db.countDocuments 的数据存储在 mongoDB 中
- c++ - std::filesystem copy_file 失败
- java - 使用 GlobalSign DSS AATL 证书进行 iText7 pdf 签名显示 Adobe Reader 中的信任链损坏
- typescript - Typescript 不会从平面属性函数中推断出解析的属性
- c# - Unity 2D - 从边缘跌落时,跌落动画不起作用
- apache - Flask SocketIO + Apache
- typescript - 关于在类构造函数中有一个带有默认道具的参数对象的打字稿菜鸟问题