RabbitMQ Topic版本

MQHelper类接上一篇:

======生产者代码:

static void Main(string[] args)
        {
            //创建连接对象
            using var connection = R_MQHelper.GetConnection();
            //创建信道
            using var channel = connection.CreateModel();
            //创建交换机
            channel.ExchangeDeclare("chn_topic_Exchange","topic",false,false,null);

            //创建队列
            channel.QueueDeclare("chn_topic_Queue1", false,false,false,null);
            channel.QueueDeclare("chn_topic_Queue2", false, false, false, null);
            channel.QueueDeclare("chn_topic_Queue3", false, false, false, null);

            //把队列绑定到交换机(即 队列订阅交换机)
            channel.QueueBind("chn_topic_Queue1", "chn_topic_Exchange","user.insert",null);//设置 topic
            channel.QueueBind("chn_topic_Queue2", "chn_topic_Exchange", "user.update", null);//设置 topic

            //加个通配符
            //在添加其他队列时,符合通配符条件  则这个队列消息也会添加进去
            channel.QueueBind("chn_topic_Queue3", "chn_topic_Exchange", "user.*", null);//设置 topic


            for (int i = 0; i < 15; i++)
            {
                //发送消息
                //根据已订阅交换机队列的routingKey指定发送消息
                if (i < 5)
                {
                    string msg = $"添加了数据:{i + 1}";
                    var body = Encoding.UTF8.GetBytes(msg);
                    channel.BasicPublish("chn_topic_Exchange", "user.insert", false, null, body);
                    Console.WriteLine("insert:" + msg);
                }
                else if (i < 10 && i >= 5)
                {
                    string msg = $"更新了数据:{i + 1}";
                    var body = Encoding.UTF8.GetBytes(msg);
                    channel.BasicPublish("chn_topic_Exchange", "user.update", false, null, body);
                    Console.WriteLine("update:" + msg);
                }
                else
                {
            //这里是根据 user.* 通配符来的
string msg = $"删除了数据:{i + 1}"; var body = Encoding.UTF8.GetBytes(msg); channel.BasicPublish("chn_topic_Exchange", "user.delete", false, null, body); Console.WriteLine("delete:" + msg); } } Console.ReadKey(); }

 

=========消费者代码:

 static void Main(string[] args)
        {
            //创建连接对象
            using var connection = R_MQHelper.GetConnection();
            //创建信道
            using var channel = connection.CreateModel();
            //创建交换机
            channel.ExchangeDeclare("chn_topic_Exchange", "topic", false, false, null);

            //创建队列
            channel.QueueDeclare("chn_topic_Queue1", false, false, false, null);
            channel.QueueDeclare("chn_topic_Queue2", false, false, false, null);
            channel.QueueDeclare("chn_topic_Queue3", false, false, false, null);

            //把队列绑定到交换机(即 队列订阅交换机)
            channel.QueueBind("chn_topic_Queue1", "chn_topic_Exchange", "user.insert", null);//设置 topic
            channel.QueueBind("chn_topic_Queue2", "chn_topic_Exchange", "user.update", null);//设置 topic

            //加个通配符
            //在添加其他队列时,符合通配符条件  则这个队列消息也会添加进去
            channel.QueueBind("chn_topic_Queue3", "chn_topic_Exchange", "user.*", null);//设置 topic

            //事件对象
            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                //获取消息
                var body = ea.Body;//获取队列中消息主体
                var msg = Encoding.UTF8.GetString(body.ToArray());
                var routingKey = ea.RoutingKey;//队列名
                Console.WriteLine($"recive message is :{msg}  ,routingKey is: {routingKey}");
            };

            //消费消息
            //可以一次消费多个队列消息(也可以消费单个队列)
            channel.BasicConsume("chn_topic_Queue1", true, consumer);
            channel.BasicConsume("chn_topic_Queue2", true, consumer);
            channel.BasicConsume("chn_topic_Queue3", true, consumer);

            Console.ReadKey();
        }

 

posted on 2022-08-19 16:32  泰坦尼克号上的活龙虾  阅读(26)  评论(0)    收藏  举报

导航