RabbitMQ in .NET Core (Part 1, Single producer and consumer with Queue)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dot net code central in today's video i'm going to discuss about revit mq in dot net core now revit mq is a vast topic and it is not possible to complete it with one video so this is going to be a series of videos and in each video i am going to take one aspect of the revitamq and walk through it but before we start understanding what repeat mq is let's discuss why i am discussing revit mq right now in my last couple of videos i discussed about microservices and if you have not watched my videos i am going to provide the link up there as well as in the description i would suggest you go through this video because it will give you a little bit of context on why debit mq is very important now when we talk about microservices one of the concept we discussed is communication between microservices and having a decoupled communication using something like a message queue and revit mq fits right in there so what is rabbitmq rapidmq is a message broker now before i get into what revit mq is let's discuss what is a message broker a message broker you can think about it like a post office its main responsibility is to broker the message between a publisher and a subscriber or a set of subscribers once a message is received by a message broker from a producer it routes the message to a subscriber message broker pattern is one of the most useful pattern when it comes to decoupling microservices and this is something which i mentioned in my previous videos as well to understand what a producer is a producer is an application who is responsible for sending messages so when you are talking about couple of micro services it's one of the micro services who is sending a message now a micro service can be both a producer as well as a subscriber and there are scenarios where it will be the case a consumer or a subscriber is an application listening for the messages and the queue is the storage where messages are stored by the broker and once we get into deal of revenuemq this concept will be more clear now let's understand what is rabbitmq as i mentioned earlier rebidem queue is a message broker it's an open source message broker and it is probably one of the most widely used message broker out there rabbitmq is extremely lightweight and it's very easy to deploy and i'm going to give a demo of how to install revit mq using docker and you will see that it's going to literally take like a minute to do it rapidmq support multiple protocols and we are going to discuss the main protocol which is supported by revit mq and revit mq is highly available and scalable in terms of availability from my experience rev mq never went down in the number of years i have used revit mq it is extremely reliable so what are the protocols that are supported by rebin mq avdmq support multiple protocol but the main protocol which comes out of box is mqp091 and that's the protocol we are going to use when i'm going to go through the demo but there are multiple other protocols which are supported through plug-in mqp091 is a binary message protocol specification and this is the core protocol specification implemented by rabbit mq all of the protocols are supported in revit mq through plugins so the other protocols supported are stomp which is a text-based message protocol mqtt which is a binary protocol focusing mainly on published subscribe scenarios amqp 1.0 now mqp 1.0 is not necessarily an upgrade from amqp091 it is completely different and it is much more complex as far as i read through the documentation it is not supported by most of the clients and the last one is http and websocket now it's time to show our demo and in the demo we are going to use rabbitmq and i'm going to use a singles producer and consumer scenario for this demo so for that first i'm going to create a dot net core console application which will act as a producer hence i'm going to create a new dot net core application and i'm going to name it as rebelmq.producer now the project is ready but we need to install rebidmq so let's do that fast before i move on to code it so i'm going to install a docker image of revit mq now if i do a docker images right now i don't have any wtmq image installed so what i'm going to do is i'm going to use a command which is going to install the rebit mq as well as it is going to start it at a particular port so this is the command i'm going to use it is docker run d the host name i'm just going to give my credit and the name of the instance will be ecom revit and this is very important we have to expose the ports which we want to access from outside so the ports that are needed is one is five six seven two that is the port used by mqp protocol and this is the one we'll use in our code and the other port is one five six seven two which is the port used by the management console now the wmq management console is extremely powerful and we'll see how to use that and i'm going to use the rebutemq management image so as you can see it is not able to find the image locally so first it is going to pull down the image from docker install it and then go and start the rapidmq engine and as you will see that within a minute the entire thing is installed and now if i do docker logs dash f e 67 three digit of the image you can see the management console is up and running and my revit mq is ready to use so now if i go to the browser and i open up localhost 15672 it's going to show up the rebidem queue console and by default rbmq has a username as guest and password is guest so i'm going to use that and we should change the password after we login now this is the management console it is extremely helpful and powerful management console and you can see it's going to give a snapshot of what is the health of this node and we have a single node and what is the disk space usage what is the memory usage and all these things this is very powerful and useful at this point in time we don't have any queues so there's no queue and these are some exchanges and i'll come to exchange and what they mean how they're used in the next video for this video we're just going to focus on cues this is the admin view where we can go and manage users and other features for users right now the guest has access to everything so we can go and clear the access and give access to what exactly we want what kind of permission we want to set if we want to change the password we can change the password of the guest here so these are some of the things we can do now i'm not going to spend too much time on revit mq admin screen if you go through the documentation in the web it has very good information but if you like me to cover more on the admin screen please leave a comment below and i'll cover in subsequent videos now let's go back to the project that we have first thing we have to do is we have to install the new get package needed for revit mq and there is revit mq client so this is the revitamq.client and as you can see it supports dotnet standard version 2.0 so it is going to work for our case once it is installed we are going to go back to the main and here we are going to create the publisher or the producer the first thing we'll do is we'll create a connection factory so for that we're going to say factory is equal to new of connection factory and for that i need to add the namespace review.client and for connection factory we have multiple option to pass we're going to pass the uri and the uri is going to be new of uri and here i'm going to pass the uri of my rebedmq client that i created and this is going to be the amqp pattern so it will be amqp colon slash slash the username call on password at the rate hostname and port number now we know our reby mq is listening on port 5672 for amqp so that's what we have passed this is the localhost now we have mapped the port of the docker container to localhost 5672 so if we use localhost colon5672 it's going to call it back to the docker container so now we are ready with the factory after the factory is created we can create a connection so we can say factory dot create connection and this is going to return i connection object now factory has multiple parameters now one is provider name where we can create a connection specific to a provider or endpoints but we are going to use the default one which is without any parameter after the connection is created now we need to create a channel so we are going to do using our connection dot and for this one we are going to use create model which returns an i model which is nothing but a channel created so as you can see in the description it says creates and return a fresh channel session and model now once we have access to the i model what we are going to do now is we have to declare a queue so we're going to use channel dot q declare and for the queue we're going to pass the queue name so we can say the queue name is demo queue and then the next thing is you see the property as durable and for durability we are going to say true because we want the message to hang around there until a consumer reads it then the next one is exclusive so we're going to set it as false and i'm going to discuss the the concept of exclusive auto delete and arguments later when we get into exchange for the time being i'm going to just set all of them as false and arguments is going to be null and arguments are going to be important when we go and discuss exchanges so here we declare the queue so once we declare queue it's now time to publish a message to this queue for that first we declare a message or message equal to new let's create an anonymous type right now so it's an anonymous type the reason i'm creating anonymous type so that later on in the consumer we can see that we can pass complex object not just string but of course when we send it it will be going we'll be converting into a byte array so for that we'll say body is equal to encoding dot for encoding i'll add the namespace system.txt get utf8.catbytes and we need to get byte from a string but the message is an object right now so i'm just going to use json convert and i don't have js newtonsoft.json so i'm just going to install the latest version and then after it is installed i can use the serialize object method and serial as the message now the message body is in byte array format now what we can do is we can now say channel dot basic publish and you can see the publish has an exchange name so in our case we are not sending it to any specific exchange we are going to keep it as empty but we are going to discuss the significance of this variable in our next video when we are going to discuss about exchanges but for now we are going to pass empty next one is the routing key and for routing key it's nothing but the name of the queue and when we discussed exchange it will be clear how queue and write routing key match with each other and for the basic properties it's going to be null and last we are going to pass the body of the message so now our producer is ready and it is going to pass this message to the queue next thing i'm going to do is i'm going to create a consumer and for the consumer also i'm going to create asp.net core console application and i'm going to name it as repeatmq.consumer and once this project is created i'm going to add the same nuget package and in the program i'll have similar code so i can just copy paste part of the code from here and then we're going to change it for client specific so let's add the namespaces now here after the queue is declared we don't need any publisher specific logic this is where we're going to create a consumer and we're going to create a eventing basic consumer which is part of the rapidmq.client.events namespace and as you can see the eventing basic consumer takes an eye model in the constructor which is nothing but the channel so we're going to pass it and after the consumer is created we can do consumer dot received and here sender and event arguments and then we can say body is equal to even dot body which is a read-only memory and then we can do a two array which is going to return the byte array of the message and from here we can save our message is equal to encoding dot let's add text system.txt space here utf-8 dot get string and we're going to pass the byte array which is the body and then finally here we can do console dot writeline of the message now i'm not digitalizing it into an object i'm just going to print the json string as is for the timing and then finally we are going to say channel dot basic consume and for the consume we have to pass the queue name which is demo dash queue and auto acknowledge we are going to set it to true which means it will automatically acknowledge we don't have to we can specifically acknowledge or not inside of the received message for the time being i'm just going to set auto acknowledge equal to true and then finally for the consumer i'm going to pass the consumer object so now the consumer is ready and set first i'm going to run the consumer so that the consumer get a time to subscribe so if i run the consumer what we are going to see is i have to give a console.readline here otherwise consumer will come out so now the consumer is up and running what we can do is if i go here i see that the queue is already created by the consumer so we have the queue and you can see that one consumer is connected to the queue there is no message yet now if we go back and if we run the producer which produces a message so the producer will produce message and come out now if you go to revit mq we should see a message coming in so here we see the message came in and it's acknowledged by the consumer and if we go to the consumer we can see so here is my evidence consumer.exe we can see the name is producer and message is hello it's printed by the consumer and you can see the message has come in and it's been acknowledged by the consumer now if i run the producer again we will see the consumer getting the message once again so i'll just do that and i'll just start without debugging and we can see the consumer got the second set of message and if we go to revit mq we can see the message has been received by the revit mq and then acknowledged by the consumer so this is single producer consumer scenario through a single queue and this gives you a very basic introduction of revit mq again review is fast this is very basic introduction but it shows you how easy it is to install start running and using revit mq in dot net core it literally took less than a minute to install the revit mq in a docker container get it up and running as you can see it is still running here and then we have the management console and then created a consumer and producer within few minutes and get going that's all i wanted to cover today in my next video i'll start with a single producer multiple consumer scenario with q and then i'll go on using exchange and describe more about the exchange and how everything works with an exchange if you like this video please give me a thumbs up and if you are new to my channel and if you think you are going to get value out of my channel please subscribe to it thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 46,695
Rating: undefined out of 5
Keywords: rabbitmq, rabbitmq .net core, rabbitmq in .net core, rabbitmq .net core queue, rabbitmq .net core microservice, rabbitmq in .net core microservice, rabbitmq in .net core microservices, rabbitmq in asp.net core
Id: w84uFSwulBI
Channel Id: undefined
Length: 19min 37sec (1177 seconds)
Published: Sun Sep 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.