rabbitmq用法
首先nuget上下载rabbitmq
版本要5.1.2
然后要自己搞一台服务器,上面要部署mq的服务端(具体教程网上很多)
接下来就是代码了,目前例两种模式,以后再有用到再说,
需要注意一下,不能同时定义多个队列名一样的接收端,这样会造成:定义了多少个同样的队列名,就要发定义同样队列名接收端个数次消息才能让它们全收到,什么意思?
比如说你定义了两个队列名都叫q1的接收端,这时候我们往q1发送消息的时候会发现只有一个接收端会收到消息,再发一次的时候另一个接收端才会收到消息,这样的情况两个模式都会这样。
1.队列模式
发送端:
using RabbitMQ.Client; using System; using System.Text; class Program { //发送 static void Main(string[] args) { String exchangeName = "自己定一个频道名"; ConnectionFactory factory = new ConnectionFactory() { AutomaticRecoveryEnabled = true }; factory.HostName = "你部署的mq的ip"; factory.UserName = "账号"; factory.Password = "密码"; IConnection connection = factory.CreateConnection();//连接服务器,即正在创建终结点。 IModel channel = connection.CreateModel(); channel.QueueDeclare(exchangeName, true, false, false, null);//创建一个名称为kibaqueue的消息队列 IBasicProperties properties = channel.CreateBasicProperties(); properties.DeliveryMode = 1; channel.BasicPublish("", exchangeName, properties, Encoding.UTF8.GetBytes("啊啊啊")); //生产消息 channel.Dispose(); connection.Dispose(); } }
接收端:
using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Text; class Program { //接收 static void Main(string[] args) { ConnectionFactory factory = new ConnectionFactory() { AutomaticRecoveryEnabled = true }; factory.HostName = "你部署的mq的ip"; factory.UserName = "账号"; factory.Password = "密码"; IConnection connection = factory.CreateConnection(); // 创建通道 IModel channel = connection.CreateModel(); channel.QueueDeclare("你定义的频道名", true, false, false, null); EventingBasicConsumer consumer = new EventingBasicConsumer(channel);//消费者 consumer.Received += ReceivedEvent; channel.BasicConsume("你定义的频道名", true, consumer);//消费消息 Console.ReadKey(); } public static void ReceivedEvent(object model, BasicDeliverEventArgs ea) { try { var body = ea.Body; string message = Encoding.UTF8.GetString(body); Console.WriteLine("接受到了:" + message); } catch (Exception ex) { } } }
打开发送控制台和接收控制台测试一下:
2.广播模式
广播模式就是在定义了某个交换机下的所有队列名都会收到消息,比如说我注册了名字叫做"qqq"的交换机,下面队列名有"q1"、"q2",那么q1和q2都会收到消息
发送端
using RabbitMQ.Client; using System.Text; class Program { //发送端 static void Main(string[] args) { string exchangeName = "交换机名字"; string routeKey = ""; //创建连接工厂 ConnectionFactory factory = new ConnectionFactory { UserName = "用户名",//用户名 Password = "密码",//密码 HostName = "你部署的mq服务器" }; //创建连接 IConnection connection = factory.CreateConnection(); //创建通道 IModel channel = connection.CreateModel();string msg = "啊啊啊啊"; channel.BasicPublish(exchangeName, routeKey, null, Encoding.UTF8.GetBytes(msg)); channel.Close(); connection.Close(); } }
接收端
using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Text; class Program { //接收 static void Main(string[] args) { string queueName = "队列名"; string exchangeName = "交换机名"; string rotkingKey = string.Empty; //创建连接工厂 ConnectionFactory factory = new ConnectionFactory { UserName = "用户名",//用户名 Password = "密码",//密码 HostName = "你部署的mq服务器ip" }; //创建连接 IConnection connection = factory.CreateConnection(); //创建通道 IModel channel = connection.CreateModel();
channel.ExchangeDeclare(exchangeName, ExchangeType.Fanout, true, false, null); //队列声明 channel.QueueDeclare(queueName, false, false, false, null); //绑定交换机,绑定完成以后就可以收到消息了 channel.QueueBind(queueName, exchangeName, rotkingKey, null); //channel.QueueUnbind(queueName, exchangeName, rotkingKey, null);取消绑定队列名 //事件基本消费者 EventingBasicConsumer consumer = new EventingBasicConsumer(channel); //接收到消息事件 consumer.Received += (ch, ea) => { var message = Encoding.UTF8.GetString(ea.Body); Console.WriteLine($"Queue:{queueName}收到消息: {message}"); //确认该消息已被消费 channel.BasicAck(ea.DeliveryTag, true); }; //启动消费者 设置为手动应答消息 channel.BasicConsume(queueName, false, consumer); } }