Nest.js Microservices Tutorial in 20 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there today we're going to take a look at nest js micro services we're going to set up a sample application that includes an api gateway that our client can talk to and then we're going to connect two separate microservices to this gateway so we're going to need three separate nest js projects for this so in your command line in whichever folder that you'd like to work in make sure you run nest new and then we'll start with our api gateway so this will be a sample backend that's what we'll call it and i will include the link to the github repo for this project in the description as always as well as a link to the microservices doc so once that's done we're gonna do nest new and then now we're gonna include our first microservices which we'll call sample communication and this microservice will represent some sort of communications based service that would email or text or really any form of communication that we want to handle to our users and the last project we'll create nest new sample analytics so this will be a micro service that handles analytics for our application so once we finish initializing our projects i've opened the sample back and up in vs code and i'm going to actually go to file and then add folder to workspace and then i'm going to go ahead and add the sample analytics and then i'll do the same thing for the sample communication so now all three of our projects are in one vs code project and then we can save this workspace so first off let's go ahead and set up our analytics service to connect as a microservice so to start off let's go ahead and set up our sample communication project as a micro service so our first thing we need to do is open up the terminal for sample communication and make sure we add yarn add at nasjas micro services now an sjs offers us a number of different transports that we can use to connect our microservices by default the transports tcp which is what we're going to use so for this microservice we're not going to be listening uh for any http connections we're not going to expose it to the outside world and the other microservice i'll show you how we can do both but for now we're only going to want to set up a tcp microservice that will be listening for events and messages from other services so we'll create an app here and then we'll call await nest factory dot create micro service we're going to pass in micro service options here and now we need to pass in the app module as we normally would and then a set of options in this case we're going to specify the transport that we're going to use and here you can see the list of different transports that are accepted so we're going to use tcp for this example and then lastly we need to call app.listen and that's all we need to do to set this microservice up so now that we've connected our sample communication project let's go back up to our sample backend which is acting as our api gateway and by that i mean this is the actual project that will receive api requests from clients and then dispatch uh different events and messages to these different micro services this way we really only need to expose one central api or an api gateway to our clients now just as we've done before we're going to open up a terminal for the sample back end and we'll have to add the microservices package from nestgs now the main ts will stay as the same as it always is but now in the app module what we're going to do is in the import section we need to register something called the clients module from nasjs microservices and we're going to call register on it and what this is going to do is it's going to allow us to inject a number of services that are that our actual application can use to dispatch events and messages to the microservices so let's see this in action for example let's hook up the communication service that we just created so we can provide any name this can be any name that all this will represent is the injection token that we'll use later on so in this case i'm calling it communication to represent the communication service next we need to specify the transport so we know in this case the transport is tcp and now we can actually use this in our app code to communicate with our services so let's do just that in our app controller i'm going to actually create a new route here and this will be a post route and we're going to call it create user so we're going to take in a create user body let's quickly just create a create user request dto and we can export a class here create user request and we're gonna have an email and password that come in over the network as two strings so now in our app controller we can specify the body here we know it'll be the create user requests of type create user requests so now that we have this dto we can specify to our app service uh that we want to handle this so so in our app service we're going to go ahead and have a create user method it's also going to take the create user request in our absolute risk just to make things simple uh we can have an in memory database here so i'll have an in-memory array of users type any so this will be an empty array here and simply we can just push uh the create user request to this array so basically we are just allowing uh the api request to come in and add to this array pretty basic stuff but now what i want to do is emit an event to our communication service telling it that the user has been created so that it can go ahead and handle different actions like maybe sending an email to the user sending them a text message allowing them to subscribe any number of concerns that really are beyond simple crud operations and we want to keep clean and separate so to do this we're going to use that injection token we created earlier so we're going to call inject here and specify that injection token so we know it's the communication string that we specified and this will be private read only you can call it the communication client and this is going to be of type client proxy from nesgus microservices so now that we have this communication client in create user we can specify that we want to emit an event when the user was created so we can specify a pattern here which is really just a string literal that uh we'll specify the event that we're emitting and then we can specify uh a payload a dated payload so i'll go ahead and create a new event i'm going to call this create user.event and all this is going to be is just an object here that is really just going to take the email we don't want to specify the password over the network all we're concerned about is the user's email that was just created so we have this new event and we can use it now so specify a new create user rate user event and then of course we'll pass in the email from the create user request so now after we're creating the user we're admitting this event uh to our micro service so in our app controller make sure we actually call this app service dot create user and pass in the requests and so now back in our communication micro service we need to wire things up to listen for this event so our controller acts as the entry point for all communication in the app and it will be the same for events and messages in this architecture so we'll add a decorator here called event pattern and now we specify the pattern to which we want to listen for and of course we know this is the user created event pattern and then we can say we want to handle user created so this is the event that we are responding to and we know that we're going to get a data payload here and this is going to be the same event createuser.event and we can simply copy over the other event we've created and paste it in here because we know the payload will be the same so we can say a create user event is what's being transferred over the wire and now we have access to this event so our app service will create another method called handle user created which will take the data and of course you know this is the create user event and now in here uh let's simply just log out uh in here saying this is the handle user created method in the communication service and we'll log out the data and maybe we could add it to do here for later in the future or if it's something you'd want to implement we could email the user and welcome them to our service so in the app controller we'll have to make sure we call handle user created and pass in the data so now back in our terminal you want to make sure we're running yarn start dev in both the sample back-end so we can listen for api requests and do the same for the sample communication app now once those have both started up we can open up postman and we can make a post request to http localhost 3000 which is where our api gateway is listening for and our default post route is the create user route so we can specify that body with the email and password and send off a few different requests here changing the email and password and then if we go take a look at our communication service we can see that it is correctly logging out for each request that comes into our back end we're sending the handle user created event and the communication service is correctly responding you can see it log out the event here so nice work with that basic example let's go ahead and now set up our last microservice the analytics service so this will be a hybrid application that is a microservice and it will also listen for http requests so as we've done before let's open up the sample analytics project and of course we'll have to add an sjs slash microservices once that finishes we can open up the main.ts now this main ts file setup will be a bit different because we are actually going to keep this bit of code here where we create the nest factory and we listen however i'm going to change this port to 3001 because our sample backend is already listening on port 3000 so we don't want those to conflict and now what we're actually going to do is we can call app.connect microservice and here we can specify the transport this will be transport.tcp and additionally we need to specify an options object here and this is because we already have our communication service listening on port 3000 so we don't want those to conflict either we'll have to specify a separate port here so now we're going to tell it to listen on port 3001 for this microservice so now in addition to calling app.listen we need to call await app.start all microservices so now we've created a hybrid application that both has a microservice connected and it's listening for http requests now in order to make use of the request response based microservice communication pattern i want to set up another event pattern here that also responds to user created events so as we've done before we can simply specify the event pattern and say that we want to respond to user created events in our app controller here so this will look very similar to the method we've already created we'll have a piece of data and we'll have to specify the create user event make sure we paste this in the create user event with just the email and back in the app controller we can use this here the create user event and now in our app service let's go implement this uh and so instead of just logging things out we're actually going to maintain an array here that we can use for our analytics and our request response uh pattern so i'm going to declare a read-only array here called analytics and it'll be of type any and set it to an empty array so now in our handle user created method we'll take in the data we know it's the the create user event and we can of course still log out as we've done before to make sure things are working as expected but then i also want to push to our analytics array i want to push an object where we'll include the email from the request but also i want to just include the timestamp so this will maybe be some fake analytics data we know exactly when the user was created and maybe we could send this to some sort of external server anything you would do to maintain some analytics so now in here we will reach out to the app service and pass in the data so as we've done before we're going to need to hook this up into our api gateway so open up the app module in the sample back end again and as we've done before we'll have to specify a community a different object here in the clients module so in this case we'll have a new name for our injection token i'll call it analytics of course we'll specify the transport which we know is tcp and now since the port is different we'll specify an options object and specify the port at 3001. so in the app service let's go ahead and paste this same line here we'll have to change this client proxy to be of type analytics here for our injection token and then of course change the name we'll call this analytics client and the in create user event i want to now emit two events one to the communication client and one to the analytics client so finally as we've done well with our other microservice make sure you cd into the sample analytics folder and run yarn start dev to start this service up and now if we fire off another crate user request we should see our analytics handler comment or log out the payload here just as we do in the communications service so finally let's go ahead and implement an example with a request and a response to our services so in our sample backend what i want to do is create a new route here this is going to be a simple get route with the name analytics we are going to call this get analytics so we're going to proxy this off to our analytics service so in our app service i'll add a new method here called get analytics and all this is going to do is proxy off to our analytics client and we're going to send a command so this is going to be similar to our event it's going to be an object uh which will be matched to whichever service we send to so we'll add an object with a command key and we'll just simply call this get analytics and the payload will be an empty object we don't need to include any data for this request now importantly this uh request here the send method it returns what's known as a cold observable so only when your code subscribes to this call will the message actually get sent thankfully nestgs when you return an observable from a controller route it's automatically subscribed to and the result is returned to the client so we can simply call get analytics from our controller here and everything should work as expected so let's go back into our analytics controller and as we've done with our event pattern we're going to need to specify a new decorator here called message pattern where instead of the just a string we're going to specify the whole pattern so this is the object with the command and of course we just set this up it's called get analytics so we can now actually implement this method which has been proxied on from our back end api gateway and we will call this dot app service and implement a simple method in here we'll call it get analytics and all this is going to do is return this dot analytics very simple so we can just call get analytics and so now after we go ahead and create a few more users hopefully see that the analytics service is being updating the analytics array so now if we call our backend with the analytics route and call get we can see this data that has been returned from the analytics service the list of emails with the timestamp and additionally our analytics service also has an http server so let's make sure that's working okay on port 3001 if we simply call the normal get route we can see the basic http response hello world so this has been a very basic example of nest js micro services for my next video i'm going to show you how you can deploy an sjs application using kubernetes so if you'd like to see that make sure you subscribe like the video and leave a comment if you have any questions and i'll see you in the next one
Info
Channel: Michael Guay
Views: 89,877
Rating: undefined out of 5
Keywords:
Id: C250DCwS81Q
Channel Id: undefined
Length: 17min 55sec (1075 seconds)
Published: Fri Jan 07 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.