NestJS Microservices gRPC

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to take a look at how we can create a nestjs microservice project using [Music] grpc so first things first let's go ahead and install nestjs or update the CLI so you can run an mpm install to in install this and we can install this globally with the- G flag at njs CLI now if you already have it they'll update it for you or if you don't have it they'll install the njs CLI for you if you don't have it on your computer already and you can go ahead and you can verify that you have it by running the n-v to check which version of njs you have so what we're going to do is we're going to create a new project and I'm in my njs folder and I got a few other folders here so I'm just going to create another one and we can use the N CLI to generate a oh sorry we can use the nli to create a new project and I'm going to call this API because we're going to create a microservices based API and I'm just going to go ahead and use npm um and that's going to install and while that's installing I'll just talk through what we're doing here today so basically we're creating an API Gateway which is going to be a restful or well it's going to have HTTP access so a client can make a HTTP request as you usually do and then we're going to offload that to one of our micr services and we're going to have grpc as the transport layer now if you haven't already seen my 15minute video on what microservices are I would highly suggest looking at that because it goes into detail it explains different types of transports such as rabbit mq um G PC and when to choose which type of micros service so that's a really good video to watch just to if you've never seen or heard of micros Services before um but if you have um then you sort of know that grpc it acts slightly differently although similar to the other types of microservices uh which is why I wanted to do a special video on this one I have done a playlist on making a Facebook Messenger um mic service based application that you can check out um using rabbit mq and the other ones sort of behave similarly to that but grpc is a little bit different and we're going to go ahead and see why that is the case but I have created this API project here so this should look quite familiar this is just a basic njs folder structure and of course we're assuming that you can create a cred application in Nest but if you haven't I got plenty of videos on the channel you can check out for that so with that out of the way basically what we want to do here is we want to turn this um into a mono repo because we got so far we've just got one project um but we want to use the microservices architecture so we want to have an API Gateway and then we want to have a to-do micros service so we can do this and I've just got the nli open just to show you that when I run this command here n g generate app to do this is actually going to change this into a mono repo so what we're building today is just going to be quite simple it's just going to be an API Gateway that connects to a micr service uh which is just going to be a basic to-do um micr service there but we're going to see exactly how we can hook this all up together oh I forgot to actually CD into the project so let's just go ahead and do that usually I'm in the project uh but in this case I am not in the project so I'm going to just delete this apps here um because I'm going to need a CD into API first and then I'm going to run this generate app to do and then now you can see here if I just close this terminal you can see that it had a quite simple uh CLI here um but it got extended so that this mono repo is true meaning that um we're building multiple uh projects in the same area so when you build it you can deploy them separately um but we're able to write the code in this one spot and we can see that it converted our API into its own folder in apps which is going to be our API Gateway and then we got the to-do micro service there now just before we get started we're going to need to install a couple of dependencies so let's go ahead and do that so I'm just going to run mpm install and if you've seen my microservices video you know that you're definitely going to need njs microservices um although you don't have to watch that video to follow along with this one um but I go into more detail in that video on rabbit mq so in this video we're going to focus on just wiring everything up and all the theory behind microservices have been covered in previous videos so we're going to install at njs microservices we're also going to want to get grpc so we can get at grpc and then grpc can be used with many different languages so we're going to need to to get the JavaScript version of grpc and then we're going to want to get um grpc Das protol loader so we're going to go into protocol buffers um and Proto files in just a moment so we'll explain that um and then we're also going to get ts- prototo and if you go to the nest doc M mentation you'll find these um installation steps or dependencies that you need uh to be there so I must have spelled something wrong grpc oh that's a slash Proto loader here okay so that's installing and let's just open the package Jason to confirm that it is doing what we want it to do um so yeah we've got these nestjs dependencies here so basically we want to use um the Proto extension vs code because we're going to create a Proto file so if you come to your extensions and you type in proto um you don't need this um but I got this one here and this just allows you to have syn text highlight in your Proto file so let's go ahead and create a Proto file and then we're going to see where uh these Proto related dependencies come into play in just a second so firstly in our next to our apps folder let's go ahead and create a Proto folder and I'm going to actually just create a file here in the Proto folder called Todo Proto and the Proto file is basically where you declare um you know your methods that you're going to call because we're in a microservices application and if someone makes an a request to the API Gateway that's going to get forwarded to a different server our to do micr service and grpc is kind of cool because when you define the signatures you get that type safety between the project and in addition to that it can also work across multiple languages so you can have different micro Services uh in different um languages and that's exactly what Google sort of solved although we're going to have to focus in njs of course but you could imagine a large company like Google Google's implementation of remote procedure core which means calling functions from your um your uh projects uh as if they were local even though they run on different servers and that's achieved by having this Proto file which has the type safety uh definitions and it can work across the multiple languages that you you know because you can imagine Google they would have their you know different you know they might have something done in C++ and they might have something in some other language uh and then they want to communicate all those together and grpc was Google's way of solving that issue um so this is the main difference between the other microservices and grpc is you've seen the well I don't know if you have seen it or not but I have a video playlist on rabbit mq which acts similar to other microservices and that effectively um you know you expose the service um and then you call it and you sort of import it and then call it whereas that's sort of happening in this case but what how it works in GPC is you sort of Define the signature um and then you're sort of rather than you know calling it and unwrapping it and dealing with it you can just call it directly so effect it sort of is doing the same thing but it sort of feels like you're calling it more locally and you you'll see what I mean uh as we get into it uh but the first thing to note about this Proto file is you need to just declare what sort of the syntax is and I think the latest version or the docs that I've been following they just say Proto three at the top uh and you can see we got that syntax highlighting there that's sort of similar to like um I don't know any yaml files or maybe Docker files where you have the particular version at the top there and this is version three so there's probably early versions that don't have extra uh you know things that you can declare there and then we go ahead and we Define the package and this sort of reminds me of java in a little bit where you just sort of say what the file is so this section is the to-do section so you just so got to declare that so then you can refer to it from other places as we need it and then basically we want to have what's known as a service and we're going to call this the to-do service and we can have this syntax here where it has RPC for remote procedure core meaning we're calling a function that's in a different server one of our micro services so from the get API Gateway we can call this remote procedure call so we would want we're just going to start off basic and we're going to have two we want to be able to post a to-do um and I'll come back to the type in a moment and then I'm going to want to get a to-do or get all of the to-dos let's say so basically because we're in a a Proto file not a typescript file we need to have the particular type so when we get something we don't really need anything in the as a parameter but we're expecting to return a bunch of to-dos so this is how we can implement the type safety part and you this is a requirement to use grpc um and it turns out that if you have no arguments you actually just need something that says empty and we're going to Define these in a moment um and then the post to-do you can imagine the post to-do is going to be different to the to-do because the post to-do you don't need the ID because you haven't generated that yet um so you'll have like a post too dto so I'm going to Define these of course but I'm just going to uh write the signature out here and then basically this file um is going to be bundled with all of our um build projects so if we have two microservices go into each of those two microservices and that way we'll know how to call things and it has the type safety and then it uses the method from the other or the cord service in this case the to-do service so we can just go ahead and we can return that and then there's just this empty code bracket here so let's go ahead and just Define this and you define it with the keyword message and this is just basically an interface or uh a type now I've got this extension so you can see these like Intelligence coming up so I'm just going to make the ID int32 and again if you're familiar with typescript you might be familiar with uh numbers but you got to remember this isn't just for typescript yes we're using a JavaScript layer over the top but this um can work in many different languages and most languages actually do have integers and different types there um so we got this we need to take into consideration that um so when we write out this Proto like uh interface like thing uh we just need to have that awareness so it's it's pretty similar though like we have a string so I'm just going to have a description uh equals to two and a bullan which is bull uh and I'm just going to say is done equal to three now you might be wondering about this one two three thing um basically I'm not assigning this to a value what this is doing is is just saying what the order of the things are within here and um you could look into the documentation if you want to go ahead changing that but it doesn't really do anything at all um unless you have a specific need for that so we've got out to do I just think of this as an interface with a number as an ID a string as a description and a Boolean is done I don't worry about any of the low level details or anything like that because you know we're dealing with this level of abstraction we don't need to we simply just need to connect things together so basically we're just defining our signature for our method and we're going to Define our types and then we can p those from any micros service we want and have that type safety uh there for us so let's just do Tod do because todos in grpc basically what you want to do is you want to use this repeated keyword so if you need an array you use repeated and then you can use um the type there so we got the the type to do which we defined above so I'm going to call this todos and again I'm just going to equal this to one and then we also need the post too dto so this is going to be pretty much the same as this part here and then this will just be one and two here and then finally we need empty because by default um it doesn't recognize that no arguments is empty so we need to actually specify empty as a curly bracket and again you need to look at the grpc documentation um so I'm just going to take it at face value and just go ahead and just use that there and of course this is returns not return so we can save that so now the good part is we've installed um that TS loader extension which means because we got this Proto fob but with that extension um we can actually just go ahead and generate the corresponding typescript file and we're going to generate that right next to this Proto file in this to-do Proto file here in the Proto folder now I I looked at this I got this command from the njs documentation if you scroll down a bit on the documentation there's a reference to the TS loader which takes you to an external website um which gives you the command to generate the typescript file from this Proto file and the command is oh and one thing to note is if you're on a Mac you might need to run um Brew install Proto buff to be able to use it um so that's just one thing to keep in mind now I've already done that I'm on a Mac at the moment I don't think you need to do it on the window Windows though um but I'm not 100% sure mpm oh sorry we've already instored things we' got the extension um basically what we want to do is we want to generate the interfaces and controller and the client uh which is basically the method that we call from the API Gateway to call those um type signatures or methods that we just created or type sign all the methods we just created so the command and is protoc C D Dash plugin equals and then basically you need to look at your node modules because there's a binary file in there which has the executable uh to be able to do this so we installed the um package and of course that in put into the node modules and there's a binary file in there that allows us to execute uh the capabilities to convert this file so we have this D Das plugin equals to the node modules and this is I I thought this was a little strange doing it this way at first um but when I read the documentation I realized that um you know rather than calling a regular command like from the command line it's it's from the package in the executable files from the node modules that you installed so that's why we're doing this uh plugin like pointing to where that file is so again that's in the binary files and there's something called the Proto C- gen- tsor prototo file and and to be able to execute that basically we want to be able to well firstly that this is um not necessarily um specific to nestjs because nestjs has its own structure of how it does things and because Nest has its own structure of doing things um there's also this extra bit we need here uh to specify that the output will be in the NJ format so we we have this uh-- tsor Proto uncore out and the place that we're pointing towards or referencing um is going to be um just the folder that we're in because uh We've CD to the base of the folder here in API and basically so yeah that's that's where we're outputting it um well sorry that's not where we're outputting it that's where the base of our folder is and then we can use the nestjs layer of abstraction here so we have this tsor prodoor opt equals Nest J with a capital j s equal to true and you can just put this in the location relative to this uh folder that we're in so if we're in the API folder we want to put it next to this Proto file so we can just specify that by saying we want this to go to the Proto folder relative from the API folder and we want to use that Proto file there so then we put it there and hopefully I didn't make any mistakes but if we run this ah good it generated this file for us here so if we look at this here we can see that it basically created the interfaces from our um our Proto file here but in addition to that it also um it came up with some things like the package name which we need to refer to to when we um call some methods and we need to implement some of these uh interfaces here so we got this client um which is what we're going to call from the API Gateway we're going to make a request to the API Gateway and we're going to need to post the to-do and this has all the type safety and it connects everything up as long as you wi everything up properly um because we also got this to-do service controller so so not in the um client or the API Gateway but in the actual micro service we can also refer to these um methods and then they'll actually call it from that micros service itself and then we just got some grpc related things for this to-do service controller methods uh which just pretty much enforces these types here so that's all well and good but one thing to note is that when we build the project it won't in its current form actually reference this file and that won't be part of the build so what we need to do is we actually need to come back to our NES CI here and we just need to change a few things here for um the way the project uh bundles things together so for this API um Gateway what we need to do is we need to change the root the entry file and the compiler options and we needed the same thing for the other um you could create like a shared library and then reference it like that um and I have done that in the previous video um in the rabit mq microservice playlist but I just want to stick to the point and that's focusing on connecting njs and GPC so I'm just going to Simply bundle that file here into the builds of both of these API G way and to do projects so basically when you build a microservices project each of these applications within apps that will be its own file or folder with files and then you deploy those um separately or you know logical way you might use kubernetes or something like that or containers um but the different servers hence microservices so we need a way to be able to get this file into those and the easiest way to do that is if we just look at like one of the apps here um if we change the um so we got the entry file we got the root here as apps API if we change the root to um well actually we we need to leave the root the same but if we change the end the root Source route um to simp L just the folder that we're in I.E this whole API folder then it's going to bundle this Proto file with us and folder but that means relative to the source route uh we'll need to change entry file because it will no longer be main but we have this um Source folder and recall that this this root is apps API so from the apps API route uh we can just go ahead and just say Source SL main here and the reason we're doing that is because in these compiler options here we can optionally have some assets and that's just the files that we want to include and what we're going to do is now that we're in the API folder rather than nested a few folder structures in we can refer to that Proto folder and we want to get the to-do Proto you could do that but in a real project you'll probably have multiple Proto files so you can just use uh just star. prototo sort of regular expression thing and that will get you all the Proto files in that folder and then finally we can go ahead and we can use watch assets and there's a description there um where if you're running watch mode watching all non- typescript assets uh setting watch assets is a top level compile option uh basically um If You by default I think it's true but if you send it to FAL uh by default it's false but we want to set it to true meaning that if we ever decid to come back to one of those Proto files um you know and then we build our project you'll be watching for those changes you look at the date that the last change was done and if it exceeds the date that it was last done then it'll go ahead and update that for us and that's good because we need that to be updated uh to be able to um you know have the up to-date methods that we have there so you can see that this is pretty much all the same entry file Source rout and compile options uh the only difference is this says to do here so I'm just going to go ahead and copy and paste that and then just write uh too here so now we have our Nest CLI set up what I want to show you is actually just building these projects so just to show you what the output looks like so if I run mpm run start Dev to do I'll run that and then I'll just exit out of it and I run mpm run start Dev API so if I look at the disc folder in API we see that we got these two apps we got the API folder and the to-do folder and if we open those up you can see that it's got the source files for that but it's also got the to-do Proto that we've bundled it together with uh so that's important um because we're going to need to refer to that file there so we just need to remember that we're going to refer to this bundled output Tod do.pro file uh from our application but we'll see that in just a moment so I just wanted to show you that um of course we're going to need to hook up our microservices now and this is where like the main part of this comes in and this is actually quite familiar if you've um wired up any other microservices before in sjs but if not I'll show you how to do it anyway so basically what we want to do is we want to um well firstly we want to go to our micro service so the way we do this is we set up the micro service and then we come back to the API Gateway so then we can call that micr service from the API Gateway so just to show you um the AP these two folders are pretty much the same at the moment because we haven't really done anything um so if I open up the to-do the main TS file we got this listening on 3000 and that's true on the API Gateway as well and we want the API Gateway to listen on Port 3000 because we need a way to communicate to our server but the micr service we don't want to be able to communicate to directly at least not in this case now I did talk about hybrid microservices and basically the main reason you want something like that is if you had some web socket stuff uh because the websocket stuff over Services I'm not actually sure how that works there might be a way to do that and if there is let me know on the comments um but I found that hooking it up uh with websockets requires uh micro service oh sorry hybrid microservices meaning you expose a port so you be able to make a server client connection there but then you also uh have the um microservice related stuff encapsulated in micros Service uh communication Calles so using GPC in our case so we just want to we don't want to be able to access that in this case though so we can go ahead and we can just change uh what we're doing here so basically what we want to do is rather than creating something here we want to call the create micr service function so I won't create an API uh sorry I won't create a um HTTP uh based API or it's a micros service one meaning we can connect to it uh with the right options um within our network but um we can't connect to it directly so basically this is going to have a type here and this is going to be the micro service options type and this comes from this JS U micro Services here so that's why we installed that before we've seen where the loader related uh Proto related uh dependencies came from uh and then we're about to use the grpc stuff in just a second to hook up the typescript file that the Proto generated with our GPC and I should mention that you don't have to generate it but You' end up just typing all that stuff out um so just to clarify um in our Tod do Proto file here of course you could type this out and you could change the names of things and you know this sort of generates a bunch of things like promise observe or end the top maybe you just want to choose the one and all those sorts of things um but you know it's much easier to just go ahead and use the CLI it's much safer as well um but you know you might have some specific uh code quality requirements that you have uh or linking rules that you're adhering to or something like that um but yeah we want to use this microservices options typ which takes this to do module uh but then in addition to that we can actually specify uh an object with any of our options and if you've seen the micros service video before this we need to specify the transport so we're going to get the transport from sjs microservices and if you had a different micros service you this is where you would say okay well I want CFA mqt Nats redus rmq rabbit mq which we set up um TCP so pretty much all these Kafka mqtt Nats redus rmq they're all similar and they all connect in a similar way uh to my knowledge at least um so you can check out the Facebook Messenger series if you want to use that one of those any of those basically TCP you basically I I think you would I haven't used that um it's something you use for about an hour when you're learning about microservices but then you realize that all these types of um things build up upon TCP to make it a lot better make it more reliable and all this sort of stuff so un you go on super low level um I probably wouldn't worry about that or maybe it's your first time ever if you'll read through the njs documentation from microservices page one we'll start talk about um TCP and again I talked about TCP in my introduction 15 minute video of what microservices are so how network uh uh protocols and how all that stuff works but yeah grpc the main difference is that Proto file and having that type safety uh across projects and calling a file um a a method from uh here as if it was local opposed to calling external thing waiting for it um it coming back and then you sort of processing it you're acknowledging it and all those sorts of things um so I think it sort of is doing those things under the hood but you'll just see in a moment anyway that's a long- winded way to say we can choose grpc here and then this itself has its own options because we need some sort sort of way to be able to connect um from to our Proto file so and why is this erroring um hold on so let's just cut this out here oh it's just aring because we haven't used it I think so if I go ahead and use this here because of course we're going to need to listen to this that gets rid of some of the errors I'm not really sure maybe I need to fill out the options so okay so we get the Proto paath because we need a way to connect to our Proto file and this is where that I showed you the um where things are building because we're considering that this is built and then we see um so we're in this to-dos folder right now like we're considering that we built it we've got this source file here and then we got this to do um Proto so what what we need to do is we actually just need to join something here so we can go ahead and get that from path I might just put this up above our stuff so NJ stuff or javascrip col stuff and then our stuff so what we want to do is we just want to join uh we want to get the dur name so the current directory and then we want to actually um go back to the too dopro so we're in the source folder because this is the main file that we're building we got this main file that we're building we go back uh outside of this folder and then next to that we got this to-do Proto file here and then that package we referenced before uh you saw that that was in the top of uh um so that's that there that Todo there and we reference that here and we're creating the Proto file um so the package is going to be Tod do now you could use environment variables if you want and probably you should um but again um you should be familiar with nestjs before jumping into this uh and all those basic things I'm just assuming you can do and tidy up um this is more just to show you how to connect njar to grpc and then of course we listen that and then we call the bootstrap function so that's the to-do um creating the micro service there now we can go over to the to-do controller so the to-do controller what we're going to need to do is recall that when we created this to-do file it actually created this um to-do service controller for us and we're just going to go ahead and Implement that so then we can uh it'll Force us to use these methods here and that's good because we'll get the type safety for it uh and then everything lines up nicely so let's delete this get request because this isn't a HTTP based thing what we want to do is we want to actually um have a so I might change this firstly um to I might just delete this whole thing um to at um grpc method because this whole server isn't HTTP it's a micros service so rather than the gets and posts that you use for HTTP we need to way to tell it that it is um you know the uh micros service grpc communication so here you can say what the service is and this sort of automatically generates for us um so we got this um to-do service here so we can just put that there and then we need the name of the method so let's start with the post request um um and so that's going to be post Todo um let's see here let me verify that post too yes so this is just one of these uh methods here but it's actually the cap capitalized one um so I think it just takes that and then you use the capitalized version of the particular method that you're calling so we can say uh post to do now the interesting thing about this is if your method is going to be called the same thing or using the same you know to do or post to do here so for example we got post to do here this is what we'll call the method and then it's going to take the post too dto which we import which is going to be of the type uh post Todo tto so I'll make that a lower case p and since we're sort of enforcing this signature here we can give back the to-do here and basically the thing about this this is completely optional I'm just leaving it there to sort of spell out um what's happening behind the scenes um but if that that uh refers to the same sort of thing then you can admit that and similar you could actually remove the to-do Service as well we can't in this case but if you're calling this from the service um and it's the same now we're doing this from the to-do controller uh but if you're using this um and the name was the same you could admit that as well but we're not doing that so we have this to-do controller and then we can go ahead and call implements to implement the to-do service controller and then we will also and if you want you could just hover over this and get a quick fix to implement the interface um so that will generate this for us here although I want it to be a little simpler than that because I'm going to give back it to do so I'm just going to go ahead and remove these and even though technically in grpc you have this request here we don't need that in typescript uh so it's perfectly fine if you just go ahead and delete that uh and I might just cut this down below the post because we need to make a post before we can see anything uh not that it really matters um but if I go ahead oh and I think I cut it into the method if I move that here save that a pretty re saves it and then copy this grpc thing we're still calling it to-do ser but this time we're going to be calling the get todos with the capitalization so get todos is returning todos so now okay this is throwing an error but we want to basically we want to use the to-do service here to be able to hook that up so we're just going to say here return this. Todo service. poost Todo which isn't created yet but we'll create that in a moment and then we'll put the post Todo do dto in there so that's just eron because we haven't created that yet we don't need the observable um we don't need empty uh I think we can just get this [Music] from this path looks a little long um that's all right for now come back to that perhaps um so I'm just going to put the nest stuff together um so we need this post dto here oh no we got that um we need Tod do oh got that as well so oh it's just erroring because we haven't done something here so let's just go ahead and just copy this here and this is going to be get todos and it's going to have a EMP empty uh arguments there so basically that's all we need from the micro service all we've done is instead of using HTTP we've just used the grpc method here and we've just implemented the interface that just got generated by the Proto file that we you know created all our types and then we're going to offload the work to the to-do service here so if we open the to-do service basically what we want to do um we're not connecting to a database just yet but I'm might connect to Prisma uh in the next video so I've sort of prepared that in some of these other projects but and I'm actually really liking Prisma opposed to type or mainly for its migrations and um well that's one of the main things migrations and also um ability to view the database in the brand brows are quite easy so it's really really nice I like it um so I might make a video on that and dockerizing that in the next one but let me know if you want to see that also thinking of um doing some kubernetes locally uh and setting that up of course when you deploy it you usually deploy it to a cloud provider but getting it going locally would be a good thing and then perhaps another playlist on deployments in general so let me know what you think of that guys um would that be worth doing um so basically we want to do just a local thing here so I'm just going to have a private to-do of the type Todo and I'm going to import that so I'm going to command period and add that from Proto too and that's probably should be the import for the other one so I think by default it did that I was looking at that it looked a little strange uh does something like that work okay so that does so that's all well and good so oh to do was the other one we needed I knew there was something and probably you caught that much earlier than I did so basically we're going to have an array of these to-do objects and let's just give us one so we can just make a get request so we're going to have a of one a description of you know whatever first to do uh is done uh we'll set that to false and then we'll just go ahead and save that there so let's just go ahead and copy this here because it's got the same shape so we can just go ahead in this service here and we can just go ahead and have our we don't need the this part here annotation but we can just get the signature there so let's just go ahead and write these methods out here so basically let's say you know well firstly get todos that's simple we can just return the to-dos uh actually we need to return it like this so you might want to modify how you do things but when we Define the file uh we defined it like this so rather than returning the array to-dos uh we got this object with to-dos and that's often a good pattern Tod do regardless cuz um you know you're returning Json rather than the arrays there um so that tends to be a little better especially when you extend apis but you know that's just a coincident that was an intention or anything like that um so let's go ahead and get todos so basically what we would want to do here in post to-do is we simply just want to create a new to-do so let's just say to-do is of the type to-do which is going to be a to-do object and we'll just set the ID equal to this length um+ one so there one in it already length will be one the next it be which two if we add more they would just be Auto incrementing like that now description uh and of course you wouldn't want to do that for real um but we're just demonstrating here with some inmemory data uh so we don't have to hook up the database we'll do that in the next video if we do that um so the post to do dto uh we can get the description there and then we can get is done which is going to be specified by the person um you could make that FS by default as well uh but we're letting the person decide so the to-dos we can just simply push the to-do so it's all pretty basic stuff here and then you can just give back the one Todo that you've created there um so I probably need to import this as well so that's all there is to creating the micro service so just to recap what we've done on that part we've gone to our micros servers the main file and then we changed it from being HTTP uh Exposed on a port to being exposed with this micr service here we then have these methods which we're going to be able to call from the API Gateway and they're hooked up to the services file which has the logic which is just in memory manipulating an array and pushing an object to an array and getting those so what we need to do now is we need go back to the API Gateway and recall that this is listing on 3,000 so we don't need to make any changes to that main TS file but we do need to connect to it so to connect to our micro service we can come to this import section here in this array here and we're going to need to use the clients module which comes from this JS micro services so I'm just going to put that up there and we just need to register that so we need to register the all of our micro services so we've got one at the moment and then we've got this name of to-do again this probably should be an environment variable um but again this name uh that we've defined in the to-do here um so we got the package uh of to-do and then we got the service to-do service and then there's some other things in here as well so we got this to-do here now once again the transport is going to be transport. grpc and then the options so I can pretty much just copy this from here I'm going to copy these here and we're going to need join recall that that's from the output uh in the disc folder the source file is uh has the main JS file and then if you go back one that'll take you to the to-do Proto that we've bundled together using the uh n CLI Json file configurations so that's all you need to do there um so what we can do is we can go to the app controller and we can just call it so this is going to be have a get and post request so let's do our get requests so this is going to be get todos so get todos when we hit this API endpoint of/ localhost 3 uh Local Host 3000 um it's going to get things for us so we can say we want to get the to-dos and then let's have our post here as well and the post is going to be called post Todo you can call it whatever you want um but it's going to have a request body which we're going to need to import which also has this thing here and then we want to get the post to do dto type which is what this is or we want to get the object which refers to the post Tod do dto type and then we just want to return thisa service dopost too and then pass in that post to do dto so the last thing we need to do is we just need to open up the app service and call that and then I'm going to show how that all works we're going to f it up make sure it all works and then I'm just going to do a quick one minute recap of everything just in case this has been too much so we got this app service here so what we want to do is we want to have a Constructor and in the Constructor we want to use inject and the reason we want to use inject is because we want to be able to get the um the grpc client so we can have this client grpc which is going to be able to type client grpc from this Js microservices and we're going to need inject as well um and that's because we're going to need to do something a little different to regular injection although this is kind of not that weird anyway but basically what we want to do is we want to um like so basically what we're doing from our API Gateway we want to be able to call methods from our to-do service so basically when we created this file we had this um to-do service client so we're on the client or the API Gateway and we want to request methods from here from the API Gateway but they're actually calling the methods from the to-do uh application and this is all done with type safety and everything and they all connect up and you know we've defined it in the transport in here uh so it connects and stuff like that applications um but we need a way to be able to use that client uh and since it's defined in the other project technically we need to um like we have access to the Proto file but it's technically behind the scenes is calling it from the other server um so it it feels like we're just calling it from this API Gateway and it feels like it's just on um but it's actually on other server so what we do here is we just say um I don't know why it keeps importing from back here but um we want to be able to get the to-do service client which got Auto generated for us um which is going to be of that type there and then we calling this inject method here uh so we can have that service accessed in this API Gateway uh through the microservices protocol so we have access to that uh client grpc here um and we need to tell it what we're referencing so we know that this is too and basically how this actually works or how it's going to work here is we want to have uh on module in it so when this module gets created basically we just want to set this dot to do service client equal to this do client uh grpc which has this get service method on it and then this is where we put our service name so we can say Tod doore servicecore name that'll got autogenerated for us and then if we want we could just put the type in here as well uh which is going to be the to-do service client here so we can go ahead and we can save that and this is going to give us access well it's going to make us feel like we can call methods from here as if they're local so for example post too we know that that's going to take the post too dto which is of the type post to do dto and then we can simply return the method that we created so we got the return this. too service client. post too we pass in the post Todo and it sort of just feels like we're just calling it from here so we're in the API Gateway we're just sort of calling this um but it's coming from the other micro service and that's sort of the key difference between grpc and other micros Services is uh like if you with rabit mq like behind the scenes technically yes it is doing a similar thing um but um you know it's sort of feels like when you do it here in rabbit mq or something another micros service sort of feels like you're calling it from another server you know you acknowledge it you received it and then all of those sorts of things whereas here you just automatically get that type safety between the projects uh it can work across different languages by implementing the Proto file and I think it also um is faster um because I think it actually um GPC I think the way it uh like CU when you send requests to different servers in microservices one of the drawbacks is you got this Json which you need to serialize and deserialize um but I think GPC does something clever which increases the speed of that so it's pretty good actually pretty good GPC and uh there's a reason why it's popular which is why I want to do this video on it just because it's a little different to others but it's also pretty cool but yeah so we got this Tod do Service uh client here and then we can have get todos and this is just simply going to be returning uh this dot to do service client. get todos and recall that we had an empty thing that we're passing here so we'll just pass an empty uh object there like that and with that I need to implement that I might need to implement that oh is that lower case okay I think it's like that then and then in the app controller here let's just get it from the Proto so if I haven't stuffed up any of the um you know file Imports or anything like that uh I should be able to everything should just work so I'm just wondering why this part here doesn't work oh this is string for some reason oh they probably said hello before I'm just going to delete that there all right so let me just go ahead and run things so let me just CD into API and let's run a mpm run start Dev to do here okay no errors found that's good and then let's run mpm run start Dev API here okay so now let's go open Postman up so Postman all right so I did my my 3,000 that worked I got a post request here description false here so let me do that now I rerun this and then that all works well and good so we've successfully hooked up our njs micro Services uh with grpc as the transport um let's just do a quick recap of everything that we've done here so we created a new micr service mono repo with API Gateway and a to-do micros service we created this Proto file here which is where we Define the actual methods that we're going to call across the different micr Services then we run a command which generated this to-do dots file and that's all of our interfaces and things we're going to implement and then you know we did bundle that together by adjust justing the nli uh configuration then we created this nestjs micro service and we just sort of hooked it up as a micros service here and then you know we have this GPC annotation and referred to it uh where we actually got the to-dos which was just some in memory stuff so I'm thinking in the next video I might use Prisma and have a look at that and how that works with micro services and also use Docker to set those up and how it all works together um and then we got the API Gateway so that's just a regular HTTP server uh where we sort of just calling the app service so that's pretty similar stuff and the app service it just injects the client GPC to be a to make the calls as if they're local so so thanks so much for watching this video and I'll see you on the next one cheers
Info
Channel: Jon Peppinck
Views: 6,381
Rating: undefined out of 5
Keywords: nestjs microservices, nestjs, grpc, nest js microservices, nestjs graphql, nestjs grpc, nestjs tutorial, graphql microservices, nestjs logger, microservices, grpc microservice, nestjs vs express, kafka microservices, nestjs typeorm, nestjs authentication, graphql nestjs, nestjs nodejs, nestjs mongodb, nestjs redis, nestjs services, nestjs crash course, nestjs microservices tutorial, nestjs postgresql, nestjs mongoose, nestjs typeorm postgres
Id: WL0sPFXuF9k
Channel Id: undefined
Length: 66min 0sec (3960 seconds)
Published: Sun Oct 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.