当前位置:网站首页 / C#开发 / 正文

ay的RabbitMQ研究报告-第7章-C#代码实战[4]-Direct Routing

时间:2016年06月01日 | 作者 : aaronyang | 分类 : C#开发 | 浏览: 865次 | 评论 0

(国内第一个rabbitMQ中文 系列讲解------引进外国技术,我是AY,安徽合肥的杨洋,1991年。我学习技术,我开心)

为了给自己站点增加流量,望各位抓包的,手下留情,自己去外国下载书籍,回来翻译着看,也不容易。谢谢你了。


====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。


前面的例子我们使用的是fanout类型的exchange,也就说我们提供routekey,这个routekey会被忽略

这一篇,主要教大家使用routekey的,我们使用direct exchange,指定routekey的方式跟队列通讯。

blob.png



新建解决方案文件夹4.Direct Exchange,添加publish和customer消费者

Publish代码:

using Common;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publish
{
    class Program
    {
        private static ConnectionFactory _factory;
        private static IConnection _connection;
        private static IModel _model;
        private const string ExchangeName = "DirectRouting_Exchange";

        private static void CreateConnection()
        {
            _factory = new ConnectionFactory
            {
                HostName = "localhost",
                UserName = "guest",
                Password = "guest"
            };
            _connection = _factory.CreateConnection();
            _model = _connection.CreateModel();
            _model.ExchangeDeclare(ExchangeName, "direct");
        }
        private static void SendPayment(Payment payment)
        {
            SendMessage(payment.Serialize(), "CardPayment");
            Console.WriteLine(" Payment Sent {0}, £{1}", payment.CardNumber,
            payment.AmountToPay);
        }
        private static void SendPurchaseOrder(PurchaseOrder purchaseOrder)
        {
            SendMessage(purchaseOrder.Serialize(), "PurchaseOrder");
            Console.WriteLine(" Purchase Order Sent {0}, £{1}, {2}, {3}",
            purchaseOrder.CompanyName, purchaseOrder.AmountToPay, purchaseOrder.PaymentDayTerms,
            purchaseOrder.PoNumber);
        }
        private static void SendMessage(byte[] message, string routingKey)
        {
            _model.BasicPublish(ExchangeName, routingKey, null, message);
        }





        static void Main(string[] args)
        {
            var payment1 = new Payment
            {
                AmountToPay = 25.0m,
                CardNumber = "1234123412341234"
            };
            var payment2 = new Payment
            {
                AmountToPay = 5.0m,
                CardNumber = "1234123412341234"
            };
            var payment3 = new Payment
            {
                AmountToPay = 2.0m,
                CardNumber = "1234123412341234"
            };
            var payment4 = new Payment
            {
                AmountToPay = 17.0m,
                CardNumber = "1234123412341234"
            };
            var payment5 = new Payment
            {
                AmountToPay = 300.0m,
                CardNumber =
                    "1234123412341234"
            };
            var payment6 = new Payment
            {
                AmountToPay = 350.0m,
                CardNumber =
                    "1234123412341234"
            };
            var payment7 = new Payment
            {
                AmountToPay = 295.0m,
                CardNumber =
                    "1234123412341234"
            };
            var payment8 = new Payment
            {
                AmountToPay = 5625.0m,
                CardNumber =
                    "1234123412341234"
            };
            var payment9 = new Payment
            {
                AmountToPay = 5.0m,
                CardNumber = "1234123412341234"
            };
            var payment10 = new Payment
            {
                AmountToPay = 12.0m,
                CardNumber =
                    "1234123412341234"
            };
            var purchaseOrder1 = new PurchaseOrder
            {
                AmountToPay = 50.0m,
                CompanyName =
                    "Company A",
                PaymentDayTerms = 75,
                PoNumber = "123434A"
            };
            var purchaseOrder2 = new PurchaseOrder
            {
                AmountToPay = 150.0m,
                CompanyName =
                    "Company B",
                PaymentDayTerms = 75,
                PoNumber = "193434B"
            };
            var purchaseOrder3 = new PurchaseOrder
            {
                AmountToPay = 12.0m,
                CompanyName =
                    "Company C",
                PaymentDayTerms = 75,
                PoNumber = "196544A"
            };
            var purchaseOrder4 = new PurchaseOrder
            {
                AmountToPay = 2150.0m,
                CompanyName =
                    "Company D",
                PaymentDayTerms = 75,
                PoNumber = "234434H"
            };
            var purchaseOrder5 = new PurchaseOrder
            {
                AmountToPay = 2150.0m,
                CompanyName =
                    "Company E",
                PaymentDayTerms = 75,
                PoNumber = "876434W"
            };
            var purchaseOrder6 = new PurchaseOrder
            {
                AmountToPay = 7150.0m,
                CompanyName =
                    "Company F",
                PaymentDayTerms = 75,
                PoNumber = "1423474U"
            };
            var purchaseOrder7 = new PurchaseOrder
            {
                AmountToPay = 3150.0m,
                CompanyName =
                    "Company G",
                PaymentDayTerms = 75,
                PoNumber = "1932344O"
            };
            var purchaseOrder8 = new PurchaseOrder
            {
                AmountToPay = 3190.0m,
                CompanyName =
                    "Company H",
                PaymentDayTerms = 75,
                PoNumber = "1123457Q"
            };
            var purchaseOrder9 = new PurchaseOrder
            {
                AmountToPay = 50.0m,
                CompanyName =
                    "Company I",
                PaymentDayTerms = 75,
                PoNumber = "1595344R"
            };
            var purchaseOrder10 = new PurchaseOrder
            {
                AmountToPay = 2150.0m,
                CompanyName =
                    "Company J",
                PaymentDayTerms = 75,
                PoNumber = "656734L"
            };
            CreateConnection();
            SendPayment(payment1);
            SendPayment(payment2);
            SendPayment(payment3);
            SendPayment(payment4);
            SendPayment(payment5);
            SendPayment(payment6);
            SendPayment(payment7);
            SendPayment(payment8);
            SendPayment(payment9);
            SendPayment(payment10);
            SendPurchaseOrder(purchaseOrder1);
            SendPurchaseOrder(purchaseOrder2);
            SendPurchaseOrder(purchaseOrder3);
            SendPurchaseOrder(purchaseOrder4);
            SendPurchaseOrder(purchaseOrder5);
            SendPurchaseOrder(purchaseOrder6);
            SendPurchaseOrder(purchaseOrder7);
            SendPurchaseOrder(purchaseOrder8);
            SendPurchaseOrder(purchaseOrder9);
            SendPurchaseOrder(purchaseOrder10);


        }
    }
}

