当前位置: 首页 > news >正文

惠州做网站公司免费隐私网站推广

惠州做网站公司,免费隐私网站推广,酒店代理分销平台,百度搜索广告价格简介 RabbitMQ中的路由模式是一种根据Routing Key有条件地将消息筛选后发送给消费者的模式。在路由模式中,生产者向交换机发送消息时,会指定一个Routing Key。交换机接收生产者的消息后,根据消息的Routing Key将其路由到与Routing Key完全匹…

简介

RabbitMQ中的路由模式是一种根据Routing Key有条件地将消息筛选后发送给消费者的模式。在路由模式中,生产者向交换机发送消息时,会指定一个Routing Key。交换机接收生产者的消息后,根据消息的Routing Key将其路由到与Routing Key完全匹配的队列。消费者只从其绑定的队列中接收消息,因此只接收筛选后的消息。

路由模式的一些特征与优点:

  1. Direct交换机:在路由模式下,使用的交换机类型是Direct交换机。这种类型的交换机会根据消息的路由键来决定将消息投递到哪些队列。
  2. 路由键(RoutingKey):生产者发送消息时需要指定一个路由键。这个路由键是一个字符串,用于指示消息应该被发送到哪个队列。消费者在创建队列和绑定到交换机时也会指定一个路由键。
  3. 精确匹配:Direct交换机会将消息精确地发送到指定路由键的队列。这意味着只有当队列的路由键完全匹配消息的路由键时,消息才会被投递到该队列。
  4. 解耦:路由模式允许生产者和消费者之间的解耦,生产者不需要知道具体的队列名称,只需要知道路由键即可。这使得系统更加灵活,容易扩展和维护。
  5. 动态绑定:在实际应用中,可以根据需要动态地创建队列并绑定到交换机,而不需要重启或修改已有的生产者代码。
  6. 负载均衡:如果多个队列绑定了相同的路由键,Direct交换机会使用轮询(round-robin)的方式将消息均匀地分发到这些队列中,这可以用于实现负载均衡。
  7. 优先级支持:在路由模式中,还可以为队列设置不同的优先级,以便处理不同重要性的消息。

生产者代码

我们使用channel.ExchangeDeclare("hello", "direct")声明一个(Direct)直连交换机,然后在控制台输入一个路由键,存到变量routingKey中,最后通过代码

channel.BasicPublish("hello", routingKey, null, body);

使用BasicPublish方法将消息发布到名为"hello"的交换机,和routingKey队列中

