欢迎来到【一个大西瓜】的博客

不曾为梦想奋斗,拿什么去燃烧青春。有梦之人亦终将老去,但少年心气如昨。
太阳每一个时刻都同时是夕阳和朝阳,每天她沉入西边,意味着她同时从另一面土地升起。
扩大
缩小

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();
        }
    }
}
View Code

查看交换机多了一个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();

                }
            }
        }
    }
}
View Code

查看交换机已经有superrd队列。 

 

 消息收发

 

 

posted on 2018-05-23 14:10  一个大西瓜咚咚咚  阅读(1169)  评论(1编辑  收藏  举报

导航