代码讲解:

增加一个路由器exchange,然后sendmessage指定routekey即可,其他没什么讲的,前面几节课ay都讲过了。


看消费者是怎么写的。

using Common;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Customer
{
    class Program
    {
        private static ConnectionFactory _factory;
        private static IConnection _connection;
        private const string ExchangeName = "DirectRouting_Exchange";
        static void Main(string[] args)
        {
            _factory = new ConnectionFactory { HostName = "localhost", UserName = "guest", Password = "guest" };
            using (_connection = _factory.CreateConnection())
            {
                using (var channel = _connection.CreateModel())
                {
                    channel.ExchangeDeclare(ExchangeName, "direct");
                    var queueName = channel.QueueDeclare().QueueName;
                    channel.QueueBind(queueName, ExchangeName, "PurchaseOrder");
                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(queueName, true, consumer);
                    while (true)
                    {
                        var ea = consumer.Queue.Dequeue();
                        var message = (PurchaseOrder)ea.Body.DeSerialize();
                        var routingKey = ea.RoutingKey;
                        Console.WriteLine("-- Purchase Order - Key <{0}> : {1}, £{2},{3}, {4}", routingKey, message.CompanyName, message.AmountToPay, message.PaymentDayTerms, message.PoNumber);
                    }
                }
            }
        }
    }
}

客户端,这次创建direct的exchange,然后在queuebind的时候添加了routekey,接下来的过程都是一样的了。



效果:

先让消费者连接rabbitmq的broker

blob.png

此时,指定exchange还没有指定的routekey的消息生产出来,所以是空白的。启动生产者

blob.png

虽然那个名字的exchange有2种routekey的消息进来,由于客户端只指定了Purchase Order的routekey的消息,所以只消费了Purchase Order的消息。



到此Direct Exchange的routekey方式的使用已经说完了

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

















推荐您阅读更多有关于“RabbitMQ,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音号:wpfui,可以看到我的很多作品效果

AYUI8社区版Github地址:前往获取

作者:杨洋(AaronYang简称AY,安徽六安人)目前是个人,还没公司AY唯一QQ:875556003和AY交流

高中学历,2015年1月17日开始,兴趣学习研究WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费.AY主要靠卖技术服务挣钱

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

兼容XP到win10,vs2015/2019,最新AYUI:7.6.5.2

vs2015 企业版密钥HM6NR-QXX7C-DFW2Y-8B82K-WTYJV

vs2017 企业版密钥NJVYC-BMHX2-G77MM-4XJMR-6Q8QF

标签列表