本文章仅为个人理解,如有错误请指正。
RabbitMQ安装、说明、常用命令:https://blog.csdn.net/kang_xuan/article/details/90718878
RabbitMQ API:
Nuget UI:查找 RabbitMQ.Client
Nuget CLI: Install-Package RabbitMQ.Client -Version 5.1.0 (查找对应版本)
RabbitMQ.Client、RabbitMQ.ServiceMode两个程序集都需要进行引用。
RabbitMQ五种队列介绍:
- RabbitMQ相关设置
- 安装RabbitMQ
- 创建虚拟机:rabbitmqctl add_vhost /demo
- 创建用户:rabbitmqctl add_user kk2019jy kk2019jy
- 设置用户权限:rabbitmqctl set_permissions -p /demo kk2019jy .* .* .*
- 注意事项:
- 建议所有队列模式先创建队列后在进行生产者发送数据操作。
- 建议消费者不使用using调用Dispose,手动说着注册在其他事件中进行调用对象Dispose操作,Demo中使用WinForm方式,所以FormClosing进行了资源的释放。
- 自动消息确认和手动消息确认,同时只能使用一种方式,如果使用手动确认消息,建议在消费者事件的最后一行代码进行确认操作。
- 简单队列
只有一个生产者,一个消费者,生产者发送消息到交换机,消费者接收队列消息并进行确认。只需要创建队列即可,不需要对交换机和队列进行绑定设置,默认使用AMQP default交换机,会将队列的名称作为路由键与交换机进行绑定。
//创建队列
private void button1_Click(object sender, EventArgs e)
{
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "192.168.116.2";
connectionFactory.VirtualHost = "/demo";
connectionFactory.UserName = "kk2019jy";
connectionFactory.Password = "kk2019jy";
//创建连接
using (IConnection connectionObj = connectionFactory.CreateConnection())
{
//创建管道
using (IModel modelChannel = connectionObj.CreateModel())
{
//声明队列,设置队列持久化
modelChannel.QueueDeclare("hello", true, false, false, null);
}
}
}
//生产者
private void button2_Click(object sender, EventArgs e)
{
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "192.168.116.2";
connectionFactory.VirtualHost = "/demo";
connectionFactory.UserName = "kk2019jy";
connectionFactory.Password = "kk2019jy";
using (IConnection connectionObj = connectionFactory.CreateConnection())
{
//创建管道
using (IModel modelChannel = connectionObj.CreateModel())
{
byte[] buffer = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new MessageInfo { StrValue = "123", IntValue = Num++ }));
//发送消息,设置路由键为hello
modelChannel.BasicPublish("", "hello", null, buffer);
}
}
}
//消费者
private void button3_Click(object sender, EventArgs e)
{
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "192.168.116.2";
connectionFactory.VirtualHost = "/demo";
connectionFactory.UserName = "kk2019jy";
connectionFactory.Password = "kk2019jy";
ConnectionObj = connectionFactory.CreateConnection();
//创建管道
ModelChannel = ConnectionObj.CreateModel();
//创建基本消费者事件对象
EventingBasicConsumer consumerEvent = new EventingBasicConsumer(ModelChannel);
//注册事件
consumerEvent.Received += ConsumerEvent_Received;
//消费者接收消息,关闭自动消息确认
ModelChannel.BasicConsume("hello", false, consumerEvent);
}
private void ConsumerEvent_Received(object sender, BasicDeliverEventArgs e)
{
string byteStr = Encoding.UTF8.GetString(e.Body);
JsonConvert.DeserializeObject(byteStr);
Console.WriteLine(byteStr);
//手动进行消息确认
(sender as EventingBasicConsumer).Model.BasicAck(e.DeliveryTag, false);
}
- Work队列模式
也称工作队列模式,竞争消费者模式,一个生产者,多个消费者,一个消息只能被一个消费者接收处理,Work模式有两种设置:循环调度和公平派遣,应用场景:效率高的消费者消费消息多。可以用来进行负载均衡
//循环调度-创建队列
private void button5_Click(object sender, EventArgs e)
{
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.HostName = "192.168.116.2";
connectionFactory.VirtualHost = "/demo";