Fanout Multicast
In the previous use of direct direct-connect switches, the Routingkey was used to decide which queue to push messages to.
Fanout, in turn, pushes the received message into all the queues it binds to.
Analysis of the business, what scenarios need it? A user registered the site of the user, generally we need to send text messages and e-mail notification, is it necessary to in the same consumer to do both of these things? This is not a single responsibility, but the message sent is the same, but in a different way. Do you want to use both types of routingkey to send them once? This is obviously not what we want. So Fanout appeared.
Fanout type of exchange pushes messages into all of the queues, so you don't need to specify Routingkey, and it's useless to specify them!
The following code looks
There are two kinds of needs, texting and sending emails
//Create a new channel to return using(varChannel =rabbitmqhelper.getconnection (). Createmodel ()) {//Publish a message varmsg = Encoding.UTF8.GetBytes ($"Two dog son"); //You do not need to specify Routingkey, and it is useless to specify it. Because the switch is fanout typeChannel. Basicpublish ("Fanoutexchange", Routingkey:string. Empty, Basicproperties:NULL, body:msg); Console.Write ("Publish successfully! ");} Console.readkey ();
Here's the code for the consumer section.
BOOLFlag =true; stringPattern =""; while(flag) {Console.WriteLine ("Please select Ccnsumer Mode 1 (send SMS)/2 (email)"); Pattern=Console.ReadLine (); if(Pattern = ="1"|| Pattern = ="2") Flag=false; ElseConsole.Write ("please make the right choice"); } using(varChannel =rabbitmqhelper.getconnection (). Createmodel ()) {//declaring switch fanout modeChannel. Exchangedeclare ("Fanoutexchange", Exchangetype.fanout,true,false,NULL); //queues that are used according to claims varQueueName = Pattern = ="1"?"SMS":"Emai"; Channel. Queuedeclare (QueueName,true,false,false,NULL); //to bindChannel. Queuebind (QueueName,"Fanoutexchange",string. Empty,NULL); //Create Consumbers varConsumer =NewEventingbasicconsumer (channel); Consumer. Received+ = (sender, e) = = { varmsg =Encoding.UTF8.GetString (e.body); varAction = (Pattern = ="1"?"Send SMS":"send e-mail"); Console.WriteLine ($"to {msg}{action}"); }; //for consumptionChannel. Basicconsume (QueueName,true, consumer); Console.readkey (); }
Below the program to run, different consumer for different consumption
RabbitMQ types of fanout in exchange