RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有与代理接口通讯的客户端

安装RabbitMQ必须要有Erlang的环境,没有装好环境的这里给出Erlang的官网可自行下载:https://www.erlang.org

一、配置RabbitMQ

  1.将RabbitMQ的安装路径添加到环境变量中,启动RabbitMQ

  2.启动WEB管理界面:rabbitmq-plugins enable rabbitmq_management

  3.登录管理界面,RabbitMQ的管理界面的默认访问路径为:http://localhost:15672

  4.可以使用guest用户登录,密码也是guest

    

二、C#实现的小案例,这里给出官方教程:https://www.rabbitmq.com/getstarted.html

  1.发布者

       var factory = new ConnectionFactory();
            factory.HostName = "localhost";
            factory.UserName = "guest";
            factory.Password = "guest";

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {

                    bool durable = true;
                    //声明一个名称为:hello的持久化的队列
                    channel.QueueDeclare("hello", durable, false, false, null);

                    string message = GetMessage(args);
                    var properties = channel.CreateBasicProperties();
                    properties.SetPersistent(true);
                    for (int i = 0; i < 100; i++)
                    {
                        var body = Encoding.UTF8.GetBytes(message+i);
                        channel.BasicPublish("", "hello", properties, body);
                        Console.WriteLine(" Send {0}", message+i);
                    }
                }
            }

            Console.ReadKey();

  2.消费者

    var factory = new ConnectionFactory();
            factory.HostName = "localhost";
            factory.UserName = "guest";
            factory.Password = "guest";

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    bool durable = true;
                    //在MQ上定义一个持久化队列,名称为:hello,如果名称相同不会重复创建
                    channel.QueueDeclare("hello", durable, false, false, null);
                    //输入1,那如果接收一个消息,如果没有应答,则客户端不会收到下一个消息

                    channel.BasicQos(0, 1, false);

                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume("hello", false, consumer);
                    while (true)
                    {
                        var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);

                        int dots = message.Split('.').Length - 1;
                        Thread.Sleep(dots * 1000);

                        Console.WriteLine("Received {0}", message);
                        Console.WriteLine("Done");
                        //回复确认
                        channel.BasicAck(ea.DeliveryTag, false);
                    }
                }
            }

  3、通过发布者的创建我们可以在管理界面中查看到添加的队列

  

01-15 22:46