GitHub Copilot For .NET Microservices Developers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this second part of a super series I'll show you how to 10x your.net micro Services productivity with GitHub copilot let's start so let's do a quick recap of the scenario that we're trying to implement here right so you remember in the previous video uh what we did is we implemented this Matchmaker Mecha Service uh with the help of a charge DBT right which is able to receive requests for a match from from players right I need a as soon as it notices that there are at least two players available a for a match they it joins them into a match and then it returns that match it back into the into the client but what we want to do now is that we want to come up with this brand new game Management Service which is in charge to go ahead and somehow listen to these requests for a for games for matches that are already and waiting for a game right because we had the players who need a game so it is able to receive that request and then it will go ahead and prepare a game in some might be some port and then it is going to go ahead and send back a the response into Matchmaker allowing Matchmaker to complete the match within the missing information the IP and the port and and finally Matchmaker can go ahead and return that information back into the client that our players are using right so in this video we're going to go ahead and implement this second microservice we'll see how these two microservices can interact and we'll see this other great AI tool that we can use in conjunction with judge BT so let's now go into the code okay so here we are back in business Studio code in our hello AI directory right and remember here where we have our Matchmaker maker service and our unit test right so now we have to go ahead and come up with our new maker service is the one that we call the game manager right and so we have to start thinking about how is that we're going to be communicating between these two microservices and so in order to implement this the first thing we're going to have to Define is what we know as contracts right so the contracts are these are the these messages are going to be exchanged between the two microservices so to do that let's go ahead and open our terminal right here I'm going to go into my terminal right and so let's go back into our hello AI data over here and we're going to do is just spin up a brand new class library for uh for this purpose so net new last sleep and we're going to name this one let's just name it contracts right and so yeah so now we have our contract directory on the left side I'm going to switch context very briefly by clicking on hello AI on the bottom left and switching into contracts yourself so that omisharp can give me a better intellisense into that new project okay so I'll collapse this and I'll go into class 1 over here now before we run any any code over here what we wanted to do as I mentioned before is that we want to use a new AI tool to assist us here right so we already know about chargept but there are there's another pretty cool tool that you can also use to assist you directly in your code environment here so that AI tool is named GitHub compiler and this is the page for the hook pilot just just look for GitHub copilot in your search engine as you'll be able to land in this page right and this one here is a tool created by GitHub and that it can provide you with intellisense and with a lot of suggestions directly into into your IE uh based on the AI That's behind this tool right and you can see all the information about it compiler over here this tool here sadly it is not free so there's there's a need to pay for it and for individuals it's going to be the price that you can see on the left side over there but there is a trial which is I think at this point at the time that I'm recording this it's going to be about 60 days of every trial so you can try it out and see if it works for you uh but uh yeah otherwise it is a fantastic tool so let's go ahead and minimize this and go back to vs code and the one thing that you're going to need here in order to I mean after a day eventually tryer or after purchasing the the service is uh you're going to need an extension to vs code so that it activates all the GitHub copilot features in there so I'm going to open up my extensions view here and what you want to do here is just look for GitHub compilot just like that and then it should be the first extension on the list over here not the nightly one just the one that says keep the hookah pilot I'm going to click on that one and so this is the one that you want to install so I'm going to go ahead and click on install if you're installing seconds yeah it is there and so yeah we can now go back into our explore review and close this and what you want to do is just confirm that on the bottom right right over here you should have that symbol over there of the GitHub pilot has been active Okay so now let's go into our class one file over here unless you rename the file into contracts because this is what we're going to just play these contracts and then let's collapse this and we're not actually going to be defining the contracts as classes we're going to be using record types instead of a classes because they just makes a lot of sense for this now notice that GitHub copilot is already starting to suggest things that we may want to do here now in this case it doesn't make sense just now so for now let's not use that let's go ahead and start implementing our record types so we're going to be needing two record types over here the first one is going to be the one that matchmaker is going to be using to send a message into game manager anytime we have a match waiting for a game so let's define public record match waiting for game all right and so for this one really all we want to receive here as a parameter is the match ID so we're going to Define here int match ID all right that's going to be enough a for game manager to start working on that match ID and provide a game and so as a game manager has completed the job it needs to send a message back into Matchmaker so let's define another record for that so it's going to be four leak record and notice that again copilot is starting to suggest things that we may want to do uh it still doesn't make sense for us so we're just going to be naming this one again create it okay and so what would we want to include in that message uh notice that these two two properties here or these two parameters actually do make sense we do want to divide the match ID and the game ID and so it is not all of that they want but it is part of what we want so I'm going to take a I'll do tab to accept that suggestion and all I want to do here Saturday just reverse the order here that's not exactly the order that I wanted I want to say game ID first and then match ID right but that's not all that we want we also want to have a couple of other things like the IP address right which is essential and then notice that copilot said well if you need an iPad there's perhaps you also want a port which does make sense we do want the port so I'll just do Tab and now we have our game created a contract ready and now that we have the contracts the next step is going to be to create the actual project for our brand new microservice so for that let me open up my terminal over here and then perhaps let's also show the Explorer so we can see the files are going to be created and so let's go ahead and provision a brand new product with a DOT net new in this case in this case we're going to be using the a worker template and this is because we don't need really address API for this this other microservice this is microservices the only thing that's going to be doing is just listening for messages and sending messages back right so in that case you don't need to provision any sort of web API all you need is a way for consuming and sending messages so it's going to be a worker project and then yeah the name is going to be game manager and by the way if you see any suggestions in my terminal these are not because of copilot that's just a simple Powershell feature so which is great uh but uh yeah that's not going to be compiled so let's go ahead and hit enter right there and then on the left side now we have our game manager a directory provision so I'm going to close this and collapse that so the first thing that we're going to do now is to switch the context once again using this selector on the bottom left we're going to switch from contracts and into getting mad data CS proc just so that we get better intellisense uh when we work on game manager and then one thing that we're going to be needing right away in game manner is a reference from game manner into contracts because we are going to definitely need to interact with the messages that are defined over there so what we're going to do is just switch into game manager directory in terminal and then we'll do dotnet at reference and that's going to go into contracts contracts at CS Pro so I'll hit enter and that gives us that reference okay and so the next thing that we're going to need is a some way to start a you know sending and consuming messages right and there are many ways to do this and there are also many Brokers message Brokers you can use for these now the one that we're going to be using for this a for this demo is the rabbit and Cube message rocker and then we're going to need is some sort of a library that allows us to both publish and consume messages from queues the queues are going to be using in rather than queue so to make things simpler we're going to be using another library that is called mass transit it's an open source project and it's a fantastic library for you to publish a zoom messages in a very very simple way right so in order to use mass transit what we're going to need is a right away is a nugget package preference so while being in the game manual directory I'm just going to do the net at package and then the package is going to be Mass transit.rabbitmq so I'll hit enter and that's going to provide us to with the reference to arriving queue beautiful so I'll collapse this now let me collapse that and then now that we we have that set up we are ready to start implementing the logic of the game manager now we have to account for two kind of two situations the first one is that situation where Matchmaker sends a message into game manager and then the other one is the one where game manager has to send a message back into Matchmaker so let's work on the very first one first which is when we have to consume these message that's coming from Matchmaker and so the way that you consume messages is using Master Transit is by implementing what we know as consumers and so in order to prevent our consumers and we're going to firstly create a directory under game manager let's call this one just consumers and then the convention for defining these consumers is that you want to use the name of the message that you want to consume and then just add a suffix with a that's just name consumer right so let's take a quick look at that a contract once again the contract was named a match waiting for game so I'm going to copy that and then back into consumers I'll say new C sharp class but waiting for game and then I'll append consumer just like that okay and then let's collapse this and let's simplify this a little bit by converting this into a files called namespace and then let's remove on this value usings let's so now we're ready to implement the consumer in master Transit the way that you define a consumer is just by implementing one interface which is named AI consumer so I'm going to come here and I'm going to go ahead and implement this brand new interface and now I'll just go ahead and start typing but as you can see gecko pilot is actually trying to offer us some a default implementation of a consumer we have options just before we can cycle through these options over there see if any of those could work for us so let's say we're going to be going for this one so just hit tab I noticed that so this this defines a consumer it also defines that we want to be consuming much waiting for game messages right so that's smart and then let's start implementing based on this so I'll just do Ctrl dot in iconsumer using mass transit right there I'll do control dot here and using contracts and that gives us an initial a state where we want to go from now it is great that we have a loggers we are going to be using that but we don't need these I have contacts over here so let's remove that we don't need that and let's also remove that from the Constructor over here that's not that we need it and so and yeah we're going to be removing this last line over there makes no sense and then the first thing that we actually want to do here is just to capture that message that we got from the Matchmaker mic service so for that we're going to be saying bar receipt message equals and I notice that on the right side we get a suggestion for context that message which makes sense so I'll do tab the next thing that we want to do is in fact we want to lock something here just stating that we have received that message so let's implement or let's improve this a little bit by saying uh received much waiting for gang message for match and that's going to be our match ID right and so let me send this into another line so we can see better and let me scroll down a bit right and the parameter is going to be just um receive message dot match ID so at this point is where we have to actually implement this Logic for a provision this game will create this game so like I said that's the part that we're going to kind of simulate we don't want to really be going into the Super complicated task of provisioning a game uh so we're just going to say to do implement the game provisioning logic or magic here right so that we're not going to do uh and so for now as it says we're going to go ahead and just simulate that we are taking some time to do the job so we're going to say oh wait does that delay and then we're going to be waiting some time it is suggesting at 1000 milliseconds I'll have to use I want to say time span that from seconds and it's going to be yeah five seconds looks good all right so assuming that at that point we are done with the creation of the game now we want to go ahead and prepare the message that we're going to be sending back into the Matchmaker microservice so the suggestion doesn't look too bad but let's let's refine this a little bit we're going to say that this is going to be outgoing message equals new and then yeah in fact we do want to return again created message so the suggestion it looks uh not too bad so I'm going to I'm going to accept it and then we'll have to complete this so that it makes more sense now for the game ID what I like to do is to actually use a good as opposed to an in for game ID so let me just go back quickly into getting created and let's define that game ID is just going to be of type good right so that's going to be a good and so we enable it now what we can say is game ID is going to be go it now and add new guide and then yes for match ID we want to use receipt message that much I didn't make sense and then the next thing is going to be the IP address right but how we're going to come up with that IP address right so this is the point where we want to kind of simulate the generation or somehow come up with an IP address right so we could go and into you know stack Overflow and try to figure out so how do I generate a random IP address or what we can do is well see if copilot can help us here so let me actually comment this for a moment and let's scroll down here and what we're going to do is well first let's fix this at the bottom which makes no sense and then just after the definition of this method which I think is our consume method let's go down and we're going to do is just say well generate a random IP address right so we're just going to type that comment I'll hit enter and then down here what we're going to see is that uh copilot is going to offer us a song way that we could go ahead and generate a random IP address right as you can see right there we have some suggestion okay so let's take it and so this is suggesting that we're going to be returning an IP address object so I'll do a control dot I will have to be using system.net for that work so that should provide us with a random IP address and so let's go back into our message over here let's uncomment this and let's just say uh Generate random IP address right and so this method uh yeah it can be static so let's make it static and then the last thing here is going to be the board right so we need to come up with some Port uh we did we could either take the solution of one two three four or we could also say well let's generate a random Port so let's scroll down and let's go over here and let's say generate yeah let's generate a random port number so hit enter yeah that doesn't look too bad however uh perhaps we can actually look for a valid port number I mean I think this is valid but let's make sure so what we're going to do is actually change this sentence into generate a valid random port number okay all right and so yeah that should be a valid range I think we can definitely go down here even into that Port over there that's you but it still it is a valid range right so we are having to think too much we already have a function to generate a random port number so now we can just say Generate random Port okay and with that we have prepared our outgoing message with that in place what we can do now is to uh well we just have to send that message a back and in fact that line that's suggested there it actually matches what we need to do so just do tap and that's a way that you send a message back into a into any other microservice now one more thing that would like to do here is to add some other logging message here saying that yeah we're sending uh that I mean the game has been created right and here are the details for that game so we're going to be saying and by the way I don't like that underscore in the logger so I'm just going to rename this logger to that that should work better and then we're going to say logger DOT log information okay and so there's a suggestion in there yeah it doesn't look too bad although I want something else but let's let's start with that okay so let's start with that it says publishing accurate message for match what I actually want to say is game game ID create it at and then we want this to show the IP address and the port for the for this match and in fact yeah that's the suggestion that I'm getting right here so it's exactly what I want so IP address import after the game ID so let's put this in order so we should have the game ID first and then yeah let's remove this and then we want to have outgoing message should be IP address and then we want to have the port right there and so yeah with that we have implemented the core Logic for our a consumer and and we are able to consume this much waiting for game message and we're sending back a game create message now the next thing that we want to do is to register the they must Transit a host so that we can open a password communication with the broker rather than queue so to do that let's go back into our Explorer and we want to know I want to do is go into program.cs and this is the place where our worker service starts now usually you want to have this worker class that's actually down here and this is the one that's going to take care of executing that background operation that they want to perform but in this case since mass transit already offers kind of uh worker service by itself right we don't really need it to use this working class over here so let's just raw head and delete this worker we don't need it let's go back to program.cs and of course he's going to complain about it but it's fine because we don't need to use this anymore instead of this what we're going to do is just say Services dot oh yeah it is already so you're saying what we want to do so in fact this is the way that you add the mass transit middleware uh or Services into your application now this is one way so yeah let's go for that so for to do that we'll have to do a control Dot and say using mass transit right there and then much waiting for game consumer that makes sense so this is going to make sure so that my mass transit can actually go ahead and stand up a queue in Rapid and Q for the a match waiting for game message right and that's going to be owned by the Manchester consumer now the next line is the one that stands up the boss of communication now this is one way to do it but there are simpler ways to do to do this these days with mattress so I'm going to just remove this and what I'm going to so and they say here is uh yes that's kind of what I want to do so using rabbit and Q and then we don't actually need to specify that many things because we're going to be using the the all the defaults for Revenue in the local box and because of that what we want to do is just say config dot configure endpoints right and passing the context right so that's what you have to do in order to establish the boss of communication between your microservice and the A and the broker in this case Revenue queue all right so this is great and we should be pretty much ready to go ahead and run our game manager microservice but one thing that we don't have just yet is an actual driving queue message broker instance right so we need a place to stand up these queues that are going to be used by our mic services and so at this point what we want to do is just to stand up that instance in in our local box right and the best way to do that is by using the writing Q Docker container now it happens to be that I don't quite remember right now what is the exact syntax to run a Docker container I mean arriving container in my box and here's the point where I would usually go into the docker Hub page to try to figure out that syntax but luckily we do have now uh hand assistant that can help us with that and remember from the previous video we were using a charge BT to help us with a lot of things right last thing we asked it to do for us was to regenerate some tests and so now what we want to do is your will perhaps we can ask him and see well what is the right way to stand up arriving Q Docker container so let's go ahead and say how do I run a gravit mq Docker container all right so let's let's see let's see what we get all right so let's see what we got here so I'm going to scroll up a little bit and let's see what we got here so yeah I mean this is uh very close into the way that we want to run our Docker container the only thing that I would not use is a specific Docker tag latest because there's a better one that will allow us to actually have some sort of a UI in order so so that we can actually see those cues and see what's going on there right so but yeah we can start with this definitely so I'm going to go ahead and copy this code over there and let's go into our Visual Studio code instance over here and let's go ahead and open our terminal here and clean up this like that and then I'm going to paste this and let's fix this a little bit I'm going to do just two ticks first thing is I'm going to do dash dash RM just so that whenever we stop this container it is automatically removed from my list of running of my existing containers right so that will automatically remove it and then the other thing is like I said the usual tag that I like to use instead of latest is the one called three dash management that's the one that's going to offer us the UI the UI portal for arriving queue and well one more thing I don't like this my rabbit name let's also name this as just rabbit and queue right so we can easily identify what this container is about so I'll hit enter and then as I don't have the the docker image running in my box right right now it's just going to go ahead and pull it down into my box but at this point I should have it up and running so docker look at PS as you can see uh the image is up and running and since a wrapping queue is now running in my box what I can do is just go into this page over here I mean look at this Port Port 15672 so if you go into this page at localhost 15672 that's what you're going to find a your having queue management UI all right so I'm going to let's go into that location and what you want to use here is just guest guest at the default over there and then if we go into queues this is what we're going to see the different cues that are going to be used for our microservices to talk to each other right right now there's nothing that you can see it's all empty so now it's a good time to go ahead and start a microservice and see if it's going to stand up the required queue so now let's go into vs code and since we are in Game manner already let's go ahead and just say dot and run and as you can see it does say that the boss has has started at localhost right there and so if we now go into uh the traveling to portal we're going to see that indeed we do have our very first queue up and running right here okay so it's much waiting for game because it corresponds to the message that we defined right and so that view is ready to receive messages from the Matchmaker microservice right right now we don't have any messages but we are ready for that so now we have this the next thing that we want to do is to well modify our Matchmaker maker service so that it is able to publish messages and that game manager can consume and that so that it is also able to consume messages back from game manager right when the game is ready and do something about that right so let's go back into Visual Studio code over here and let's just stop this service for now let's close this and what we want to do is perhaps let's use a close all the tabs and now we're going to start working back in our Matchmaker microservice so I'm going to go ahead and once again I'm going to come here I'm going to switch context from game manager and perhaps this time let's choose all content projects let's see if that hopefully gives us intelligence across all of existing projects right now and the first thing that we want to do is to also enable mass transit for our Matchmaker microservice just like we did for game manager so let's go ahead and open our terminal and then let's actually create a brand new one for our other microservice so this is going to be Matchmaker and then we just want to say dot net add package Master Transit Dot rabbitmq okay so now Matchmaker can also talk to rivalingq but the other thing is that much America is going to also need access to the contracts right so to do is just say .net at reference and then into contracts at contracts at csfrog so what is in place it is time to go ahead and start publishing messages from our Matchmaker right so let's collapse this and what we want to do is just go into services and into matcheservice.cs let's collapse that and if you remember this this class only has just one method match async we didn't want in charge of doing the whole matching logic right and the change that we want to introduce here is that we want to publish a message anytime a match reaches the match found State when we have both players in a match so that would happen if you scroll down that will actually happen in this section right here here's where we a complete a match we switch it to match found and that's the time we're going to go ahead and publish a message that the game manager service it can can use right so to do that first thing we're going to need is an instance of an object a called the boss this is offered Again by by mass transit the mass transit library and so what we want to do then is just declare so let's declare here private read-only and this is going to be ibus right let's let's just name it bus so I'll do control dot so that we can import master and sit and now let's just click here I'll do a control dot so that we can generate a Constructor is the last option here in the reconstruction with mesh service eyebus all right so notice that vs code now has a way to a receive ibos B Advanced injection and it's been assigned to the to the bus a variable writer and now we have access to the bus we can do something super simple here which is uh that in this section here where just before returning what we can do is just this we can say oh wait bus dot publish and then we're going to say new and it's going to be match waiting uh for a game right there and then we want to provide the ID of the match right so in this case it's going to be open Match that ID all right and so all I have to do control that here so that we can use the contracts all right so that will be all that we need to do to publish that message but also now that we are using this a await here we can go we can simplify a little bit the way that we return things so there's no need to do a weight test from result we can just return the open Match like that and down here also put this here we can also say return new match all right so yeah that's pretty much what we have to do to put these messages from here now since we made a change here in match service so that it receives ibus we likely have to go back and fix our unit test so that they account for the fact that there's a new dependency introduced into this match service so let me go ahead and open an Explorer here and if we go into our unit tests over here and into much service tests we're going to notice that there is a bit of a problem right so because much service it requires an iPhone instance now the good thing is that what we need here represents just an interface and that's something that we can easily mock all we have to do is just make sure that the the logic in the master service a class itself is valid so because of that we're going to be mocking that that dependency so and to do that we need to introduce a brand new nugget package so let me open my terminal here and let's switch briefly from Matchmaker and into Matchmaker that really tests and we're going to be adding a Brand New Bag no nougat package .net add package and the package is named mock right it's a super popular package you can use to mock the dependencies of your tests okay so let me now close this and now all we have to do in order to introduce a mock instance of the ibos is just say here new uh and notice this once again copilot is helping us a lot here so I don't have to remember syntax all we have to do is just to tap and now we have a proper Mark of ibos that's going to be injected into a match service I just have to do a control dot here to use mock like that temporarily Ctrl dot here in ibos to use mass transit and just like with this over here we can do the same thing with the other tests over here and over there all right so things should be working just well for our unit tests let's verify that they they are still working properly so I'll just click on run all test right here let's see what we get yep we got three out of three passing so everything is looking great for our unit tests and what we want to do now is to start working in the logic to consume those messages that are convenient coming back from the game manager microservice right when a game is ready and so to do that let's go ahead and close this and back into our Explorer View and what we want to do now is to First we're going to define a couple of new properties into our gain match entity so if you go scroll up here and I'll collapse this and I'll just close that and let's go into entities and game match we're going to be needing a couple of new properties for both the IP address and the port that we're going to be returning into our client right because those are present in the message that comes from game manager into Matchmaker but Matchmaker also needs that information in order to store it as part of the game match so just right here we're going to be adding a couple of properties the first one is going to be it's going to be a string okay and this is going to be the IP address now the IP address uh is not going to be represent in the entity when the game match is created so that's why we're going to be making it new level by adding a question mark right there and then the next property is going to be the port and as you can see copilot is already generating the when we hit for the portal just to tap and we are done with the port now the next thing that we're going to need is something in match service in the our match service so that we are able to update an existing match with new information like the port and the ipis and all that stuff so we're going to do is just scroll down and we only have the match async method right now we're going to be adding a brand new method down here okay and then once again I mean copilot is suggesting something in there but we want to actually ask it to generate something else for us right once again a heyco pilot add a method to update a match IP address port and state right you can always just write a comment like that hit enter and let's see what copilot a can do for us and as you can see right there we have a method that looks pretty pretty good I'll just do tab here and which is able to indeed update an existing match so first thing is that yeah we need a match ID we need an IP address as a string we need a port and we also need the state right so that it didn't add it so let's go ahead and say match State match state or just say State okay so first thing we do is we try to find a match and if we and if we are able to find it we will go ahead and do something about it right however what I'd like to do is just to invert this logic a little bit so we're going to say instead of that is just say well if match is null yeah let's go ahead and throw draw an exception right because uh that will be kind of uh an invalid situation uh let's actually say argument exception right so argument exception match with ID majority is not found right and the actual argument is going to be just name of machidi yeah and compiler keeps suggesting things for me to do and so if we get into the next line it means that we are good and so I'm going to just remove those lines this last line is not needed to remove it and at this point all we have to do is just assign the IP address assign the board and then assign the status it doesn't have to be much data game read it should be the state that came from the parameter all right and that's it also we don't need this method to be async because there's no async situation going on here so let's remove async task and just turn this into a void method and there's no need for the async word over there okay so just like that we have a random method to update an existing match now we should also include this method into our interface right so we can use it somewhere else so I'm just going to copy the signature of this method and let's go into our image service and then I'll just paste it right there so with that available now we are ready to implement our game created consumer remember that anytime we need to consume a message we Implement a consumer that's the way that we do it in mass transit so open up my Explorer over here I'm going to create a brand new folder called consumers under Matchmaker and then I'm going to just create we have to create a consumer for the the right message for the guide contract so let's go into contracts briefly remember that the message is named game created so I'm going to copy the name of the message and then under consumers in Matchmaker I'll say new c-sharp class is going to be getting created consumer all right and here we are collapses let's make it so that this is fast code name space remove extra stuff and if you remember all we have to do to implement a consumer is just to implement one interface that has just one method so an interface that we have to implement is a iconsumer and uh yeah as you can see it is already suggesting the interface to implement and so yeah we'll go with that although we don't want to do contracts that game created let's just do a game create it and then let's import the missing stuff so using mass transit and using contracts all right and so then I'll do a control dot once again so that it goes ahead just implements interface for me all right and so yeah this consumer is going to be fairly simple all it has to do is just to get a message update the match with an information and that's it that's pretty much it um but one more thing that we want to introduce here is well two things first we want an instance of that a imatch service that the one that knows how to manage all those matches and so I'm going to declare here private with only imatch service okay that we're going to name just match service and I'll have to do control dot to use match service a Matchmaker that Services right we also want to include this into the constructors I'll do a control Dot and this has to be injected the very last option here okay Constructor right there and now we have much service being injected and assigned into the match service a variable right there okay let's scroll down a little bit and the next thing that we want to have here is just an instance of a logger so that we can just print some messages saying that yeah we consumed the message and we're good to go right so for that all we have to do is just a logger what I'm going to do here is just say hey please add a logger that's all so just by doing that I'll hit Tab and now we have our brand new logger defined right there of course we want to also introduce this one into our Constructor so I'll say add parameters to again create consumer image service right there and now our logger is injected and is assigned a writer and with those two in place we can go ahead and actually Implement our consume methods so scroll down into consume and what we're going to do first is to just receive the message solve our message equals context message then we want to add a some sort of login information okay and this doesn't look too bad so I'm going to accept a suggestion from copilot uh we're going to modify this a little bit what I want to say is just um again created message received for match and then song match ID right that's all we want to show for now yeah and likely the best way to show this is not like this but not with interpolation but by using uh the parameters like this so like that and we will say message dot match ID and then here we'll just say match ID right that's the right way to use your logger after that it is a time to actually go ahead and update our match and uh well as you can see copilot already infer that that's what we want to do so I'll just do tap and then yeah so this method is going to go ahead and update the match with the match ID the IP address the port and finally the match state so I'll have to do control dot match state so that we can use matchmaker.entities and uh yeah at that time at that point we are pretty much done but let's add some other logging message here so logger DOT log information and what we want to do here is just a state what is going to be the IP address and Port of the of the match at this point so let's say game ready or match yeah so we'll accept that with for much much ID but let's do more let's say at and then likely we want to show here is the IP address right and the and the port right and perhaps we can do this in uh next in the next line so we can see better all right so I'll do this match ID and then yeah the IP address and then the port finally since we're not doing any kind of async task over here what we have to do is just return task that completed task awesome so the consumer is ready and now we want to do is just register the mass transit services on Startup so that you can actually open the boss of communication with the broker so let's go now into our Let's uh let's collapse all of this and let's go into Matchmaker program.cs over here and remember that here's where we right now all we are doing is just registering they get endpoint right and with the Singleton and that's where we can go ahead and also do the mass transit stuff so we can do is just say um yeah I mean very similar to what what is offering us to do so let's just do Tab and now we have a registration of us Transit I'll do a control dot here using mass transit and it is registered already adding the consumer the revenue that we created control dot using Matchmaker that consumers right there and then just like we did before we know that there's a simplified way to do this so I'm going to use say x dot using rabbit and Q okay and then I'll accept the suggestion and I'll modify this slightly so that it is even simpler by saying that configure endpoints with the context yeah so notice how compiler just keeps assisting us and helping us get this done super super fast and so yeah that's pretty much it we should be ready to start sending and receiving messages across the two microservices so to put this into action what I'm going to do is just do Ctrl J to open my terminal right here and at this point we do have two terminals so what I want to do is to split my terminal here so I'm going to grab my terminal that's in the top into this section over here so we can see both of them and I'll do I'll switch to the right directory Matchmaker so we have much maker on the left side game manner on the right side so and I'll just dot net run in Matchmaker and I'll do dot net run in-game manager right so now we have both microservices running both are listening to rabbiting queue let's look at the status of gravity right now over here and as you can see now we have a brand new brand new queue which is the one for getting created over there and besides the other one that we already have so that means that both microservices are ready with their cues at least ready to listen for the messages right so all we have to do now is just I'll just do Ctrl P let's open our matchmaker.http file this one over there and let's try to make a match so let's go with player one first I'll hit send request okay and so collapses for a moment as you can see player one is there we are waiting for an opponent awesome back to terminal over here and then let's do player two and let's see what happens right so let's hit send requests and notice on the right side we have a the game man that has received the message right for match one and then a right now yeah at this point it is pretty much done it took about five seconds and then as you can see the game with this good it got created in this IP address 4479 something something import 32630 for match one and also on the left side we can see that Matchmaker was able to receive back that information about that brand new match that got created and so yeah so that means that we have a success right we are we were able to implement our microservices application and it is working all as expected okay and so yeah and if you see Revenue right now likely is going to show pretty much empty I don't know if this is going to show us a graph let me look at much waiting for game so yeah as you can see there's a little bit spike in there that's one message that went across this one queue and if you look at the other queue you can create it you're going to see that yeah there was something else going on there but it was uh super fast so the messages are gone but there is no synchronous communication between microservices which is the way that you want to call this stuff right so fully decoupled now uh that's pretty much what we wanted to do but I mean we could end there or push this a little bit more so because I mean it's nice to have this API but how about creating a little bit of a client that our players can use to to get matched right so let me go ahead and just stop both of my servers and close this and close that let me actually close everything over here and what we're going to do is just stand up a console application and then we're going to be asking chat GPT to see if if it can generate that console application code for us right so let me open my Explorer over here let me open up my Terminals and Anthony let me spin up brand new terminal over here and we're just going to create a simple console application right so.net new console and let's name it just client okay so we have our brand new client on the left side as you can see and what I'll do just to get some intelligence before I'll click on the bottom left to switch into the client project right now okay and collapse this and then we'll go into program.cs and so as you can see that we have pretty much nothing here other than a hello world uh but we want to have here is some sort of simple logic to be able to ask for the player name and then pull the RM point until we get a successful match so let's see if GPT can actually help us here right and so one thing that I like to do is first just make sure what is going to be the port of our Matchmaker and so I'm going to open up my terminals over here let's see matchmaker is on the left side and so if we can see yeah it's right here this is the port 5290 that's where matchmaker is running right now so I'll copy that and then a class this and now let's go back into chargept and so let's see um implement um see Sharp console that asks for a player invokes the uh well it would be that endpoint and we know that we have to specify here uh match and then the player right player endpoint and then pause the end point until the match state is game ready all right so that's a requirement let's see what we get from chargpt okay so let's see what we got here so here's a little console application so it is using a client to invoke a device address as the address of our Matchmaker microservice it will ask for the player name and then it will go ahead and invoke the right endpoint yeah that looks correct play your name and then let's see he's saying well until match state is not game release which likely is going to be filled later on uh yeah that's filled down here right it's going to say okay so I'm going to wait for one second and then it will go ahead and invoke once again so it's polling and then it will go ahead and read and deserialize again match and then uh yeah it looks looks pretty well pretty good the only thing that is not doing is just a generating the call 4K match it is assuming that we have it already available which we do but it's available in a different product right so one thing we're going to do before I mean this looks this looks good uh pretty good uh but uh first thing we're going to do is just make our client aware of the game match class right so we're going to Matchmaker and entities we have our game match a object over here and of course there's a few ways to make decline aware of this class here but we're going to keep things very simple right now and so I'll just copy this and I'll paste it on the client uh usually what you want to do here is just expose a game match as a dto in the microservice and then put that into a nougat package and then have the client consume a new patch right but we're not going to be doing that right now let's just do it this simpler way and then our next page here is not going to be Matchmaker that entities is just going to be the client right and so yeah now we have an instance of the gang match class I mean the class we have it in Decline we can use it now and so in the program let's go ahead and bring in that logic that we had over here and this is kind of the old style of the console app so which is fine really what we need is what's inside this main method over here let's copy this and let's put that over here okay fix it just like that okay and so let's see uh what we have to fix here so this looks good all good um well it looks like it's trying to use I think this class is actually coming from Newton software Json Newtons after Json yeah but we don't really need to do this I mean there's a simpler way to do this what we can do is just say response that content dot read from Json async and then here we want to provide the actual type of the object that we want to deserialize into let me actually do control dot here so that we can import system.net.http.json and then here we want to specify game match all right like that and so I'll do control dot here also to use a declining space and this is going to be our match right so I'll just do match here and then we don't need this other line and then uh yeah so let's stream a match date is a string right now but what we have here is actually the the enum so let's switch this from matchstate from avar string and into the right type which is going to be game match state actually is just yeah much State like that so much State and let's initialize it with a match state that um waiting for upon it right that should be an initial State and then while matchstate is not match State DOT yep game ready then we go ahead and do this logic over here right and lastly over here we're going to go ahead and just capture the match State and then the actual property here is not just a state it is actually match state all right now this is saying that potentially read from Json async is going to return a new level value here but that should not really be the case because we know that we should always be receiving a a match a match regardless of how many times we invoke it right so let's go ahead and do the following over here let's put some parentheses over here and like that and that yeah that's kind of the null for giving operator saying hey I don't care what you're warning me you're about a match will always have an instance right so you can always just realize that you can you will be able to reach the batch state and I think that's that's all we need to do at least for that very first implementation we'll see if we can improve this in a moment but let's see how this goes right so let's try it out and let's coin let's go ahead and open our Terminals and we are going to be cleaning our terminals let's dotnet run on both sides all right and then let's go into our other terminal where we have our client right so luckily for this one we also want to have a couple of terminals so let's go ahead and switch into client and then let's put the other one on the right side okay just so that we can see if these clients can actually work together so let's go ahead and say uh run on both sides all right so yeah it is asking for the player name so let's say again player one and then let's say layer 2 on the right side okay and if things are working properly we should eventually get a match ready uh yeah indeed match is ready to start right and we can see that things actually happened right here on the back end we can see that it yeah things actually happen uh but this yeah this client is not really useful I mean always saying that I mean enter the player name and eventually it just says much rate to start so perhaps we can show a little bit of more information here uh so that this this looks better so let's get back into the client code here and see let's see how we can improve this a little bit okay so let's uh let's go ahead and look at this code and I think at this point here right just after assigning the match today we can do something better over here so we're going to be doing a switch statement right so that's interest payment that's going to switch on Match State yeah right there on Match state and let's see what will be the first Clause here so we're going to say case pilot is suggesting a bunch of things uh yeah I mean I think the first one makes more sense the other ones don't make too much sense uh so I'll just go ahead and start typing here match State DOT waiting for opponent okay and so yeah yeah this makes sense right if much days waitable opponent let's go ahead and just as display a message that says waiting for opponent right now the next case is going to be match state that match found and then yeah it is suggesting that we say well much fund but we actually want to show a little bit of more information let's say yeah much found and your opponent is and then yeah it is suggesting that we say match opponing name but actually it is match that player two so let's do match player two write that uh well actually I mean we don't know if is is going to be player one or player 2 right it depends on what slot is assigned for a current player right so let's actually improve this even more by trying to calculate the current uh the correct opponent so let's say opponent okay is it um yeah I mean I think we got it uh let's do this I'll just take tap let's see how this looks like so if the match is player one equals our player name the current player it means that the opponent is going to be player two otherwise the opponent is going to be player one so somehow copilot figured out the whole thing by itself so that's pretty awesome and so now your opponent is opponent that's all it is and let's go for the last case over here so let's keep going down so yeah case match today that getting ready uh yeah this is a good suggestion to start so much is ready to start so let's say match is ready to start at and then we want to provide the we're going to say instead of that we're going to say match that IP address right and it is suggesting that we also want to show the board because yeah of course if you want to show the IP address we also want to show the port right ready to start at IP address and Port something something let's just append a little bit of more info here we're going to say Dot enjoy all right enjoy the game enjoy yep getting ready so yeah this should be it I think we don't need the last line that should do it and yeah that's it so let's try this out I'll do Ctrl J and I think our server is still up and running right so I'll do I'll just clean the screen so let's pick up another set of players so let's go with player three oh actually dotnet run and.net around over here and then let's do player three and then let's do player four so yeah so waiting for opponent match found your opponent is player four uh your opponent is player three and beautiful both opponents are ready to play as you can see player 3 is ready to play with player four this is the correct a IP address as it is showing on the right side for our other player yeah that means that success right sir console is working as expect I hope that was useful and if you'd like to know more about microservicesn.net please check out my free Workshop linked below this video where I go over the steps to move from Mod leads to microservices how to get and deploy to the cloud and even how to stand up a cicd pipeline in just a few steps and don't forget to subscribe to the channel so that you're the first to know whenever I publish new videos thanks for watching and I'll see you next time
Info
Channel: Julio Casal
Views: 2,174
Rating: undefined out of 5
Keywords:
Id: _5OrL7AOrmM
Channel Id: undefined
Length: 56min 47sec (3407 seconds)
Published: Tue Mar 07 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.