Asynchronous messaging with Azure Service Bus

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to the tech 9 factory video blog in this video we are going to talk about a synchronous messaging with other service bus Before we jump into demo it's worth to talk about some interesting facts in this video I'm not going to talk about all features available in the azure service bus in this specific video I would like to talk how to use Azure service bus and micro service architecture so here specifically we are going to talk about a synchronous messaging when talking about communication between micro services we are talking about integration events so we can try to imagine that we we have like a car rental company and we have like a system for it and this system was architected with micro services and right now we have like reservation micro service responsible for managing reservations for customers and we have also catalog API micro service and this micro service is available for providing information about the cars that are available for rent and right now we can try to imagine that if there is a price per day for a single car in a catalog API and if this price is changed probably we would like to also inform reservation micro service that this change happened and we would like to update this price this price in all reservations so we don't want to tightly couple micro services and that is why we will use the azure service bus and publish/subscribe pattern to provide a synchronous communication so it's important to mention that with other service bus topics we can publish a message to a topic and then multiple receivers can receive this message so here I have like a simple solution diagram and as you can see we have for micro services catalog micro service reservation micro service payment micro service and rent micro service and all those services are not communicating directly with with themselves so here as you can see on the right side there is other service bus used and if there is an event happen in one of those micro services this event is emitted to Azure service bus and and all interesting interested subscribers can receive this message this information about event so if there is a a price change in a catalog micro service we can publish this information and other micro services like reservation micro service can subscribe to those changes and can handle had this change in its database so in this case in a thorough cash for readies okay great that was about theory right now let me open as a report all and let's see what's happening there and also I would like to present a sample project that I developed to present how to communicate between micro services using Azure service bus the project we are going to discuss is available on my github the link is provided in the video description the project is called Cars Island micro services and can be found in a Microsoft Azure repository so here it is my other service bus instance in the azure portal and let me open topics section here in a topics I have registered one topic it's called cars island events so we will send all different events that happen in our solution fruit this topic so let me open this cars island events topic and let me then go to the subscriptions so as I mentioned before we can subscribe to different topics so here for the subscriptions I have to subscribe Asians catalogue subscription and reservation subscription so each subscription was created for each micro-service in my solution so currently for the catalogue micro service and reservation micro-service okay so right now let's open visual studio and let's go through the source code here it is Kars Island solution open in a Visual Studio 2019 as we can see there are four micro services so catalog payment rent and reservation and in this video we are going to talk about catalog micro service and reservation micro-service here also I have data from the catalog database we can see that there are four cars available for rent and every car has a column called price per day so this is the price that customer has to pay to rent the car for one single day so here is the BMW car and current value is free 330 but we would like to update this price to be 400 for instance and right now with this change in the catalog API we would like to also inform reservation microservice that this price per day for this specific car has changed and then in every single reservation where this car was reserved we would like to update this price per day so let me go through the source code let me start with the catalog API so here in the catalog API there is a folder called controllers and we have cars catalog controller let me open it and let me scroll down here is the the method called update car racing so what will happen here we can we can pass the car to update model and if this specific car exists in the database what we would like to do we would like to check whether the price per day has changed and if the price has changed we would like to also save this information in the house price per day change variable so once the price is updated we can call update method on the cars catalog dbcontext to update information about this specific car and also what will happen we would like to also send a message through a thorough service bus so reservation micro service can receive it and update this price per day value in all reservations where this specific car was reserved so what will happen here let me scroll down we can check whether price per day has changed if it's change we are creating price per day change event let me open this class so car price per day change integration event derives from integration event class this is the abstract class with two properties unique ID and creation date and right now let me get back to the car price per they change integration event so here in this integration event in this message that we will send through the other service bus we would like to pass information about car ID so for for what card is for which card is a price was updated and also we would like to pass the old price per day and the new value value for this price per per per day for this specific car so we will pass all those free values in this message that will be sent through the auto service bus let me close this class for now so once we create this event what we can do we can call publish events through even bus acing and this method will be called on the catalog integration event service let me let me leave ad and save event async method for now we will not discuss this method in this video let's focus on this publish events through event bus acing so as we can see here I passed this price per day change event so let me open the definition and let's see what's happening here inside this publish events through event bus async method what we can see first of all we we can mark this event as in progress and as I mentioned we will not talk about the events tour in this video the more that the most important part is here so here I'm calling event bus instance and I'm calling publish a sing method on it and here I'm passing the event so here I will send the message through the azure service bus let's see the implementation of publish async method so let's go to the implementation as we can see here I'm creating service bus message so this message is the the class available in the Microsoft Azure service bus NuGet package and in this specific names namespace so if you would like to use a thorough service bus you have to install this nougat package so let me get back to this source code here so once I open the implementation for sending or not this one for for sending these messages here so publish acing metal we will see that I'm creating the message this message has specific message ID I'm passing the body and this body will be the integration event which I passed so in this case car price update and there is also a label so it's assigned to event name we can we can place labels in the messages so the subscribers can decide whether this message should be handled or not so we can filter messages in the other service bus so as you can see here below I'm creating a topic client from the other service bus SDK and I'm calling saying send acing metal and I'm sending this message so this message will be sent and all subs treiber's will receive this message so right now let me close it and let me open reservation API here and let me open court dependency injection and integration service collection collection extensions and here what's happening here I have to set up connection with other service bus and also I have to say to which events I would like to subscribe in the reservation micro service so as we can see here I have this line where I'm registered registering even handler for car price per day change integration event so for each event we have to register even handler to decide what will happen in the micro service once this message is received from the service bus and please also note that here above I have this event bus subscription manager it's because we would like to decide whether a specific subscription was created in the micro service or not so let me open this subscription manager right now so here in this class we can manage subscriptions that are related to a drawer service bus so we can add subscription so we can subscribe for a specific event we can also of course unsubscribe so below there will be remove subscription method so we can unsubscribe for a specific events we can check whether a specific subscription exists for a specific event so I really encourage you to go through the source code that is available on my github let me close this class for now so let's see what's happening below here as we can see I have to also register service bus connection management service and this class is responsible for managing managing toppy client so here I'm checking whether a connection to a specific topic was closed or not if it's closed I have to create a new instance of the topic client and this class is is required to send the messages to other service bus topic below here I'm registering as a service bus even bus instance so what's happening here there is a factory method and we can see that here under eye event bus interface I will register this specific implementation so let me open a thorough service bus event bus this is the implementation for managing messages that will be sent and received through the azure service bus and inside this asura service bus event bus class what we have here there is a publish method so we will use this method to publish events through the other service bus but also as we can see there is subscribe ace in metal and here you can notice that we are using subscription manager to make sure that we are not subscribing to the existing event so here what we what we can do we can check whether we already subscribed for the event and if we do not subscribe to a specific event what we can do we can add rule on the subscription client that is available in the azure service bus namespace it's from this nugget I mentioned before we can add rule and as you can see we are setting filter to be the event name so if I would like to only receive in events about the car price per day updates here I have to set this filter so we can subscribe to different events and we can filter those events those messages in the other service bus and once I add this rule to the subscription client of course I have to also register this information in the subscription manager to make sure that that this subscription will be stored in this subscription manager a dictionary of course there is also a unsubscribe method so if we decide that we don't want to receive specific events we can remove this rule using remove rule acing on the subscription client instance and again we have to remove this subscription using subscription manager remove subscription a method so below here there is also register subscription client message Handler and this method is responsible for handling receive messages so let's also discuss what's happening here on the subscription client instance we are calling register message Handler and here inside this method we will receive messages from the other service bus and we will process them also so let's see what's happening here we can get event name because this event name will be assigned to the label property on the message that is received from the other service bus we can also extract even data so here is the message data we can get message body and we can get stream once we have this information we can call process event passing the event name and the body of the message and once we process this message we can call complete acing on the subscription client in sons to inform other service bus that we successfully received this message and we proceeded so it doesn't have to be no longer in the in the other service bus now let's open process events metal here inside this metal first of all we have to make sure that we have subscription for this specific event if this subscription exists what we can do we can get handle for this specific event so here we can get all the subscriptions for for this specific event and for every single subscription information we would like to get a handler so we would like to get all the handlers for specific event and here below there is a code responsible for creating creating this event handler and below there is also methods involved on this handler called handle acing so every event handler has handle a sync method to handle this event that was sent through the other service bus and of course once the message is processed processed we can set process as true and we can return it and then in in this in this processor we can again if if this message was processed successfully we can call complete acing metal on the subscription client ok so right now let me launch this solution and let me present it in action and of course once again I encourage you to visit my github repository and go through the cars island micro services project too to see the details of this implementation I launched to API so catalog API and reservation API and right now I would like to update price per day for existing car so let's call put method and here I will replace sample data with with data about a specific car in this case BMW so here I would like to change price per day to be 400 so let's click execute button and now let's go to the visual studio as we can see here in the catalog integration event service we have new event so let's see we can see that for car ID we if for for this specific car so in this case BMW there is an event so old price we can see that was 330 a new price is 400 and right now if I click continue this event will be sent and please note right now that in the reservation API in the car price per day change integration event handler handle acing metal was cold and here there is information that new event was received and we can see exactly the same data so this message was sent through the other service bus and this message was received by reservation micro service and right now I can get all user IDs from the reservation repository and I can get all the reservations for a specific customer so in this case in the for each loop I can get customer reservation and I can call update price in customer reservation metal so let's look what's happening in this update price in a customer reservation metal so here I can check whether car in the car ID in this specific reservation equals car ID that was sent in a event and if this car is equal then I can check whether the price per day for the car in the reservation equals the old price and if it equals I can update the price per day to be the new price and then we can call up the reservation a Singh metal so here we saw how to implement a synchronous communication between micro services using Azure service bus once again I encourage you to visit my github and look at the cars island repository to learn about implementation details thank you so much for watching this video hope you found it interesting and helpful if you have any additional questions please do not hesitate to contact me either on Twitter or LinkedIn and of course I encourage you to visit my blog tec-9 factory comm see you in the next video
Info
Channel: Tech Mind Factory
Views: 6,511
Rating: undefined out of 5
Keywords:
Id: aJn5CDhWvJQ
Channel Id: undefined
Length: 22min 38sec (1358 seconds)
Published: Thu Jul 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.