.NET 6 - Message Broker with RabbitMQ 🐰🐰🐰

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign thank you for watching this video I am Muhammad and today we're gonna be discussing something really interesting which is rabbit mq rabbit mq is basically a message broker and it's going to allow us to send messages between a producer and a consumer we're going to be discussing all of these terminologies and how we can Implement them in the video so don't worry about it if you like this video please like share and subscribe it will really help the channel as well if you'd like to support me and get access to the source code please consider supporting me on patreon or you can buy me a coffee now with that being said grab your cup of coffee and let's get started so first things first let's see what's on the agenda for today first we're going to be discussing what is wrapped mq some of the terminology that we're going to be following some of the principles that we can actually Implement within rapid mq and then once all of that's done we're going to see the ingredients that we're going to be needing in order for us to actually build our application and then we're gonna jump into coding so first of all what is rabbit mq rabbit mq in essence it's a message broker and what does this mean it basically it means that it accepts messages and it sends them as simple as that we can think about it as a post office it basically it means that we hand them a letter and the post office is going to be responsible for it to actually deliver to the right address rabbitmq follow the same concept all we need to do is hand them a letter based on the configuration that we give it it will actually be responsible to delivering that Capital to whatever it needs to go it's very simple it's called the message broker and basically it's in summary it has it is responsible for sending a message from point A to point B based on some configuration that we give it as simple as that and you can think of point a and point B as basically two systems system a system B system one system two so on so forth and we can see how we can actually do Communications between all of these Services utilizing the rabbitmq framework so now that we can actually understand what is drop time queue let us understand some of the terminology so in essence whatever whoever is sending a message is basically a publisher so for example from my side if I am sending a message to someone I am a publisher if I'm an application sending it a message to a different application that that application is a publisher so once a message wants to be sent by a single application that's application is called the publisher and once it's being sent it's basically arrived to a queue and basically a queue there means it's basically we can think about it as like at a sorting office where basically all of the messages arrive to and basically live there until until they are delivered so once a message left a publisher it arrive to IQ and we can see here that as a publisher I'm sending a message and basically Whenever I send a message is go to this queue and basically here we can see that we have numbers on them so it starts with five four three to one what does this mean this means but basically the message are coming in as 5 4. so first in first out so the first message is being sum the first message is being delivered so here we can see that it's actually utilizing an order of somehow in order for it to deliver the message so we cannot skip the order here it needs to actually follow that order of the messages so let's say I send a message another service and the message another service would send a message because I sent a message at the first place and my message will have to arrive first there is no way out of this it's called a fifo first in first out and that's how the message are being sent and how this is how the message are being propagated to the different systems and basically whenever the publisher sent a message depending on the order they have sent the message it's basically depending on that order it will be delivered so the first one as we said a publisher who's responsible for sending a message then the messages will live in a queue and that queue basically it will be responsible for all of the messages will live until they are delivered and they're basically in a certain order depending on the order of the delivery of those messages or basically the sun date and lastly we have the consumer the consumer are basically whoever is actually waiting for this message so for example let's say I'm trying to send a message from the first system to assist to system 2 system age system B and basically once the mass suggestive system B is going to be responsible for sending emails for example so system a which is going to be the user registration for example it's going to be the publisher it's gonna send the message through rabbitmq it's going to be rabbit mq is going to be responsible to deliver this message to system B once system B is together for example the email service what system we actually received that message is going to be basically responsible to do any processing with it in that example is to sending an email and basically system B is called the consumer because it's actually consuming that message and here we can see the full based the base terminology for a rabbitmq application or basically implementation we have the publisher who's going to be responsible for sending that message we have the keyword all of the messages line up based on the order they were added which is called a fifo first and first out and basically we have the consumer at the end which is going to be basically responsible for actually doing something with that message that we have sent and basically here we can see that this diagram give it a very full overview about how we can actually Implement Implement rabbit mq and we can actually how we can Implement a publisher consumer as well without messages so now that we have covered them in terminology let's check some of the implementation that we can actually do within drop time queue and basically the first uh the first one which is the easiest one is the uh this one that we see currently here which is gonna be something I must from point A to point B as simple as that and this is basically called the hello world we're basically sending a message from one point to another as simple as that we don't have multiple systems we don't have multiple sorry multiple receivers all we have is one sander and one receiver and we're sending this information from one point to another so the second one publish unsubscribers so that's a bit different so the first one is basically what we did is we are sending the information from one point to another although we have different consumers publish and subscribers mean that what in essence what we're doing is I'm a consumer I'm only interested in receiving for example messages regarding email I am a different consumer and that consumer for example is responsive is only interested to hearing about orders so the publisher as myself I'm basically sending information and sending messages to however uh to the cues I'm sending messages and basically uh those services are only interested in listening to certain types of messages so those messages those consumers or those services are only going to be listening in to those types so the service was responsible for emails is only going to be waiting to listen about messages for emails the service was going to be responsible for orders is only going to be waiting to receive messages regarding orders so although I'm a publisher I'm sending two messages those services or those consumers will not accept all the messages they're only gonna accept the messages that's mattered to them and the way they actually tell the rabbitmq or the service that they are actually interested is by subscribing to them and basically what they're going to be doing is they're going to be subscribing to that topic so once the server is booting up it says okay listen to me rabbitmq all I want from you is just the messages regarding emails I'm Dr Robert I'm you say okay you're gonna be handling the email messages another service will be running and once it's actually what's a startup it does okay all I'm interested in in messages regarding orders and that's it okay will know service one is only going to be responsible for orders service tools only gonna be responsible for emails and basically because these Services have subscribed rabbit tamiki will be able to actually direct those messages to this service we don't have to do anything else rabbitmq will be responsible to do those so here we can see that uh the pub sub message we're only basically sending uh based on the subscription of the consumers so here we can see how we have a different type which is called routing and basically routings mean here we are only sending messages to the queues selectively and this is very important so we can see here that we have type and based on this graph we can see that we have different types of messages we're sending we're sending for example error info and warning and this is for for the logs so we can say that I have only one consumer who's only responsible or has only responsibility is handling logs and that's it so here we are being very selective of what we are sending or not subscribing we're basically checking the type of a message and based on that type we are actually processing that message to a certain queue or basically to a certain uh at a certain consumer and basically based on that type we are actually processing so we can see that we have for example here one consumer who's accepting everything it doesn't really matter on the other hand we have another consumer which is consumer one it's only taking the error types and that's how we can for example see those different uh messages being sent based on the routing so IPC is very interested as well so what does RPC mean here well basically the request reply pattern so what does this mean so let's say I'm a producer and I produce a certain message when I send the spread when I send this uh a message to the queue uh it's gonna received and it's going to be received by the consumer and then the consumer and that's hand is going to just send me like okay I have received this message in case there's any extra action that I need to do it's Gotta send it back to me so we can see here that we have the request and the reply so in all of the other ones once the message has been sent from the PT user we don't really care about the outcome on the other hand in the RPC which is the request reply pattern I do actually care about the reply and I actually care about what is the response that I'm actually expecting in order for me to continue so that's why here we can see that the request reply actually it's about it's a two-way system so I send and I'm expecting to receive on the other hand all of the other ones I just sent I don't really expect anything in return foreign for today so as we can see here uh we need uh some kind of an editor and it could be Visual Studio code it could be a rider it could be anything that we want uh and we are comfortable with uh we also need visuals uh not SDK you can download it you can go to Google contact.net SDK and you can download the latest version this should work with any uh a recent.net version and we need Docker in order for us to run a rabbitmq locally and basically this is all we're going to be needing we need three things which is going to be the code editor we're going to be needing that.net SDK and we're going to be needing the docker to be running locally as simple as that so now that we have finished all of this now it's time for us to jump into the code and actually start experimenting with the hello world concept just basically sending up sending a message from a producer to a consumer through whatever time queue so let's get into it so here what I have is basically I have my terminal and what I'm gonna be doing is I'm gonna be creating an application and basically this application is going to be a type of API so I'm going to be creating an API here and basically my API is going to be the producer it's going to be responsible for sending the message through rabbit mq and once that message is sent I'm gonna have a console application on the other hand which is going to be the consumer which is going to be responsible for actually receiving that message from rabbitmq and actually processing with it and for us to make it a bit interesting we're gonna be mimicking an airline portal Formula One Airlines and basically we're gonna be seeing what happens once we actually try to book a ticket and basically we're gonna send in this message and basically once we book a ticket we're going to be sending it to a service or a consumer which is called the processing booking processing and it's going to be responsible for generating a ticket for us and this is going to be a simple implementation when sending a message from a producer to to a consumer with.net so the first thing that I need to do here is I want to create my application and for me to do that as we said we're going to be creating a producer and a consumer and here we can see that we have two applications so we have uh the producer and the consumer so it's good for us to create a solution we'll put these two together so let's start with that so we're gonna put nut new assalam which is stand for solution and here what we're going to be doing is we're going to give it a name I'm just gonna call it formula Airline and now I have a slant so if I top here you can see that I have an asylum file so now I'm gonna be creating my web API I'm gonna put dotnet new web API I'm gonna give it a name and this name will be let's say formula Airline dot API so now that I have my um API up and running or basically created let me create also let me add my API to my as a solution and basically what I need to do is I need to link my API with a solution and this is very simple all I need to let me check if this is fine yeah my pixel is not holding anything so all I can let me move this a bit up all I can do here is put.net asalan and it was called formula Airline and I'm gonna say add formula Airline dot API and that's it and we can see right now that it has been linked and if I do I do not build from here the both of them should be building sorry the my API should be building successfully perfect so this is my producer what I'm gonna be doing right now I'm gonna be also creating My Consumer so it's gonna be Doc nut new console and I'm gonna give it a name I'm gonna call it Airline formula Airline and let's say booking processing let's say ticket to processing it's better so it's generates a ticket for us we call it ticket processing and now we can see we have my ad console so what I'm going to be doing right now is I'm gonna be linking also the same thing my solution with the console application so all I can do is just really utilize this and formula and line Dot ticketing solution and now it's think now if I do not build I should see both of them being built and as I as you can see here I have my formula Airline API that is being built and let me make this a bit bigger so we can see it on a single line and I also have my formula airline ticket processing dll so that way I'm building two of my project directly from a single solution perfect so now that I have that the next step is looking open this within Visual Studio code uh the first thing that we're going to be doing is I'm basically going to create my model for my booking okay so what I can do is I'm going to open my terminal and before I do anything all I'm going to do is just another build here to make sure that everything is running as it should be so it does not build succeeded for both exactly the one that I wanted perfect so now inside my formula.api I'm gonna be creating a new folder and this folder is going to be called models and basically here within this models folder I'm going to create a new file and it's going to be called bookings.cs which is going to stand for the booking that I'm gonna plan to create and it's going to be a c-sharp file and basically I'm gonna give it a namespace and it's gonna be formula Airline dot API dot models and then I'm going to give it publicly class I'm gonna call it booking and it's gonna be a very simple class so first of all we need to give it an ID and then I'm gonna give it the string let's say name passenger name passenger name let's make it as default empty and I'm just gonna put something similar for example string let's say passport number and we can also for example put properties you can put from flying from where and we can put rope string two like flying from wire to and lastly we can put the status as simple as that crop integer status so let's do a quick recap so what I did here basically I created my application I created a solution I added two applications one of them is going to be the web API which is going to be the producer another one which is going to be the console which is a consumer and basically here what I'm doing is I'm actually taking uh both of these I'm putting them in a solution file it doesn't really matter it just makes my life easier when I want to build them it will build them both simultaneously and basically once I did that I just created my model which is going to be responsible for the bookings because this is a sample and don't want to make this video like two hours or three hours long we're gonna be utilizing an in-memory database if you're interested in learning how we can actually connect different types of database to our.net application I have a video where actually go through the installation and the integration between policy class and dotnet I'll link it here somewhere you can actually watch it or in the description down below below the like button so right now what you can do is uh we're gonna utilizing an in-memory database but this will work also with any types of database that we want so now since we already created our models the the next step for me is gonna be before we create a controller let's create the services so here what I'm going to be doing is I'm going to be creating a new folder called services and the first thing that I want to create here is an interface and this interface is going to basically give me one method which is going to be something the message and this is going to this service is going to be directly linked to the rabbit mq and uh basically what we need to do here is we need to basically create an interface create an implementation and basically done instructed into our application and once we do that we are actually able to reference this one whenever wherever we want inside our application so before we actually create any of this we just need to install a package which is going to be our rabbitmq package and the way we do that is going to be it's very simple so it's going to be dot-not uh package and it's gonna be rabbit mq dot client okay I need to go inside my formula API and then I can run that again apologies and now we can see it's being added perfect let's go back and here right now if I come to my CS brush file I'm able to see that I have my rabbit and Cube line available for me exactly the way I need it on the other hand I need to also install this uh in my consumer but we'll do this when we start working on the consumer so now inside my services folder I need to create a new file and I'm gonna call this uh iMessage producer which is going to be responsible for sending messages and the reason I called it with an eye because it's an interface so now all I need to do is just refer the namespace and it's going to be formula line dot API dot services and I'm gonna clear the public interface and I'm gonna call it I message that covering it nope I message reducer and all I'm gonna have here is very simple I'm gonna put single command which is going to be a single method public void oops public for it for it if I can type it's gonna be something messages and this one because we want to make it as genetic as possible what we're going to be doing is we're going to be making Anonymous so the object that we are receiving is going to be of type T and that will make it easy for us in our case we want to send different types of messages it will allow us to do that and that will be really beneficial for something and to people to different uh consumers uh again it's a good practice to have right now so all we need to do is just put it off type t and then here I'm gonna say t and we're gonna say for example the message that we want to send as simple as that so here what I have is I have a an interface and this is interface it's gonna be it have only one method and this method is going to be responsible for sending that message so once I have that let's create a new file and this is gonna code message producer .cs and this is going to be the implementation of the iMessage interface so again same namespace formula API Airline dot API dot Services is going to be public class and here is going to be just simple message producer and basically it's going to take the I message producer and we're going to implement it and here we can see that I am doing the implementation for my interface so now that I have added my interface and basically I want to implement it the first thing that I need to do is actually start implementing the connection with my rabbitmq instance and for me to do that all I need to do is within this I need to start preparing for the communication with that rabbitmq so in order for us to do that all I need to do is put far Factory equal new connection Factory and I need to fix those references and basically here a factory means that I'm a it's a place where I'm actually inputting the configuration that I need in order for me to communicate with my uh rabbit mq so I'm gonna put the address the username the password and all of that so first of all if I check here I have my host name and I'm gonna put as localhost and then I have my username and a password so I'm going to put username and I'm just going to Simply as user and I'm gonna put my password and I'm gonna put it also as let's say my pass anything simple for example and basically after I do that I need to specify a virtual host and here I'm just going to make it Forward stash so basically once we have a factory the next step is we need to specify the connection so our connection equal Factory dot create connection and basically what we're doing here we're actually creating a connection from the information that we have from the factory so we think of the factory as the setup like the different configuration that we need and then we initiate a connection from this one so now once we have done that the next step is we need to specify a channel using VAR Channel equal and basically this one is going to be equal to connection dot create a channel [Music] create model and basically what this will allow us if we hover over it at C it will basically allow us to create a new channel and in case this channel is actually not there it will be created if it is still it's just gonna return it and process it so here we can see that uh to be fair I'm not really sure why it's called that it's called a model rather than a channel but in essence what we're doing here is we're actually connecting to rabbitamicio telling it okay do you have a channel called for example messages or bookings or whatever we want in case it has it will bring it back if not it will create a new one it will give us a reference to it so now that we have created the channel the next step for us is to actually create the queue that we're going to be responsible which we're going to putting the messages on so for that we're gonna put Channel and then we're gonna put Q declare and here basically we're going to be utilizing that channel to put the Q inside and I'm going to be utilizing the bookings I'm gonna call it bookings because basically what we're doing is we're creating the bookings on it and the only two configurations that I want to set is the durability I'm gonna say if it's durable I'm gonna say true and the other one is it's gonna be if it's gonna be the the exclusive and I'm gonna see through that's all the configuration that I'm gonna be adding and now once that is done all I need to do is take that message from an object I'm gonna convert it from a single object and I'm gonna make it into a uh bytes of arrays so I would be able to send it and basically what I need to do is I need to serialize it to a string and then once I see realize it I can convert it to a byte array so for us to do that we're going to be interesting the different.net6 libraries and we're gonna put first of our Json string equal Json serializer to realize it and I can fix those references and I'm gonna put here Dot serialize and it's going to take the message and then all I'm gonna put here is VAR but the equal let's see if this is gonna work it's gonna be encoding dot oops let's fix those references but utf-8 Dot when is it byte see where was it gut bites it's gonna be from Json string and that's it and now all I need to do is just basically send the message so what I did here is I was able to take it take the message I was able to create a factory for the connection which is all the configuration I created a connection I connected to a channel I created a queue that I needed I converted my object into an array of bytes and now all I need to do is just send it so now all I want to do is just publish my message so in order for me to do that I'm going to put Channel oops Channel dot basic publish and let's see what it's going to ask me for The Exchange I'm gonna leave it as empty or let me see the different implementation so we can see the publication address we have the exchange okay so let us start with that so I'm Gonna Leave The Exchange as empty and then I'm gonna put the rooting as bookings and then after that it's going to ask me is it uh for the message body so let's go down here and we'll be able to see the body that we want to send and all I'm going to say is I'm going to send the body like this and I'm gonna put here body equal body oops so that way I'm able to tell it exactly the way I want it and basically if we take a hover over this we can see that it's taking the exchange was fantida rooting key that we need and all of the rust are not mandatory except the body that I want to send to so this is basically my service and this will allow me in essence to send a message to rabbitmq so now I'm gonna come into my program.cs and I'm gonna attach it here so here what I'm going to be doing I'm gonna put Builder Builder dot Services dot add sculpt and I'm gonna pass the I message uh producer this is going to be really important is going to be a message producer and all I need to do right now is fix those references and if I do a build perfect now I'm actually able to build my application which should be good now inside my controllers I'm going to be creating a new file and basically the reason I'm creating a new controller here is going to be responsible to mimic the booking so in case I create a working from there it will help me to actually propagate the message once I create a booking to the different mq and then to the consumer itself so I'm just gonna call this bookings controller .cs and I'm just gonna copy paste whatever we have here and then change it that way it's faster so instead of having it as weather forecast we're just gonna change it to bookings and then once that is done we don't really need this so we'll remove it and we don't need this so we also remove it and let's close this we don't really need it now so that way we'll have a little bit more space and we don't need this now so basically right now we have an empty controller with no actions but at least we have a controller so now what I'm going to be doing is I'm gonna add in my service it's gonna be private read only I message producer let's fix those references here and then let's copy this one okay all right let's add it manually Visual Studio code hits again so we're gonna say formula Airline dot API dot services and now we can see it here and now all I need to do is just add the Sear instead of without the underscore and here all I'm gonna have is underscore messages producer equal message producer it's very simple and once I have done all of that my next step for me is just all I'm gonna creating is a single action which is going to be the post and here I'm gonna be basically representing the creating of a booking All I'm gonna be doing is I'm just gonna take whatever booking it exists here I'm gonna add it to my random database or basically in-memory database that I have which I need to create right now and then once I do that I'm just gonna communicate with the service and actually send that message so let's do this so first of all let's create this in memory database and the way I'm going to do that is and I'm gonna put this one's going to be the in memory DB and we're gonna put public and then I'm gonna put it as static and here all I'm gonna add to it is read only and it's going to be a list of uh booking the dto that you have created I'm gonna just call it bookings equal new that's it and now if I fix those references oh it worked surprise surprise okay so now that I have to do is I just need to create that so I'm going to put HTTP post so this action right now here is going to be repeatedly responsible for creating this action uh for creating that booking I'm gonna put public uh I action result I'm gonna call it to creating a booking I'm just gonna take a booking I'm just gonna call it book actually let's give it a better name new booking and basically what I'm gonna be doing here is fairly simple so first of all if model of state DOT is valid we're able to continue actually we can make it not if it's not valid we'll return a bad request in case it's valid all we need to do is just put bookings dot add we're gonna add the new booking as simple as that and now uh what we want to do is utilize the message producer and we're gonna say sending a message of type booking and then here what we're going to be doing is just sending the actual body which is going to be new booking oops and we're gonna return okay as simple as that so this is going to be the main controllers okay here new booking so let's see what we did here basically what we did is we created a new action we called it create booking and we gave it a post and basically it takes a booking which is going to be the model that we have created previously with Chuck if it's a valid so everything should be there if it is a valid uh request if it's not valid sorry we're returning a bad request if it is running we're just adding it to the list that we have and then we'll actually connecting with the message producer in order for us to send this message so let us build this just to make sure it's working dotnet build and we can see here building succeeded and which is really good so now if I go to the application itself so Airline API and I'm just going to hold up Natron so now right now it will not send a message but with April if it's all successful or let me add uh if it if it actually works we'll be able to actually add a message but it's gonna fail when we're actually trying to send the message so now let's open our web browser let's open Firefox let's bring it here and what I'm going to be doing is I'm going to put localhost and I think it is to see on the port 5057 so let's add that 50 57 forward slash Swagger forward slash index.html yes I accept perfect why can't I see this okay see if there's any error nope so https Swagger oh I forgot the I an index of HTML okay perfect now I can see my new controller bookings and I can see that's actually taking uh this is the object so if I click on try it out now this should fail it should add it but it should fail on the actual something so passport number one two three from let's say Beirut to say Dubai and state is equal one and if I click on execute it should give me an error which is exactly the thing that I should expected on where it's not rich it's not able to reach Target make it a bit easier let me make this a bit bigger see if this is fine okay so if we see here that it's not able to connect the rabbit mq because there's no instance of mq available for us which is exactly the outcome that we wanted because right now what we're going to be doing is we're going to be actually creating a Docker compose file and basically this Docker compose file is going to be responsible for putting in a Docker a version of rabbitmq it's going to be responsible for actually the initiated do the basic configuration the username and the password and it's going to make sure that it is running and once we've done that we're actually able to have a full version of Quran locally and our application will be able to connect to it so let's see how we can do that so first of all let me stop this and what I'm gonna be doing here is inside this root uh folder that I have all I'm gonna be doing is I'm gonna creating a new file and this file is going to be called docker Dash compose Dot tml okay so before we do all of that you need to download Docker to your machine to make sure that you are able to do that so let's go back to my web browser and here if you want to find Docker or you need to go is Docker download inject all and basically a seed guide talker and here for example you'll be able to choose whatever Docker instance you want based on your own machine so if you have Windows your download Windows you have mac you download Mac Linux the next so and so forth so just make sure you download it and you install it excuse me once you download it and install you should be able to see like a whale sign and if you open it up let's see open the dashboard you will be able to actually see it's running as it should be because it has the green uh logo that the engine is running correctly so that makes sure that you have that so once you have downloaded Docker you're actually able to do follow but make sure you download it first so the first thing that we need to do in order for us to let's go back here to make sure we are following this okay so first thing we need to do when it comes to our Docker uh compose file is we need to specify the version so we're going to put version and we're going to be utilizing version 3.8 and the first thing we need to do is we need to initialize the services so we're going to put services and the first service was the only service right now is gonna be rabbitmq and uh something that you need to pay attention to here is the space in within the yaml file or the docker compost file is really important if we must up the spacing it will not be recognized and this is something that we really need to pay attention to spacing is crucial we need to pay attention closely to the spacing of our Yemen file so now once we specify the name of the service the next step is we need to specify the container name and with the container name we're gonna call it as simple as rabbit I'm Q and then once we specify the container name we need to specify which image we want to utilize so in order for you to find the right image let me go back to my browser and here what we can do is you can go to Google just type Docker image rabbit mq and here we can see that it automatically the first one was to the C if you are able to see that properly oh it's a bit small okay let's zoom in so once you go directly to the docker Hub instance and you look for Docker for rabbitmq you'll be able to see that all of the different versions of rabbit mq that you can utilize all of them and basically you can see they have a quick reference on the different architecture that is supported you will be able also to see like how to use it how to be able to run it it will give you basically a lot of good information on how you can actually get started and basically how we can actually start implementing all of the configuration that you need to have in order for you to have a robot a rabbit amicu instance available on your machine so what I did here is I'm just gonna utilize the uh this version right here which is going to be the rabbit mq 3.8 management because the management here in Athens it will allow me to have a GUI graphical web interface which is the graphical user interface which is basically going to allow me to actually monitor my messages see that if the matches has been sent cfms has been consumed let's see a graph of all of my incoming and outgoing messages all of that is going to be available for me directly through this management tool so that's why I want to install it and utilize this version of my rabbitmq so in order for me to do that so it's gonna be here let's take this rabbit mq management and here we're going to be specifying the image see if we mess up the spacing is not happy and once I do that all I'm gonna say is I want the Alpine version which is the smallest one and once I specified a the image I want to specify the environment and inside my environment I need to specify my username and a password in order for me to configure my instance and here I'm just going to put rabbit I'm EQ and I'm gonna give it let me copy this because it's it's very uh it's much more easier the any typo it's gonna basically break it down so I can just put it here like this so the first one is rabbit and Q default underscore you the user is gonna basically give it my user let me see what did I give it as a user here user my pass let me update it so this one needs to be user and here it's gonna be my pass okay so now I have specified the rabbit mq default user and default password the next step is I need to specify the ports which is going to be allowing me to connect to it and here I need to specify two ports so the first Port is for actually the instance of rabbit Mikey which is going to be running in the background and the second one is for my web interface which is going to be responsible for me to actually monitoring my my rabbit mq instance and here we can here and for this we need to specify two ports as I mentioned so the first one is gonna be and we're gonna map the default Port which is going to be the five five six seven eight no fix five six seven two two five six seven two and for the web interface it's gonna be one five six seven two one five six seven two and here what I'm gonna do is just gonna add the hush I'm just gonna say here rub third face I'm gonna add a hash I'm just gonna say rabbit Miku instance so here we can see that the first one is going to allow us to connect directly with the instance the second one is going to allow us to connect with that web interface so if I save this right now so let's see how we can run it I'm gonna open my terminal and all I need to put inside my terminal let's see I'll ask so here I can see that I have my Docker compose file available for me in order for me to run it I'm just gonna put docker thus compose dog Docker Dash compose up it means that's and I I'm actually trying to run my Docker compose if I click on enter we can see that it's spawning the latest version of my rabbit mq it's downloading it on my machine if you don't really know how Docker works I have a different video where I go through the different implementation of Docker how it works in the background what's the process that takes but in in essence it checks locally on my machine if I have Docker a rabbit mq installed available if it is available it uses if not it connect to Docker Hub and it gets from there and now while we're speaking we can see that my rabbit mq instance is running if I go up I can see let me go to see to find the logs where it shows that setting up my username and a password so here let's see what it is so here we can see it's running it's running so here we can see it downloaded it it started to configure itself it's working on the monitoring perfect it's running and it tells me that my web management is available so right now if I go back to my web browser go back and let's go to my local instance so localhost and it was one five six seven two as you can see here directly without me doing anything or any extra configuration I got a login page requesting for my username and a password in order for me to log in so if I remember correctly it was user on my pass so let's put it here as user and my pass and if I put on login we can see it directly took me to the login page of my rabbit mq and all of that is available out of the box I did not create any of this and we can see here that it says Uh there's no information on the latest messages because nothing has been sent yet we can see here that the nodes which is available the connections the channels which has been available we can see this once we start creating and running and here we can see the different queues that we have so on so forth so let us uh go to our application here and I'm just gonna basically do a DOT not run to run my application and now all I'm going to be doing is going back to my web browser and is here instead of actually where is my Swagger it's not just I'm gonna restart this object again so execute and we can see here I got a 200 and now if I go directly to my rabbit mq we can see here that this changed so we can see here that I got uh there is a message that switch has been sent and we can see we have a total of one and we can see that a message has been received on my queue and if I go on queues we can see that I have my booking queue which has been added if I click on it I can see all of the information which is available for me here which is the number of messages one it has been sent so on so forth I can see like if I have any consumers I Can See For example the size of the message the body and all of that so here for example is still processing this so let me see what how this will happen right how we can actually start consuming all of that so all I need to do is stop this uh let's do a clear and also let's open another terminal here and I'm gonna actually go to my uh let's go to My Consumer which is gonna be the formula and then no it's gonna be called ticketing processing so right now if I do not build inside of this should be empty and fine so now if I open my program.cs and I should be able to open it up sorry I should open my program.c ask this one we can see it's completely empty let me see if this is looking fine perfect so all I need to do right here is let me update this so I'm actually able to process the queue which is coming in so I'm gonna say here welcome to the ticketing service for example I need to basically have the same Factory as I did before in order for me to start processing it or before we do that I need to install my nuget package so I'll just installed this nuget package so it's gonna be dot not add package rabbit I'm Q I think that was it let me show about mq dot client I think client okay perfect so right now if I click on my CS Pros I'll be able to see it there perfect so now let's come here and let me add the factory so it's gonna be VAR Factory equal the new uh I think convection Factory production factory without the O oops okay let's fix those references and now this one is basically gonna ask me to fill in the same information so all I'm gonna be doing is I'm just gonna copy paste whatever I have put in the message producer which is this one all of these up to this point instead of me typing it all over again so let's come back here where is it it's close this uh Destiny just paste this here so what I'm doing is I'm just taking the same configuration that I have done before instead of me trying to retype them again so localhost again user bypass same virtual host and then basically I'm creating a connection I'm checking for the channel and I'm saying if you don't have a channel it's called booking created if not we can re reutilize it so once we have done all of that the next step for us so all we need to do is just put VAR consumer equal new Eventing basic consumer I requests see if we need to fix those reference yeah and then from here I need to pass the channel that we have created and then once that is done so now that I have that all I need to do is just basically have my consumer to start receiving and I'm going to be utilizing basically an incremental way so whatever the match is coming in it will basically allow to receive multiple messages simultaneously and the way we do that is pretty straightforward so put consumer dot received plus equal and then we're gonna specify here the arguments we're gonna be having a Anonymous function and this Anonymous function is going to be allowing us to actually take the event excuse me on the arguments and then basically built from there and I'm going to specify here model and I'm gonna say event arcs and once that's done all I can do here for example is I can put VAR oops I forgot the arrows so I can put here bar body equal event args Dot body Dot oops to array and then here what I'm doing is basically I'm getting here my uh I'm getting my uh byte arrays that I have which is because the body is a type byte arrays and then once I have done that I need to convert despite arrays into text so I'm gonna see here for a message equal uh encode encoding let's fix this Dot utf-8 Dot where is it where is it cut string got string where is it let me type it to get string okay and just put body and now all I can say is console dot right line I can just put message okay uh let me put string concatenation a message has been received and I can put the body of the message here and foreign okay new ticket processing is initiated something like that foreign so once I done all of that and basically I am actually consuming my message let's put the semicolon at the end once I did all of that I'm actually consuming the messages that has been sent to me the last thing is I need to do is just I need to stop the application to continue further so I'm gonna put first of all Channel Dot basic consume there is a basic consume and let's see what it's expecting as you can see it's expecting the Q name the auto lock the consumer tag and let's see the different implementations string okay I think that should be fine it's clear here okay yeah the only reason is I'm just changing the I change the screen locations and the configuration of it so that's why I'm a bit worried about how OBS is actually recording the screen apologies so what we can do here is I can just specify the queue that I have which is it was called booking so let's take this and then once I have this here all I need to do is I'm going to put the auto help for to true and then it's just causing the consumer tag which was also booking I think at me double check a consumer tag I think it's the one that we created here oh yes the consumers we call the consumer and that should be it and lastly All I'm gonna put is console.red key so we can stop the application from keep on running so very simple well the implementation is quite simple so all we did is it created a connection Factory once we created a conduction Factory we are able to actually get the information that to create a connection make sure that the model is there then we basically created a consumer we converted configure the consumer to connect to the right queue that we want and basically read those messages once it's actually started reading those messages we just decoded the body which is going to be of which is of byte array we convert into a string and then we created a simple sentence which is popping out the message out on the screen and that's it basically so now once we have done all of that let's just do that not build and right now it's the safest building as we make this bigger so Bill succeeded so now if basically all I need to do is just run it if I put dot not run we should be able to see that my message is appearing so not natural so we can see that the first message that I sent it should appear here let's see let's see let me run the other ones as well so this one here let's run this as well this is running and here we can see that this is running as well okay it says user is not authenticated why okay I think I put here okay let's see let me try to send a different message so let's go back to my web browser and just send the message we got a 200 if I come here we should be able to see that message popping up we can see that there's a message incoming and now if I go here we should be able to see that for this passenger Muhammad 2 uh sorry passenger with the id2 and Muhammad Lawan from Beirut to Dubai has been received by my ticketing service so right now let's go back to my web browser and let's see that this information so let's compare them is that clear again sorry for this a bit more so a bit more okay so here we can see the body id2 id2 password one two three from Beirut or Dubai so now let's do a quick summary of what we have covered today in essence today what we were able to do we have discussed what is rabbit mq and how we can actually what is the uh what are the terminologies and the different implementation that we can do there and then we created an application which is the new web API which is responsible for sending a messages which is going to be the producer then we created a console application which is going to be the consumer and responsible for receiving those messages and I hope that this video was helpful please like share and subscribe if you do if it was it will have the channel as well if you'd like to have access to the source code please consider supporting me on patreon uh as well if you have any questions or if you have any ideas of anything else you would like me to implement regarding rabbit mq please let me know and we'll basically create another series on this specifically regarding the different implementation that we can have there uh I hope this video was helpful and have a great day
Info
Channel: Mohamad Lawand
Views: 33,034
Rating: undefined out of 5
Keywords: .net 6, .net, api, c#, coding, entity framework c#, asp.net core tutorial, .net core, asp.net core, web api, redis cache manager, dotnet core tutorial, dotnet core web api, dotnet core, dotnet performance, background task, dotnet background jobs, dotnet backgroundservice, background tasks, background jobs, dotnet hangfire, dotnet message broker, dotnet message queue, rabbitmq tutorial, rabbitmq docker, rabbitmq dashboard, rabbitmq dashboard tutorial, rabbitmq docker compose
Id: eEipVEq8F1k
Channel Id: undefined
Length: 60min 14sec (3614 seconds)
Published: Mon Nov 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.