RabbitMQ消息队列(五)-安装amqp扩展并订阅/发布Demo(.Net Core版)

时间:2022-12-21 21:00:40

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 = ;
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交换机。

RabbitMQ消息队列(五)-安装amqp扩展并订阅/发布Demo(.Net Core版)

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 = ;
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队列。

RabbitMQ消息队列(五)-安装amqp扩展并订阅/发布Demo(.Net Core版)

消息收发

RabbitMQ消息队列(五)-安装amqp扩展并订阅/发布Demo(.Net Core版)