【微服务专题之】.Net6中集成消息队列-RabbitMQ中直接路由模式

前文回顾

【微服务专题之】.Net6下集成消息队列上-RabbitMQ

【微服务专题之】.Net6下集成消息队列2-RabbitMQ


RabbitMQ中直接路由模式

视频讲解icon-default.png?t=M5H6https://mp.weixin.qq.com/s?__biz=Mzg5MTY2Njc3Mg==&mid=2247484258&idx=1&sn=aa9e4a6d71d141353a115c15aede8869&chksm=cfc8ab59f8bf224fdaf2f5b728cc03b25987c5460c2cd386c301ba126aba2fc485bd351e4419&token=2109866031&lang=zh_CN#rd

路由模式就是生产者与消费者之间基于交换机通信的时候,生产者指定路由发送数据,消费者绑定路由接收数据。这个与前文讲解的发布/订阅模式有一定的区分,发布订阅模式只要绑定了交换机的队列(queue)都会收到生产者通过交换机发送过来的数据,而路由模式增加了一个指定路由的设置,会声明发送到交换机下的哪个路由,而接受的消费者只有绑定了队列并且声明了该路由才会接受到数据。

代码演示

生产者代码:

 1public static void Send(IModel channel)
 2        {
 3            channel.ExchangeDeclare( "hello-direct-exchange",ExchangeType.Direct);
 4            var count = 0;
 5            while (true)
 6            {
 7                Thread.Sleep(1000);
 8                // 发送的消息
 9                string message = $"Hello World {count}";
10                var body = Encoding.UTF8.GetBytes(message);
11                var body2 = Encoding.UTF8.GetBytes(message+"body2");
12
13                // 基本发布 不指定交换
14                channel.BasicPublish(exchange: "hello-direct-exchange",
15                                     // 路由键   就是队列名称
16                                     routingKey: "route1",
17                                     // 基础属性
18                                     basicProperties: null,
19                                     // 传递的消息体
20                                     body: body);
21
22                channel.BasicPublish(exchange: "hello-direct-exchange",
23                                     // 路由键   就是队列名称
24                                     routingKey: "route2",
25                                     // 基础属性
26                                     basicProperties: null,
27                                     // 传递的消息体
28                                     body: body2);
29                count++;
30                Console.WriteLine(" [x] sent {0}", message);
31            }
32        }

消费者代码

 1public static void Receive(IModel channel)
 2        {
 3            channel.ExchangeDeclare("hello-direct-exchange", ExchangeType.Direct);
 4            channel.QueueDeclare(queue: "hello-direct-queue",
 5                        durable: true,
 6                        exclusive: false,
 7                        autoDelete: false,
 8                        arguments: null);
 9            channel.QueueBind("hello-direct-queue", "hello-direct-exchange", "route1");
10            channel.QueueBind("hello", "hello-direct-exchange", "route2");
11
12            // 创建一个消费者基本事件
13            var consumer = new EventingBasicConsumer(channel);
14            consumer.Received += (model, ea) =>
15            {
16                var body = ea.Body.ToArray();
17                var message = Encoding.UTF8.GetString(body);
18                Console.WriteLine(" [x] Received {0}", message);
19            };
20            channel.BasicConsume(queue: "hello-direct-queue",
21                                 // 自动确认
22                                 autoAck: true,
23                                 consumer: consumer);
24
25            channel.BasicConsume(queue: "hello",
26                                 // 自动确认
27                                 autoAck: true,
28                                 consumer: consumer);
29
30            Console.WriteLine(" Press [enter] to exit.");
31            Console.ReadLine();
32        }

效果展示


PS:代码详解见视频~如需源码,请加VX: qubiancheng666

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值