• .NET中RabbitMQ的使用


    概述

      MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public License开源协议。AMQP(高级消息队列协议) 是一个异步消息传递所使用的应用层协议规范,作为线路层协议,而不是API(例如JMS),AMQP 客户端能够无视消息的来源任意发送和接受信息。AMQP的原始用途只是为金融界提供一个可以彼此协作的消息协议,而现在的目标则是为通用消息队列架构提供通用构建工具。因此,面向消息的中间件 (MOM)系统,例如发布/订阅队列,没有作为基本元素实现。AMQP当中有四个概念非常重要(一个虚拟主机持有一组交换机、队列和绑定):

    1. virtual host,虚拟主机
    2. exchange,交换机
    3. queue,队列
    4. binding,绑定

    Window下安装RabbbitMQ

    文件下载安装

    Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang。通过下面两个连接下载安装3.2.3 版本:

    1. 下载并安装 Erlang OTP For Windows (vR16B03)
    2. 运行安装 Rabbit MQ Server Windows Installer (v3.2.3)

    默认安装的Rabbit MQ 监听端口是5672。先安装Erlang OTP后安装RabbitMQ,安装方式默认即可,RabbitMQ可以勾选安装后台服务、服务启动和停止等操作。

    激活Rabbit MQ's Management Plugin

    使用Rabbit MQ 管理插件,可以更好的可视化方式查看Rabbit MQ 服务器实例的状态,打开CMD命令,cd到安装目录(.. abbitmq_server-3.2.3sbin)下,输入下面的命令激活:

    rabbitmq-plugins enable rabbitmq_management

    要重启服务才能生效,可以执行

    net stop RabbitMQ && net start RabbitMQ

    输入网址,打开监控页面:  http://localhost:15672 (默认账号和密码:guest 和guest)

    配置RabbitMQ用户权限

    RabbitMQ是存在用户权限的,默认是guest 密码也是guest,隶属于Administrator管理员下。现需要配置新用户和权限,继续打开CMD命令,cd到安装目录sbin下:

    用户操作指令:

    ::查询服务状态
    rabbitmqctl status
    
    
    ::列举虚拟主机列表
    rabbitmqctl list_vhosts
    ::列举用户列表
    rabbitmqctl list_users
    
    :: 添加用户和密码
    rabbitmqctl  add_user  hao  abc123
    
    :: 设置权限   
    rabbitmqctl  set_permissions  yy  ".*"  ".*"  ".*"
    
    :: 分配用户组
    rabbitmqctl  set_user_tags yy administrator
    
    :: 删除guest用户
    rabbitmqctl delete_user guest
    ::修改用户密码
    rabbitmqctl change_password {username}  {newpassowrd}
    

      

    像ADO.Net的五大对象也是,操作数据库先进行连接connection,然后使用command过滤出要选择的数据,使用DataReader或DataSet、DataAdapter,在RabbitMQ的使用当中也有基本固定的步骤。

    一、生产者

    1.创建连接connection:不管是生产者还是消费者都需要先于RabbitMQ服务器连接,才能进行数据交换

    2.创建通道 Channel:生产者、消费者的消息传递是在通道下传递的

    3.声明交换器、队列

    4.交换器与队列进行绑定

    5.通过交换器BasicPublish数据到队列

    二、消费者

    1.创建连接 :和生产者一样

    2.创建通道:和生产者一样

    3.声明交换器、队列

    4.交换器与队列进行绑定

    5.通过BasicGet方法获取队列中的数据

    上面一、二是大致的基本步骤,按照大致的步骤来基本不会出现大的问题,其实生产者和消费者的前4个步骤基本一样,主要是第5个步骤,一个是生产BasicPublish发布,一个是获取Get。

    三、出现的错误

    在ConnectionFactory创建连接对象时出现上面提到的bug:None of the specified endpoints were reachable.

    四、demo

    1.首先在生产者端和消费者端引入RabbitMQ

     2.生产者端

      

    using RabbitMQ.Client;
    using System;
    using System.Text;
    
    namespace RabbitMQProduct
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Welcome to RabbitMQ Product!");
                DirectExchangeSendMsg();
                // TopicExchangeSendMsg();
                Console.WriteLine("按任意值,退出程序");
                Console.ReadKey();
            }
    
            /// <summary>
            /// 连接配置
            /// </summary>
            private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
            {
                UserName = "howdyadmin",
                Password = "123456",
                Port = 5672,
                VirtualHost = "howdyVirtualHost"
            };
            /// <summary>
            /// 路由名称
            /// </summary>
            const string ExchangeName = "howdy.exchange";
    
            //队列名称
            const string QueueName = "howdy.queue";
    
            /// <summary>
            /// 路由名称
            /// </summary>
            const string TopExchangeName = "topic.howdy.exchange";
    
            //队列名称
            const string TopQueueName = "topic.howdy.queue";
    
    
            /// <summary>
            ///  单点精确路由模式
            /// </summary>
            public static void DirectExchangeSendMsg()
            {
                using (IConnection conn = rabbitMqFactory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        channel.ExchangeDeclare(ExchangeName, ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
                        channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
                        channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName);
    
                        var props = channel.CreateBasicProperties();
                        props.Persistent = true;
                        string vadata = Console.ReadLine();
                        while (vadata != "exit")
                        {
                            var msgBody = Encoding.UTF8.GetBytes(vadata);
                            channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: msgBody);
                            Console.WriteLine(string.Format("***发送时间:{0},发送完成,输入exit退出消息发送",
                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                            vadata = Console.ReadLine();
                        }
                    }
                }
            }
            /// <summary>
            /// topic 模糊匹配模式,符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“log.#”能够匹配到“log.info.oa”,但是“log.*” 只会匹配到“log.error”
            /// </summary>
            public static void TopicExchangeSendMsg()
            {
                using (IConnection conn = rabbitMqFactory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        channel.ExchangeDeclare(TopExchangeName,ExchangeType.Topic, durable: false, autoDelete: false, arguments: null);
                        channel.QueueDeclare(TopQueueName, durable: false, autoDelete: false, exclusive: false, arguments: null);
                        channel.QueueBind(TopQueueName, TopExchangeName, routingKey: TopQueueName);
                        //var props = channel.CreateBasicProperties();
                        //props.Persistent = true;
                        string vadata = Console.ReadLine();
                        while (vadata != "exit")
                        {
                            var msgBody = Encoding.UTF8.GetBytes(vadata);
                            channel.BasicPublish(exchange: TopExchangeName, routingKey: TopQueueName, basicProperties: null, body: msgBody);
                            Console.WriteLine(string.Format("***发送时间:{0},发送完成,输入exit退出消息发送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                            vadata = Console.ReadLine();
                        }
                    }
                }
            }
        }
    }

    上面的代码分别创建了两个路由和两个队列,一种是DirectExchange,一种是TopicExchange,验证时需要生产者和消费者使用同一种的ExChange。

    3.消费者端

    using RabbitMQ.Client;
    using RabbitMQ.Client.Events;
    using System;
    using System.Text;
    
    namespace RabbitMQConsumer
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Welcome to RabbitMQ Consumer!");
                //DirectAcceptExchange();
                 //DirectAcceptExchangeEvent();
                DirectAcceptExchangeTask();
                //TopicAcceptExchange();
                Console.WriteLine("按任意值,退出程序");
                Console.ReadKey();
            }
    
            /// <summary>
            /// 连接配置
            /// </summary>
            private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
            {
                HostName = "127.0.0.1",
                UserName = "howdyadmin",
                Password = "123456",
                Port = 5672,
                VirtualHost = "howdyVirtualHost"
            };
            /// <summary>
            /// 路由名称
            /// </summary>
            const string ExchangeName = "howdy.exchange";
    
            //队列名称
            const string QueueName = "howdy.queue";
    
            /// <summary>
            /// 路由名称
            /// </summary>
            const string TopExchangeName = "topic.howdy.exchange";
    
            //队列名称
            const string TopQueueName = "topic.howdy.queue";
    
    
            /// <summary>
            /// 基于时间轮询的,每隔一段时间获取一次
            /// </summary>
            public static void DirectAcceptExchange()
            {
                using (IConnection conn = rabbitMqFactory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        channel.ExchangeDeclare(ExchangeName, ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
                        channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
                        channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName);
                        while (true)
                        {
                            BasicGetResult msgResponse = channel.BasicGet(QueueName, true);
                            if (msgResponse != null)
                            {
                                var msgBody = Encoding.UTF8.GetString(msgResponse.Body);
                                Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
                            }
    
                            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
                        }
                    }
                }
            }
            /// <summary>
            /// 基于事件的,当消息到达时触发事件,获取数据
            /// </summary>
            public static void DirectAcceptExchangeEvent()
            {
                using (IConnection conn = rabbitMqFactory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        //channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
                        channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
                        //channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName);
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (model, ea) =>
                        {
                            var msgBody = Encoding.UTF8.GetString(ea.Body);
                            Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
                        };
                        channel.BasicConsume(QueueName,  true, consumer: consumer);
                        Console.WriteLine("按任意值,退出程序");
                        Console.ReadKey();
                    }
                }
            }
            /// <summary>
            /// 基于事件的,当消息到达时触发事件,获取数据
            /// </summary>
            public static void DirectAcceptExchangeTask()
            {
                using (IConnection conn = rabbitMqFactory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        //channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null);
                        channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null);
                        channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);//告诉broker同一时间只处理一个消息
                        //channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName);
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (model, ea) =>
                        {
                            var msgBody = Encoding.UTF8.GetString(ea.Body);
                            Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
                            int dots = msgBody.Split('.').Length - 1;
                            System.Threading.Thread.Sleep(dots * 1000);
                            //处理完成,告诉Broker可以服务端可以删除消息,分配新的消息过来
                            channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                        };
                        //noAck设置false,告诉broker,发送消息之后,消息暂时不要删除,等消费者处理完成再说
                        channel.BasicConsume(QueueName, false, consumer: consumer);
    
                        Console.WriteLine("按任意值,退出程序");
                        Console.ReadKey();
                    }
                }
            }
            /// <summary>
            /// topic 模糊匹配模式,符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。因此“log.#”能够匹配到“log.info.oa”,但是“log.*” 只会匹配到“log.error”
            /// </summary>
            public static void TopicAcceptExchange()
            {
                using (IConnection conn = rabbitMqFactory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        channel.ExchangeDeclare(TopExchangeName, ExchangeType.Topic, durable: false, autoDelete: false, arguments: null);
                        channel.QueueDeclare(TopQueueName, durable: false, autoDelete: false, exclusive: false, arguments: null);
                        channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
                        channel.QueueBind(TopQueueName, TopExchangeName, routingKey: TopQueueName);
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (model, ea) =>
                        {
                            var msgBody = Encoding.UTF8.GetString(ea.Body);
                            Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
                            int dots = msgBody.Split('.').Length - 1;
                            System.Threading.Thread.Sleep(dots * 1000);
                            Console.WriteLine(" [x] Done");
                            channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                        };
                        channel.BasicConsume(TopQueueName,  false, consumer: consumer);
    
                        Console.WriteLine("按任意值,退出程序");
                        Console.ReadKey();
                    }
                }
            }
        }
    }

     

    消费者端也是两个路由两个队列,在实现DirectExchange时使用了三种方式,DirectAcceptExchange是基于时间轮询的,每隔一段时间获取一次,DirectAcceptExchangeEvent、DirectAcceptExchangeTask是基于事件的,当消息到达时触发事件,获取数据。

    4.实验截图

     

    参考原文地址(感谢分享):

    https://www.cnblogs.com/5ishare/p/6784149.html

    http://www.cnblogs.com/xibei666/p/5931267.html#3680686

  • 相关阅读:
    短信发送器小案例 smsManager
    短信大全小案例
    JavaScript学习总结(4)——JavaScript数组
    JavaScript学习总结(3)——JavaScript函数(function)
    JavaScript学习总结(2)——JavaScript数据类型判断
    JavaScript学习总结(2)——JavaScript数据类型判断
    JavaScript学习总结(1)——JavaScript基础
    JavaScript学习总结(1)——JavaScript基础
    Spring学习总结(6)——Spring之核心容器bean
    Spring学习总结(6)——Spring之核心容器bean
  • 原文地址:https://www.cnblogs.com/personblog/p/10681741.html
Copyright © 2020-2023  润新知