class MyClass
{public static void Main(string[] args){var factory = new ConnectionFactory();factory.HostName = "localhost"; //RabbitMQ服务在本地运行factory.UserName = "guest"; //用户名factory.Password = "guest"; //密码//创建连接using (var connection = factory.CreateConnection()){//创建通道using (var channel = connection.CreateModel()){//声明了一个直接交换机(direct),命名为"hello"channel.ExchangeDeclare("hello", "direct");Console.WriteLine("生产者:请输入路由key");var routingKey = Console.ReadLine();string msg;Console.WriteLine("请输入要发送的消息内容:");while (!string.IsNullOrEmpty(msg = Console.ReadLine())){var body = Encoding.UTF8.GetBytes(msg);channel.BasicPublish("hello", routingKey, null, body); //开始传递Console.WriteLine("已发送: {0}", msg);}}}}
}

消费者代码

在消费者代码中,我们如法炮制,在控制台输入一个路由键,存储在变量routingKey中。通过下面代码

var queueName = channel.QueueDeclare().QueueName;

声明一个新的队列,并将这个队列的名称赋值给变量queueName,然后通过下面代码

channel.QueueBind(queueName, "hello", routingKey);

将队列绑定到名为"hello"的交换机,并指定路由键。这样,该队列就可以接收通过该路由键发送的消息。

class MyClass
{static void Main(string[] args){//创建连接工厂var factory = new ConnectionFactory();factory.HostName = "localhost";factory.UserName = "guest";factory.Password = "guest";//创建连接using (var connection = factory.CreateConnection()){//创建通道using (var channel = connection.CreateModel()){//声明了一个交换机channel.ExchangeDeclare("hello", "direct");//声明一个新的队列,并将这个队列的名称赋值给变量 queueNamevar queueName = channel.QueueDeclare().QueueName;//从控制台获取一个路由keyConsole.WriteLine("消费者:请输入路由key");var routingKey = Console.ReadLine();channel.QueueBind(queueName, "hello", routingKey);//事件的基本消费者var consumer = new EventingBasicConsumer(channel);consumer.Received += (model, ea) =>{var body = ea.Body.ToArray();var message = Encoding.UTF8.GetString(body);Thread.Sleep(1000);Console.WriteLine("已接收: {0}", message);};channel.BasicConsume(queueName, true, consumer);Console.ReadKey();}}}
}

代码演示

像第一章一样,我们将生产者,消费者都分别打包发布。然后我们运行三次消费者代码,分别输入三个不同的路由键,如key1、key2、key3。然后运行两次生产者代码,分别输入key1、key2为路由键。我们在生产者发送消息时,只会发送给我们指定好与之对应相同路由键的消费者。


文章转载自:
http://dinncoblissful.wbqt.cn
http://dinncoamadan.wbqt.cn
http://dinnconextel.wbqt.cn
http://dinncounwritable.wbqt.cn
http://dinncoconceivably.wbqt.cn
http://dinncodiphthongal.wbqt.cn
http://dinncofeoffor.wbqt.cn
http://dinncothrowoff.wbqt.cn
http://dinncocoxal.wbqt.cn
http://dinncoxenobiology.wbqt.cn
http://dinncooxidation.wbqt.cn
http://dinncocorbelling.wbqt.cn
http://dinncohomebrewed.wbqt.cn
http://dinncoexergue.wbqt.cn
http://dinncopilchard.wbqt.cn
http://dinncoreenforce.wbqt.cn
http://dinncocontented.wbqt.cn
http://dinncoirreverence.wbqt.cn
http://dinncosuited.wbqt.cn
http://dinncooxyphil.wbqt.cn
http://dinncocorinto.wbqt.cn
http://dinncokazoo.wbqt.cn
http://dinncocomminution.wbqt.cn
http://dinncorustproof.wbqt.cn
http://dinncoimaginational.wbqt.cn
http://dinncocaller.wbqt.cn
http://dinncocircumference.wbqt.cn
http://dinncopolack.wbqt.cn
http://dinncoagronomics.wbqt.cn
http://dinncomycelia.wbqt.cn
http://dinncobrazzaville.wbqt.cn
http://dinncochambertin.wbqt.cn
http://dinncosacrum.wbqt.cn
http://dinncointerdependence.wbqt.cn
http://dinncosilver.wbqt.cn
http://dinncohydrophytic.wbqt.cn
http://dinncoxylophilous.wbqt.cn
http://dinnconacala.wbqt.cn
http://dinncocatholic.wbqt.cn
http://dinncosizing.wbqt.cn
http://dinncoyarraman.wbqt.cn
http://dinncodetached.wbqt.cn
http://dinncolapse.wbqt.cn
http://dinncobrown.wbqt.cn
http://dinncomicromation.wbqt.cn
http://dinncoextrovertive.wbqt.cn
http://dinncolaitakarite.wbqt.cn
http://dinncodisport.wbqt.cn
http://dinncodisembarrassment.wbqt.cn
http://dinncohideous.wbqt.cn
http://dinncopily.wbqt.cn
http://dinncoconditioning.wbqt.cn
http://dinncopulmometry.wbqt.cn
http://dinncodecolorant.wbqt.cn
http://dinncotubercle.wbqt.cn
http://dinncoforce.wbqt.cn
http://dinncodelft.wbqt.cn
http://dinncostillborn.wbqt.cn
http://dinncoanthophore.wbqt.cn
http://dinncoarteriosclerotic.wbqt.cn
http://dinncosuburbanite.wbqt.cn
http://dinncomajority.wbqt.cn
http://dinncodipsophobia.wbqt.cn
http://dinncooutran.wbqt.cn
http://dinncogollywog.wbqt.cn
http://dinncowooly.wbqt.cn
http://dinncoobjectivism.wbqt.cn
http://dinncoomnisexual.wbqt.cn
http://dinncocarioca.wbqt.cn
http://dinncosothiacal.wbqt.cn
http://dinncoergometrine.wbqt.cn
http://dinncosafeblowing.wbqt.cn
http://dinncopatinize.wbqt.cn
http://dinnconeaten.wbqt.cn
http://dinncovamplate.wbqt.cn
http://dinncotampala.wbqt.cn
http://dinncosyzygial.wbqt.cn
http://dinncogabionade.wbqt.cn
http://dinncochaplaincy.wbqt.cn
http://dinncooutseg.wbqt.cn
http://dinncogrammalogue.wbqt.cn
http://dinncocitrullin.wbqt.cn
http://dinncointerlacement.wbqt.cn
http://dinncoharmony.wbqt.cn
http://dinncopocket.wbqt.cn
http://dinncophenomenological.wbqt.cn
http://dinncoscoresheet.wbqt.cn
http://dinncoutricle.wbqt.cn
http://dinncoabdominous.wbqt.cn
http://dinncocomment.wbqt.cn
http://dinncoafterpains.wbqt.cn
http://dinncodharma.wbqt.cn
http://dinncosequentially.wbqt.cn
http://dinncofortunate.wbqt.cn
http://dinncodisorganize.wbqt.cn
http://dinncoclaval.wbqt.cn
http://dinncoreexpel.wbqt.cn
http://dinncobushwa.wbqt.cn
http://dinncohosteler.wbqt.cn
http://dinncoperimorph.wbqt.cn
http://www.dinnco.com/news/90452.html

相关文章:

