RabbitMQ消息队列(五)-安装amqp扩展并订阅/发布Demo(.Net Core版)
publish发布消息
新建一个Asp.Net Core控制台项目:PublishDemo
安装Nuget包
Install-Package RabbitMQ.Client
添加命名空间引用
using RabbitMQ.Client; using RabbitMQ.Client.Events;
先定义好用来发消息的交换机、队列、RoutingKey、消息等变量。
String exchangeName = "wytExchange"; String routeKey = "wytRouteKey"; String message = "Hello World!";
完整代码
using System; using System.Text; using RabbitMQ.Client; using RabbitMQ.Client.Events; namespace PublishDemo { class Program { static void Main(string[] args) { String exchangeName = "wytExchange"; String routeKey = "wytRouteKey"; String message = "Hello World!"; ConnectionFactory factory = new ConnectionFactory(); factory.HostName = "192.168.63.129"; factory.Port = 5672; factory.VirtualHost = "/wyt"; factory.UserName = "wyt"; factory.Password = "wyt"; using (IConnection connection=factory.CreateConnection()) { using (IModel channel=connection.CreateModel()) { //声明交换机(名称:log,类型:fanout(扇出)) channel.ExchangeDeclare(exchange: exchangeName, type: "direct",durable:false,autoDelete:false,arguments:null); Byte[] body = Encoding.UTF8.GetBytes(message); //消息推送 channel.BasicPublish(exchange: exchangeName, routingKey: routeKey, body: body); Console.WriteLine(" [x] Sent {0}", message); } } Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } }
查看交换机多了一个wytExchange交换机。
Subscribe发布消息
新建一个Asp.Net Core控制台项目:SubscribeDemo
安装Nuget包
Install-Package RabbitMQ.Client
添加命名空间引用
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
先定义好用来发消息的交换机、队列、RoutingKey、消息等变量。
String queueName = "wytQueue"; String exchangeName = "wytExchange"; String routeKeyName = "wytRouteKey";
完整代码
using System; using System.Text; using RabbitMQ.Client; using RabbitMQ.Client.Events; namespace SubscribeDemo { class Program { static void Main(string[] args) { String queueName = "wytQueue"; String exchangeName = "wytExchange"; String routeKeyName = "wytRouteKey"; ConnectionFactory factory = new ConnectionFactory(); factory.HostName = "localhost"; factory.Port = 5672; factory.VirtualHost = "/wyt"; factory.UserName = "wyt"; factory.Password = "wyt"; using (IConnection connection=factory.CreateConnection()) { using (IModel channel=connection.CreateModel()) { //声明交换机 channel.ExchangeDeclare(exchange: exchangeName, type: "direct",durable:true,autoDelete:false,arguments:null); //声明队列 channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false, arguments: null); //将队列和交换机绑定 channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: routeKeyName, arguments: null); //定义接收消息的消费者逻辑 EventingBasicConsumer consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { Byte[] body = ea.Body; String message = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] {0}", message); }; //将消费者和队列绑定 channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } } } }
查看交换机已经有superrd队列。
消息收发
作者:一个大西瓜
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!