RabbitMQ 官网
工作队列有两种工作模式,分别是【循环分发】和【公平分发】;
循环分发:RabbitMQ的消息分发默认按照消费端的数量,按顺序循环分发,也称为轮询。使用工作队列的好处就是它能够并行的处理队列。如果堆积了很多任务,只需要添加更多的工作者(workers)就可以了。
公平分发(Fair Dispatch):消费者1从队列中获取一条消息之后,处理完消息之后手动发送反馈回执,告诉RabbitMQ我处理完了,接着就会再从队列中获取一条消息 ;在消费者1获取消息后处理业务之时,消费者2也会从队列中获取一条消息之后,处理完消息之后同样会手动发送反馈回执,告诉RabbitMQ我处理完了,接着也会再从队列中获取一条消息。两个消费者(消费者1&消费者2)不需要彼此等待对方处理完业务才能获取消息,这样一来业务处理时间短的消费者就能多处理消息,做到“能者多劳”,提高工作效率。
【注意1:保证消费者一次只接收一条消息channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false)】
【注意2:使用公平分发需要消费者手动反馈,所以必须关闭自动应答(ack 改为手动)】
一个生产者,多个消费者
1、工作队列--轮询模式--生产者
using RabbitMQ.Client;
using System;
using System.Text;
namespace WorkSendConsole
{
class Program
{
///
/// 工作队列--轮询模式--生产者
///
///
public static void Main(string[] args)
{
///队列名称
string queueName = "work_rr";
///1. 创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
HostName = "127.0.0.1",//rabbitmq ip
Port = 5672,//端口号
UserName = "guest",//用户名
Password = "guest",//密码
//VirtualHost="/tj"//虚拟主机
};
///2. 创建连接对象
using (IConnection connection = factory.CreateConnection())
{
///3. 创建连接会话对象,信道
using (IModel channel = connection.CreateModel())
{
///4. 声明一个队列,绑定队列
channel.QueueDeclare(
queue: queueName,//队列名称
durable: false,//持久化
exclusive: false,//排他队列
autoDelete: false,//队列自动删除
arguments: null);
for (int i = 0; i < 20; i++)
{
///消息内容
string message = $"Hello World {i} !!!";
///5. 发送消息
channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: Encoding.UTF8.GetBytes(message));
Console.WriteLine(" [x] Sent {0}", message);
}
}
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
2、工作队列--轮询模式--消费者1
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
namespace WorkReceiveFirstConsole
{
class Program
{
///
/// 工作队列--轮询--消费者
///
///
public static void Main(string[] args)
{
///队列名称
string queueName = "work_rr";
///1. 创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
HostName = "127.0.0.1",
Port = 5672,//端口号
UserName = "guest",//用户名
Password = "guest",//密码
//VirtualHost = "/tj"
};
///2. 创建连接对象
using (var connection = factory.CreateConnection())
{
///3. 创建连接会话对象
using (var channel = connection.CreateModel())
{
///4. 声明一个队列,绑定队列
channel.QueueDeclare(
queue: queueName,//消息队列名称
durable: false,//持久化
exclusive: false,//排他队列
autoDelete: false,//自动删除
arguments: null);
///事件基本消费者
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
///接收到消息事件
consumer.Received += (model, ea) =>
{
///打印消息
Console.WriteLine(" [x] Received {0}", Encoding.UTF8.GetString(ea.Body.ToArray()));
};
///消费消息
channel.BasicConsume(
queueName, //队列名称
true, //自动确认
consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
}
}
3、工作队列--轮询模式--消费者2
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
namespace WorkReceiveSecondConsole
{
class Program
{
///
/// 工作队列--轮询--消费者
///
///
public static void Main(string[] args)
{
///队列名称
string queueName = "work_rr";
///1. 创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
HostName = "127.0.0.1",
Port = 5672,//端口号
UserName = "guest",//用户名
Password = "guest",//密码
//VirtualHost = "/tj"
};
///2. 创建连接对象
using (var connection = factory.CreateConnection())
{
///3. 创建连接会话对象
using (var channel = connection.CreateModel())
{
///4. 声明一个队列,绑定队列
channel.QueueDeclare(
queue: queueName,//消息队列名称
durable: false,//持久化
exclusive: false,//排他队列
autoDelete: false,//自动删除
arguments: null);
///事件基本消费者
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
///接收到消息事件
consumer.Received += (model, ea) =>
{
///打印消息
Console.WriteLine(" [x] Received {0}", Encoding.UTF8.GetString(ea.Body.ToArray()));
};
///消费消息
channel.BasicConsume(
queueName, //队列名称
true, //自动确认
consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
}
}
4、工作队列--公平模式--生产者
using RabbitMQ.Client;
using System;
using System.Text;
namespace WorkFairSendConsole
{
class Program
{
///
/// 工作队列--公平模式--生产者
///
///
public static void Main(string[] args)
{
///队列名称
string queueName = "work_fair";
///1. 创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
HostName = "127.0.0.1",//rabbitmq ip
Port = 5672,//端口号
UserName = "guest",//用户名
Password = "guest",//密码
//VirtualHost="/tj"//虚拟主机
};
///2. 创建连接对象
using (IConnection connection = factory.CreateConnection())
{
///3. 创建连接会话对象,信道
using (IModel channel = connection.CreateModel())
{
///4. 声明一个队列,绑定队列
channel.QueueDeclare(
queue: queueName,//队列名称
durable: false,//持久化
exclusive: false,//排他队列
autoDelete: false,//队列自动删除
arguments: null);
for (int i = 0; i < 20; i++)
{
///消息内容
string message = $"Hello World {i} !!!";
///5. 发送消息
channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: Encoding.UTF8.GetBytes(message));
Console.WriteLine(" [x] Sent {0}", message);
}
}
}
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
5、工作队列--公平模式--消费者1
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
namespace WorkFairReceiveFirstConsole
{
class Program
{
///
/// 工作队列--公平模式--消费者
///
///
public static void Main(string[] args)
{
///队列名称
string queueName = "work_fair";
///1. 创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
HostName = "127.0.0.1",
Port = 5672,//端口号
UserName = "guest",//用户名
Password = "guest",//密码
//VirtualHost = "/tj"
};
///2. 创建连接对象
using (var connection = factory.CreateConnection())
{
///3. 创建连接会话对象
using (var channel = connection.CreateModel())
{
///4. 声明一个队列,绑定队列
channel.QueueDeclare(
queue: queueName,//消息队列名称
durable: false,//持久化
exclusive: false,//排他队列
autoDelete: false,//自动删除
arguments: null);
///限制消费1条消息,消费完在继续下一条消息
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
///事件基本消费者
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
///接收到消息事件
consumer.Received += (model, ea) =>
{
///打印消息
Console.WriteLine(" [x] Received {0}", Encoding.UTF8.GetString(ea.Body.ToArray()));
/// 注意:手动确认
channel.BasicAck(
deliveryTag: ea.DeliveryTag, //回调的消息的唯一标识
multiple: false //是否确认多条
);
};
///消费消息
channel.BasicConsume(
queueName, //队列名称
false, //注意:【自动确认】改为【手动确认】
consumer
);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
}
}
6、工作队列--公平模式--消费者2
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
namespace WorkFairReceiveSecondConsole
{
class Program
{
///
/// 工作队列--公平模式--消费者
///
///
public static void Main(string[] args)
{
///队列名称
string queueName = "work_fair";
///1. 创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
HostName = "127.0.0.1",
Port = 5672,//端口号
UserName = "guest",//用户名
Password = "guest",//密码
//VirtualHost = "/tj"
};
///2. 创建连接对象
using (var connection = factory.CreateConnection())
{
///3. 创建连接会话对象
using (var channel = connection.CreateModel())
{
///4. 声明一个队列,绑定队列
channel.QueueDeclare(
queue: queueName,//消息队列名称
durable: false,//持久化
exclusive: false,//排他队列
autoDelete: false,//自动删除
arguments: null);
///限制消费1条消息,消费完在继续下一条消息
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
///事件基本消费者
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
///接收到消息事件
consumer.Received += (model, ea) =>
{
///打印消息
Console.WriteLine(" [x] Received {0}", Encoding.UTF8.GetString(ea.Body.ToArray()));
/// 注意:手动确认
channel.BasicAck(
deliveryTag: ea.DeliveryTag, //回调的消息的唯一标识
multiple: false //是否确认多条
);
};
///消费消息
channel.BasicConsume(
queueName, //队列名称
false, //注意:【自动确认】改为【手动确认】
consumer
);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}
}
}
*
*