微信公眾號:趣編程ACE
關注可了解更多的.NET日常實戰開發技巧,如需源碼 請公眾號后臺留言 源碼;
[如果覺得本公眾號對您有幫助,歡迎關注]
.Net中RabbitMQ中交換機模式的使用
前文回顧
【微服務專題之】.Net6下集成消息隊列上-RabbitMQ【微服務專題之】.Net6下集成消息隊列2-RabbitMQ
【微服務專題之】.Net6中集成消息隊列-RabbitMQ中直接路由模式
TopicExchange 交換機模式
如果我們需要將一條信息發送到多個隊列上,假若利用直連模式,那么就會有很多的路由,而TopicExchange只需要配置定義好的路由規則,即可省略多余的路由指定。
PS:路由規則有一定的約束,比如需要采取* .#. * 的格式,用.號 分隔
其中
1.*表示一個單詞
2. #表示任意數量(零個或多個)單詞。
消費者程序演示
public?static?class?TopicExchangeReceive{public?static?void?Receive(IModel?channel){channel.ExchangeDeclare("hello-topic-exchange",?ExchangeType.Topic);channel.QueueDeclare(queue:?"hello-topic-queue",durable:?true,exclusive:?false,autoDelete:?false,arguments:?null);channel.QueueBind("hello-topic-queue",?"hello-topic-exchange",?"route.*");//channel.QueueBind("hello",?"hello-direct-exchange",?"route2");//?創建一個消費者基本事件var?consumer?=?new?EventingBasicConsumer(channel);consumer.Received?+=?(model,?ea)?=>{var?body?=?ea.Body.ToArray();var?message?=?Encoding.UTF8.GetString(body);Console.WriteLine("?[x]?Received?{0}",?message);};channel.BasicConsume(queue:?"hello-topic-queue",//?自動確認autoAck:?true,consumer:?consumer);//channel.BasicConsume(queue:?"hello",//?????????????????????//?自動確認//?????????????????????autoAck:?true,//?????????????????????consumer:?consumer);Console.WriteLine("?Press?[enter]?to?exit.");Console.ReadLine();}}
生產者程序演示
public?static?class?TopicExchangeSend{public?static?void?Send(IModel?channel){channel.ExchangeDeclare("hello-topic-exchange",?ExchangeType.Topic);var?count?=?0;while?(true){Thread.Sleep(1000);//?發送的消息string?message?=?$"Hello?World?{count}";var?body?=?Encoding.UTF8.GetBytes(message);//var?body2?=?Encoding.UTF8.GetBytes(message?+?"body2");//?基本發布?不指定交換channel.BasicPublish(exchange:?"hello-topic-exchange",//?路由鍵???就是隊列名稱routingKey:?"route.2",//?基礎屬性basicProperties:?null,//?傳遞的消息體body:?body);//channel.BasicPublish(exchange:?"hello-direct-exchange",//?????????????????????//?路由鍵???就是隊列名稱//?????????????????????routingKey:?"route2",//?????????????????????//?基礎屬性//?????????????????????basicProperties:?null,//?????????????????????//?傳遞的消息體//?????????????????????body:?body2);count++;Console.WriteLine("?[x]?sent?{0}",?message);}}}
效果展示
Headers Exchange 模式
其實這種模式類似與Http請求里面的headers,我們定義一個字典然后通過交換機攜帶,作為交換機與路由器交換媒介,相比于Topic Exchange交換模式,header定義的格式類型更加豐富。
消費者程序演示
public?static?class?HeadersExchangeReceive{public?static?void?Receive(IModel?channel){channel.ExchangeDeclare("hello-headers-exchange",?ExchangeType.Headers);channel.QueueDeclare(queue:?"hello-headers-queue",durable:?true,exclusive:?false,autoDelete:?false,arguments:?null);var?headers?=?new?Dictionary<string,?object>(){{"test","A"?}};channel.QueueBind("hello-headers-queue",?"hello-headers-exchange",?String.Empty,?headers);//channel.QueueBind("hello",?"hello-direct-exchange",?"route2");//?創建一個消費者基本事件var?consumer?=?new?EventingBasicConsumer(channel);consumer.Received?+=?(model,?ea)?=>{var?body?=?ea.Body.ToArray();var?message?=?Encoding.UTF8.GetString(body);Console.WriteLine("?[x]?Received?{0}",?message);};channel.BasicConsume(queue:?"hello-headers-queue",//?自動確認autoAck:?true,consumer:?consumer);//channel.BasicConsume(queue:?"hello",//?????????????????????//?自動確認//?????????????????????autoAck:?true,//?????????????????????consumer:?consumer);Console.WriteLine("?Press?[enter]?to?exit.");Console.ReadLine();}}
生產者程序演示
public?static?void?Send(IModel?channel){channel.ExchangeDeclare("hello-headers-exchange",?ExchangeType.Headers);var?count?=?0;while?(true){Thread.Sleep(1000);//?發送的消息string?message?=?$"Hello?World?{count}";var?body?=?Encoding.UTF8.GetBytes(message);//var?body2?=?Encoding.UTF8.GetBytes(message?+?"body2");var?basicProperties?=?channel.CreateBasicProperties();basicProperties.Headers?=?new?Dictionary<string,?object>(){{"test"?,"A"}};//?基本發布?不指定交換channel.BasicPublish(exchange:?"hello-headers-exchange",//?路由鍵???就是隊列名稱routingKey:?String.Empty,//?基礎屬性basicProperties,//?傳遞的消息體body:?body);//channel.BasicPublish(exchange:?"hello-direct-exchange",//?????????????????????//?路由鍵???就是隊列名稱//?????????????????????routingKey:?"route2",//?????????????????????//?基礎屬性//?????????????????????basicProperties:?null,//?????????????????????//?傳遞的消息體//?????????????????????body:?body2);count++;Console.WriteLine("?[x]?sent?{0}",?message);}}
效果演示
FanoutExchange 模式
扇形交換機模式是最傻瓜的一種交換模式,總的而言只要將隊列綁定到交換機上,就能做到消息互通了。當然了這種機制處理事件的速度也是所有交換機類型里面最快的。
消費者程序演示
public?static?class?FanoutExchangeReceive{public?static?void?Receive(IModel?channel){channel.ExchangeDeclare("hello-fanout-exchange",?ExchangeType.Fanout);channel.QueueDeclare(queue:?"hello-fanout-queue",durable:?true,exclusive:?false,autoDelete:?false,arguments:?null);//var?headers?=?new?Dictionary<string,?object>()//{//????{"test","A"?}//};channel.QueueBind("hello-fanout-queue",?"hello-fanout-exchange",?String.Empty);//channel.QueueBind("hello",?"hello-direct-exchange",?"route2");//?創建一個消費者基本事件var?consumer?=?new?EventingBasicConsumer(channel);consumer.Received?+=?(model,?ea)?=>{var?body?=?ea.Body.ToArray();var?message?=?Encoding.UTF8.GetString(body);Console.WriteLine("?[x]?Received?{0}",?message);};channel.BasicConsume(queue:?"hello-fanout-queue",//?自動確認autoAck:?true,consumer:?consumer);//channel.BasicConsume(queue:?"hello",//?????????????????????//?自動確認//?????????????????????autoAck:?true,//?????????????????????consumer:?consumer);Console.WriteLine("?Press?[enter]?to?exit.");Console.ReadLine();}
生產者程序演示
public?static?class?FanoutExchangeSend{public?static?void?Send(IModel?channel){channel.ExchangeDeclare("hello-fanout-exchange",?ExchangeType.Fanout);var?count?=?0;while?(true){Thread.Sleep(1000);//?發送的消息string?message?=?$"Hello?World?{count}";var?body?=?Encoding.UTF8.GetBytes(message);//var?body2?=?Encoding.UTF8.GetBytes(message?+?"body2");//var?basicProperties?=?channel.CreateBasicProperties();//basicProperties.Headers?=?new?Dictionary<string,?object>()//{//????{"test"?,"A"}//};//?基本發布?不指定交換channel.BasicPublish(exchange:?"hello-fanout-exchange",//?路由鍵???就是隊列名稱routingKey:?String.Empty,//?基礎屬性null,//?傳遞的消息體body:?body);//channel.BasicPublish(exchange:?"hello-direct-exchange",//?????????????????????//?路由鍵???就是隊列名稱//?????????????????????routingKey:?"route2",//?????????????????????//?基礎屬性//?????????????????????basicProperties:?null,//?????????????????????//?傳遞的消息體//?????????????????????body:?body2);count++;Console.WriteLine("?[x]?sent?{0}",?message);}}}
效果演示
PS:具體的代碼效果演示看視頻哦~