RabbitMQ入门(-)

  • 软件安装
    1. 安装 otp_win
    2. 安装 rabbitmq-server
  • 软件启动
  1. 进入C:\Program Files (x86)\RabbitMQ Server\rabbitmq_server-3.4.1\sbin输入命令:rabbitmq-plugins enable rabbitmq_management
  2. 停止服务:net stop RabbitMQ
  3. 启动服务:net start RabbitMQ
  4. 在浏览器中输入地址查看:http://127.0.0.1:15672/
  • 测试代码
    1. 发布端代码 
        static void Main(string[] args)
        {
            string sExchangeName = "ExchangeName1";
            string sRoutingKey = "RoutingKey1";

            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    for (int i = 0; i < 10; i++)
                    {
                        string message = $"Hello,World--{i}";
                        byte[] body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: sExchangeName, routingKey: sRoutingKey, basicProperties: null, body: body);
                        Console.WriteLine(" [x] Sent {0}", message);
                    }
                }
            }

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }

     2. 接收端代码

        static void Main(string[] args)
        {
            bool flag = true;
            string pattern = "";
            while (flag)
            {
                Console.WriteLine("请选择Cnsumer模式  1(发短信)/2(发邮件)/3(发微信)");
                pattern = Console.ReadLine();
                if (pattern == "1" || pattern == "2" || pattern == "3")
                    flag = false;
                else
                    Console.Write("请做出正确的选择");
            }

            string sExchangeName = "ExchangeName1";
            string sRoutingKey = string.Empty;
            string sQueueName = "QueueName1";
            switch (pattern)
            {
                case "1": sQueueName = "msg"; break;
                case "2": sQueueName = "email"; break;
                case "3": sQueueName = "wechat"; break;
                default:
                    break;
            }

            var factory = new ConnectionFactory() { HostName = "localhost" };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                // 声明交换器
                channel.ExchangeDeclare(sExchangeName, ExchangeType.Fanout);
                //声明队列
                channel.QueueDeclare(queue: sQueueName,
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);
                // 进行绑定
                channel.QueueBind(sQueueName, sExchangeName, sRoutingKey);

                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body.ToArray();
                    var message = Encoding.UTF8.GetString(body);
                    Console.WriteLine(" [x] Received {0}", message);
                };
                channel.BasicConsume(queue: sQueueName,
                                     autoAck: true,
                                     consumer: consumer);

                Console.WriteLine(" Press [enter] to exit.");
                Console.ReadLine();
            }
        }

说明:接收端让用户选择3种Cnsumer模式,用来同时测试三个不同的接收端,所以测试时可以同时打开三个客户端选择不同的Cnsumer查看效果.

上一篇:软件设计模式三大类


下一篇:DAY 206 Python验证常见的50个正则表达式