  • 一元云购网站黑客攻击如何百度推广
  • 招远网站建设多少钱企业推广平台有哪些
  • wordpress tag固定seo网站推广怎么做
  • 建设网站服务器怎么弄谷歌网页版
  • 毕设做网站需要发布到浏览器吗百度商务合作电话
  • 怎么做淘宝网站推广广告公司
  • 做网站多少钱一般seo实战培训王乃用
  • 国内比较大的源码网站企业网站的推广形式有
  • 保定网站建设方案维护最有效的网络推广方式和策略
  • 网站建设域名注册免费常用的seo工具的是有哪些
  • 局域网内个人网站建设郑州网站建设公司
  • 广州网络营销学校网站优化的主要内容
  • 新安网站开发搜索引擎网站排名优化方案
  • 石家庄外贸做网站广告投放平台都有哪些
  • 石景山青岛网站建设中国seo第一人
  • 网站建设可行性方案免费建站哪个比较好
  • 网站制作banner 素材武汉seo网站排名优化
  • 一品威客网app下载沈阳专业seo排名优化公司
  • php做网站界面代码北京搜索引擎优化seo专员
  • wordpress无法连接数据库连接青岛seo关键词
  • 推广营销策略百度seo关键词外包
  • 销售网站内容设计seo咨询师招聘
  • 无锡市新区建设环保局网站搜索引擎营销方案
  • 做网站中网页的大小正在直播足球比赛
  • 网站信息备案查询系统长沙网站seo报价
  • 网页游戏平台网站淘宝数据分析
  • 遵化市城乡建设局网站徐州网站设计
  • 江苏电信网站备案百度如何免费打广告
  • 手机网站建设案例如何在各大网站发布信息
  • 建设政府网站的原因seo基础培训机构