rabbitmq 初识

下载安装 erlang

https://www.erlang.org/downloads

 

下载安装 rabbitmq

https://github.com/rabbitmq/rabbitmq-server

 

安装插件

在sbin目录下

cd D:\Program Files\RabbitMQ Server\rabbitmq_server-3.8.8\sbin

执行

rabbitmq-plugins.bat enable rabbitmq_management

现在可以访问

http://localhost:15672/

 

 用户名和密码都为guest,该账号貌似只能在本机登录

 

创建一个账号 设置管理员权限, 虚拟机hosts为 /

 

C# 例子

引入

 

 发送端代码

 class Program
    {
        static void Main(string[] args)
        {
            IConnectionFactory connectionFactory = new ConnectionFactory
            {
                HostName = "ip",
                Port = 5672,
                UserName="meng",
                Password= "meng"
            };
            connectionFactory.VirtualHost = "/";
            //
            using (IConnection con = connectionFactory.CreateConnection())
            {
                using (IModel channel = con.CreateModel())
                {
                    //queue 表示队列名称
                    //durable 是否持久化
                    //exclusive 是否独占连接,关闭后会自动删除
                    //autoDelete 自动删除
                    //arguments 
                    channel.QueueDeclare(queue:"que",durable:false,exclusive:false,autoDelete:false,arguments:null);
                    while (true)
                    {
                        Console.WriteLine("输入消息");
                        string str = Console.ReadLine();
                        byte[] msg = Encoding.UTF8.GetBytes(str);
                        //exchange mq的交换机
                        //routingKey 路由key, 交换机根据消息转发到指定队列。如果使用默认交换机,routingKey要使用队列名称
                        //basicProperties
                        //body 二进制数组,
                        channel.BasicPublish(exchange: "",routingKey:"que",basicProperties:null,body:msg);
                        Console.WriteLine("发送" + msg);
                    }
                }
            }

        }
    }

接收端代码

 

    class Program
    {
        static void Main(string[] args)
        {
            IConnectionFactory connectionFactory = new ConnectionFactory
            {
                HostName = "192.168.35.122",
                Port = 5672,
                UserName = "meng",
                Password = "meng"
            };

            using (IConnection con = connectionFactory.CreateConnection())
            {
                using (IModel channel = con.CreateModel())
                {
                    String queueName = String.Empty;
                    if (args.Length > 0)
                        queueName = args[0];
                    else
                        queueName = "que";

                    //声明一个队列
                    channel.QueueDeclare(queue: queueName,exclusive:false ,durable: false, autoDelete: false, arguments: null);
                    //创建消费者对象
                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                       string message=Encoding.UTF8.GetString(ea.Body.Span);
                        Console.WriteLine("接收到信息为:" + message);
                    };
                    //消费者开启监听
                    channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
                    Console.ReadKey();
                }
            }
        }
    }

 

posted @ 2020-09-13 11:27  富坚老贼  阅读(166)  评论(0编辑  收藏  举报