NestJS Microservices with RabbitMQ | Messenger Clone [1]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
we're going to build a fully functional cross-platform Facebook Messenger clone using react native and sjs you only need some basic skills with react and nest.js as we're going to learn all about microservices with Nest JS rabbitmq react native docker postgres redis so if that sounds exciting let's jump straight into the system design [Music] for anyone that hasn't worked with react or an sjs or they want to dive much deeper into building a practical project to get comfortable with the Technologies before diving into the Facebook Messenger clone I'd suggest that you check out my YouTube channel I've got a 30 hour LinkedIn clone series and I've also have a 12 hour Amazon e-commerce shop series on my YouTube channel and also in this video we're going to be exploring microservices with rabbitmq if you've never seen micro Services before I've got a quick 15 minute tutorial on my YouTube channel so make sure you check that out it's all about microservices what they are the message Brokers and also the different types of transports and how to select the right one it's a great video even if you do know about microservices as it provides great context for what we'll be building in this Facebook Messenger clone Series so make sure you check those out and without further Ado let's get started on the system design so we'll start with our client the client is where we're making the requests from so this is going to be our mobile device in the case of the Facebook Messenger clone on our mobile device and this is going to make a request to an API Gateway so I'm going to have this service here this is its own server and the API Gateway is responsible for handling the request and forwarding them to the right service so if we think about our Facebook Messenger client the very first thing the person's wanting to do is they want to be able to log on so they're going to want to actually sign up and then log on and to do that we want to have an off service that handles that and um data relating to users is often less done in a relational database so we're going to actually connect to a relational database here for that sort of data and this will be the postgres database so I put PNG here for postgres so what other things would we want in our Facebook Messenger application so we're now able to sign in and register an account um and log out and all those sort of user related functionalities another thing we'd want to do is we want to be able to chat so that's a deep service in our Facebook Messenger clone uh before we can chat one thing we might want to do is we might want to see which of our friends are online or not so we can have another service here so I'm going to have a service here and this is going to be called presence and that's basically the green light or the green circle you see on your friends list to see who are online who is offline because often you'll go to that page and you'll be able to see oh someone's online you're more likely to send them a message um and depending on if they're online or offline um it may you know will have different interactions or different ways of handling the um the way the message is received and another service that we might have is we might also want to be able to call so I'll put that over here and that's basically video calls or audio calls um that you might have and with chat the thing about chat is you actually have he has huge volumes of messages that get received and needs to be dealt with and there's often you know a child user sends a message twice the message the user might send a couple of messages and receive a couple of messages but all in all it's roughly like a one to one sort of read to write ratio there and because we've got such large volumes of messages that um we need to access them quickly for our whole application to run smoothly um so something that would be good to handle that in terms of data storage will be an in-memory um data caching or key value storage so I think a good choice here will be uh rents so I'm just going to put this here so redis we can use this actually for many purposes it's not to be confused with the Reddit transport layer which we've discussed in the previous video um but Reddit actually started off as an in-memory caching um it's about access things very very fastly but it can actually use it as a primary database and we're going to use it for the super lightweight and super performant um and because we're dealing with such large volumes of these chat messages it's going to allow us to you know have that quick access to that so you may be wondering how all our services going to communicate to one another and we're going to use rabbitmq so rabbitmq as mentioned in the previous video is the most popular message broker um it's used we're going to use it mainly for its reliability but it also deals with complex routing and it's highly available so let's take a look at the rabbit mq and how it works so basically we're going to have some sort of service so let's say we have a service a here and we call a the producer so a producer is the person that creates the message that's going to get forwarded and then likewise we're going to have a consumer so on the other end of this we're going to have some sort of service here we'll call it B and this is the consumer so as we talked about in the message broker video um if a is going to receive lots and lots of requests it needs to be able to handle those and if it receives a request and is still processing the request it's going to you know clog up the server and that means other requests to a aren't going to be handled nicely and on the other side of things if B has lots of requests that it needs to handle based on a um you know they'll slow down things so the solution to this is actually to create another server and I'm just going to put this red box around everything here just to indicate that this is essentially what rabbit mq is this is where the message broker side of things come into play so the way rabbitmq works is the producer it creates some sort of message it needs some sort of it gets a request and it needs to be able to handle that request so what it does is it actually moves that into what's known um as an exchange so this is an exchange and the exchange is responsible for taking the request and um sending it to one or more cues depending on the particular route or whatever it's trying to solve so we can actually this exchange you can forward to various different message brokers um and these are going to be the queues here so this is a queue a q data structure so basically um what it looks like here is a it's receiving a request and it wants to keep receiving more and more requests so it offloads the particular Quest it sends it firstly to The Exchange The Exchange is then responsible for um you know it puts it at the beginning of the queue so in this case there are no tasks in the queue um so it's going to send it to the front of the queue and it could do that for multiple ones if we're talking to multiple services for instance um and then b b the consumer it's looking at these cues and it's basically when it's free when it's not dealing with any other tasks it can take something off the queue here and it can go ahead and it can process that so what it means is a is able to get more and more requests it can offload those into the exchange and that they send the queue R and B just deals with one of them at the time now it is possible that we have more than one consumer here so I'll just go ahead and highlight that by putting another service in there but as you can see the rabbit mq um it's basically extending on the message broker video that we've seen in the previous video and we're now able to handle requests how does rabbitmq work well let's say we have a server and it's receiving lots and lots of requests um we don't want that server to slow down so we want that server to continually receive requests so we can actually offload some of the tasks into a queue so this is where rabbitmq comes in handy the producer it creates the message it offloads it to an exchange The Exchange is responsible for forwarding that to a queue or more than one queue if it connects to multiple services The Exchange um yeah it passes it to the start of the queue and then the consumer it's waiting for a message when it's not dealing with any other task it can pop something off the front of the queue and it can handle that process so where rabbitmq solves an issue is it takes the load off the producer a it um you know it separates where all those um processes are happening and it deals with them one at a time through the Q data structure and then B it also is only dealing with one process at a time which means A and B they're no longer receiving high workloads and then also the rabbit mq this is sitting in its own server to handle all of that another technical point about rabbit NQ is that when the consumer receives a message from the queue it's called an acknowledgment and you may see this in the code as an ax and this basically allows us to have that reliability there um because we can actually acknowledge that we have received message from B which is allows us to remove it from the queue here but it also allows us to process it over here we've certainty that has actually received the message and the same isn't true in regular TCP um communication so we can see the benefit Robert mq has here so now that we've explored rabbitmq and the very high level overview of the system design or the Facebook Messenger architecture let's go ahead and dive into the code and actually start to connect our first microservice up in sjs using um let's go ahead and get started building our Facebook Messenger clone so the first thing we want to do is we want to set up the back end and we want to set up the nest JS micro service so we're going to see how to set up a micro service in sjs and we're going to use rabbitmq as the particular transport so of course we're going to need to have Nest installed so go ahead and install the CLI there and you're also going to want to use Docker because dock is the easiest way to get the rabbitmq service up and running if we go ahead and install those for your particular operating system and then we're going to jump into the code here and we're just going to open up the terminal and just check that you got Nest installed by doing Nest V just check you've got the right version there and we're going to run nest new API so what we're doing here is and we'll just use npm to scaffold it what we're doing here is we're creating the base project API Gateway and what we're building in this video is we're building the connection between the API Gateway and also the particular service and we're going to use the beginnings of the auth service so we're going to see how we can set up the micro service and rabbitmq and have them communicate with one another using that mentions transport there so let's go ahead and just let this load okay so now that's loaded let's go ahead and CD into our API and if we look at this folder here it's just the typical Nest JS API so we should be familiar with how to make a mess JS or the nsjs boilerplate so we should be able to be familiar with how to make an sjs API and if you're not already please see my LinkedIn clone series or my Amazon clone series to get a good idea because this is going to be more of an advanced project that builds up on at least the fundamentals of nest.js um so we can see that we've built this we've got all the boilerplate for our nsjs application so what we want to do is we actually want to use the nest CLI and there's actually a command Nest generate app and we're going to generate the auth app and before I run that let's just take a look at this Nest CLI file and we can see that we've got this Source root here um sorcerer is source and there's not really that much configuration going on here but let's go ahead and run Nest generate app auth and if we have if we just minimize this terminal for a second we can see that it's added a whole bunch of stuff into our CLI so the first thing or one of the important things to note is that the root is the apps API so it's generated this apps folder and then it's generated the API folder in there because our base project was the API so that's going to be by default the root and we can see that you know it has all the um entry points and all that as you would expect but you can see that we've actually got two things going on here we've got the API and then we've also got the auth and these are the applications and this is what's known as a mono repo so we can see here that we've got the mono repo to true and what this allows us to do is it allows us to share any of our common code so a way to do that is we can actually generate a library so we can run Nest generate or G for short library and I'm going to call this shared and this is just setting up a shared Library let's just go ahead and use app as the default here I'll go ahead and click enter here and we see that it's created this Libs folder so if we open up the libs folder uh and we see that we've got the shared folder in here now by default it's just got this shared module shared service um but we can add in there so for example we might have multiple different apps but we need Authentication or authentication guard on multiple different API endpoints or service endpoints um so you know that's one way we can share that functionality uh the other benefit of having this mono repo is that all of our packages that we install at the root level will be shared in our apps and that's really good because when we're using microservices we have quite a lot that we do share um so for example let's go ahead and install a few so we're going to want the nest JS config service because we're going to reference some environment variables to set up our server so we're also going to want to get the micro services dependency and if we look at the documentation on sjs we can see that we also want to go ahead and get this ampq lib and ampq Connection Manager for using rabbitmq in sjs so let's go ahead and get those and we can just install those wait for those to install and then we should see in the package.json here when it installs we should get these extra dependencies here so we see we've got the config micro services and these two here so we can go ahead and close that let's just go ahead and close our terminal up and let's just start to connect well actually before we work on the nest.js specific stuff let's get our rabbitmq server up and running so in this in the root of the API folder here we're going to want to use docker and we're going to want to create a Docker compose file so let's go ahead and create that Docker compose Dot yaml and there's no expectation that you know Docker here um so let's just go ahead and run through what Docker is um Docker is a way to basically um create containers and what a container is is it's basically a um its own particular bit of software that runs which manages all of the environmental stuff that you need and the benefit of using it is that we're able to use it on any machine and have the same configuration so I didn't want to go too much into the theory of Docker I want to be have have a more practical um you know practical approach by learning by doing so basically what we're doing is we're creating an a rabbitmq server so that's different to this servers here and if you're familiar with the monolithic um structure or architecture there's just one endpoint for the oh sorry there's more than one endpoint uh unless you're on graphql but there's one server um a monolithic server here we actually got multiple servers so we've got the API Gateway that's its own server we've got the auth service which is its own server and every one of these Services is going to be its own server so and in addition rabbit mq is the transport and it's also on its own server um as we see in the video um before this video sorry in the videos um before this video came on so let's go ahead and check out how we can write it so the first thing we have here is um oh and I should probably mention that um I've got these Docker extensions here it helps you write the docker file so you can just go ahead and search Docker and extensions and get that first one there it comes bundled with a bunch from Microsoft there so that's pretty handy um but basically sorry we don't want version version version version is just like the latest version of Docker compose and the latest version just happens to be three you can always just go to the docker compose uh information on the docker website um to get the latest it doesn't change too often as you can see that we're only in version three but it's always good to check if you're not sure and then in Dockers in Docker compose files we can actually uh have this Services um key here and one thing to note is indentation is important in Docker so make sure um everything's lined up as you see here so version and services there on the same layer and then you can either press tab or you can have two spaces I got my tab set to two spaces so I could press either but the services are basically each independent server that we're spitting up which is in its own container so we want to get the rabbit mq service going to start off with so with Docker we have this thing called an image and an image is basically the bit of it's sort of like the blueprint and it has all the complexity done for us so on Docker if you go to Docker hub you can search for pretty much anything you want a postgres database rabbitmq node whatever it is you're looking for and there's probably already an image created for you so you just want to go ahead and see whatever it's called so this is just called rabbitmq but we also want to have a graphical user interface for this so there's also another one called rabbitmq and the specific version of the rode mq is 3 Dash management so we can go ahead and search for this here if we just go to Docker Hub and just type in rabbitmq we can see the official image here and then you know you can sort of look at all the different versions um for example you can go into here click on version 3.9 management um and there's a whole bunch of different information here you'll see that it has specific port numbers and all these sorts of things um so if you need to get into any of the lower level details you can definitely come here and check them all out um but fortunately I've done this ahead of time so we can just simply reference that image there and that is all we really need to do to have that um but by default the container name takes the image name so I'm just going to not want that three dot dash management there so I'm just going to call the container name rabbitmq and likewise I'm going to have the hostname of rabbitmq that's just they're not too important these two here um or the one above it at least because that's just a naming of the particular microservice it just helps simplify things when you've got a whole bunch of services running just to easily identify which one's which but something that is important is the volumes um and the way we have volumes uh we specify by having a let's see a dash here like this and a volume basically is um where the data is going to be saved um so basically you have name volumes and unnamed volumes um so an unnamed volume is basically something that we will go on the server ah sorry in the docker container so the thing about Docker is it's sort of like Linux if you if you're familiar with Linux for example where it's you have the operating system um and then you have your folders um and then you have all of the necessary software that you need on that operating system um and then that's what allows the actual image to be able to run on that particular container or so yeah the image sort of is like a layer higher than that um so basically what it means is what we can say here is it's sort of like having a virtual machine let's say so like when this gets created you can sort of imagine that you have like another uh machine and that has its own folder structure and everything like that and you can actually um sort of access the folders in that virtual machine um like thing so basically on the docker or in the dock container there's a VAR Library and they these are this is actually um the recommended way to do it um if you look at Docker Hub if you look at the details on Docker Hub so I'm not just making this up um this is just following the documentation basically um so that means like because you can imagine that you have like a database you don't want to lose all the information on your database um the volume allows you to that information to be sort of bound to the container um you can also make it down to this local files and stuff like that um but right now we'll just focus on this here um so we can also have some ports here and by default the docker Port is five six seven two and the reason we have this in a string is because we're actually going to map it so we're going to map it to five six seven two so you might think why do we need to do this so basically there's two you've got to imagine that there's two different machines there's our machine and when you normally you know have an sjs on Port 3000 or react on Port 3000 um you can just run the application and go to the port but you've got to remember that this Docker file or Docker this container is going to be running in its own sort of machine let's say like technically it's running on this machine but it's treated as if it's going to be on another machine um similar to like when you deploy it it's not actually going to be on your machine it's going to be another machine so basically we want to be able to access it on our host machine so our host machine is going to be 5672 and then we can map it to the machine other Port that's in uh docker so that allows it to communicate between the two so the other thing we want to do although the port we want to have here is 15 672 and again this is just from the documentation and why we have this why we have two ports is well we want um one for the actual rabbit mq server but we also want to have another graphical user interface which comes with this three dash management one so we should be able to um you know access that on a different port um and the final thing we want to have here is we want to have some environment variables because you like you can't just log into these services well you can if you don't have a password but that would be really insecure um you need a way to actually log into all of these different tools like databases and these management consoles and all these sorts of things so you can have something here which is an EnV file and this is just going to point towards a DOT EnV file which we're yet to create so let's just go ahead and create this EnV file so the EnV file is going to sit in the root here and basically we're just going to have a whole bunch of variables here and these variables me allow us to access them both from our Nest application and also in our rabbitmq application because right now I'm just going to have this service here um just mainly because it's the easiest way to get the rabbit mq server up and running um but we also want to connect to it with an sjs and just for this video here while we're learning about micro Services I'm going to have those Nest JS Services just running locally um I will move them into the um into the dock compose file in the next video um but in this video I really want to just introduce microservices and just how they can connect to one another um so let's go ahead and come up with some environment variables and these variable names they're also in the Docker Hub rabbitmq documentation at least some of them are like rabbit unders oh sorry rabbit mq underscore default underscore user and I'm just going to call this user and then I'm going to have another one here for the pass for some reason I don't like the word password they choose pass and then so these are the variables that are defined um oh I don't know where that happened these are the variables that are defined um in the documentation so I'm just going to go ahead and sort of copy this same syntax here um where it's the rabbitmq underscore whatever it is um for my particular variables here so I'm going to have a rabbitmq user so we've got the default user but then we also just got the rabbit mq user um and these values can be whatever you want them to be you just got to remember what they are um so I'm just going to make them simple so I'm going to have the rabbitmq user which is user rabbitmq pass which is password [Music] um let's just copy this down two more times we're going to have the rabbit mq host which is localhost five six seven two and we're also going to have a queue so we're going to have the queue um so right now we're going to um so we've got our API Gateway and then we're going to go to our auth service we want to connect that so there's going to be an auth queue in between those two so I'm just going to have a rabbit mq auth Q and I'm just going to call this auth underscore Q and these environment variables that we've defined in the root they're going to be available everywhere in our application if we use the sjs config service and make use of them so now we've got that we should be able to run our Docker compose file so let's go ahead and just open up the command line and we can just simply run Docker Dash compose um up and actually before I do that I might just go ahead and connect all of the um sjs stuff actually let's just see if that works just to make sure so we know that we've got the management in 15 6 7 2 so let's just wait a second for this to load and actually this might take a second to load the first time because it's got to go through all the different layers and get rabbitmq and all that sort of stuff um but basically when that loads we can go to localhost 15 oh there we go we got it loaded already and then we can see that we're in the management dashboard um and then if we want to log in we can log in uh what was it username and password I believe we can go ahead and we can log in um username password okay I'm not sure why that's failed oh it's just user let's come back here user password and then we can see we're in here so we haven't actually set this up yet um but we've got this queue here and when we set it up we're going to see our auth Cube we can actually see the communication between the services um and so we've got this exchanges here and these queues here which we'll look into when we set it up so let's go back to our code and we can close this up we can close this up I might just control C this to kill that um so let's just run Docker compose down just to make sure um okay so now what we want to do is we want to actually let our API get way communicate with a microservice so if we go the first things first is if we go into the API Source folder here and we're going to main here we see that we're listening on Port 3000 and that is what we want because the API Gateway that's receiving the request the HTTP requests from the client so we want to make a port publicly available um but we'll see in a moment with the micro Services how this changes um so let's go to the app module now in the app module this is where we can start to Define um our micro services so first things first we're going to need access to those environment variables that we created so let's just actually reopen that environment variable so we can easily copy and paste the names of them but we can actually go ahead and use the config module here and we can use for root to access it globally throughout our application and we can just go ahead and set is global to true and we can access the environment file path uh now that we're in a mono repo um the relative files are from the base so we can just say dot EnV the base here so we can access those environment variables here so we see that we've got the typical app control app servers we don't really want the you don't really care about the app service let's just leave it there for now but let's just um just because we want one service that will come back to changing but basically what we want to do is we want to provide um basically what we want to do is we want to provide our micro service we've the ability to communicate here so basically we need to provide all of the environment variables to our conflicts to the auth service so we haven't actually created the auth service yet um perhaps it might make more sense if we go to the actual app itself so let's actually before we come here let's actually go to the actual micro service so we've got this API folder here we've also got this auth folder here and if we open up the main file here we see that this is also listening to Port um three thousand now we don't want to do this for our auth service um it is possible to listen to the ports and also have the microservice communication that's called hybrid microservice and we've seen that in the previous video but we want this to be a micro service and then we want to be able to call this microservice and communicate it from the API Gateway so what we want to do here is we still want to use Nest Factory to create the auth module but we want to just go ahead and delete this line here and what we want to do is we want to access our config service so let's just say config service by holding taking hold of the app and we can go ahead and get the config service um so we should be able to import that from this JS here and we can save that so basically what I want to do here is I want to get all of the variables from the config service so that's these variables here so for example rabbitmq user we want to get hold of that so let's have a variable here we'll call it user and we'll use capital letters to signify that it's not going to change so we can use that config service here and we can just go ahead and get the rabbitmq user and then we're going to do the same thing for the password for the rabbitmq host and for the rabbitmq auth queue and then of course we can rename these password host you and then we can just save that to get prettier so what we can do rather than connecting to the port we can call app and there's actually a method on it called connect microservice and this is going to be of the type micro service options what's going to uh you know have that generic type that it can call with the micro service options here and this will give us some type safety for our sorry not hot safety uh intellisense for our object that we want to create so to specify the particular transport that we want our microservice to use we can just go ahead and use this enum here so we can use the transport enum and let's go ahead and grab that in from sjs microservices and then we can see that it this various different types here we want the rabbit mq one because our app's going to talk to one another using the rabbit mq transport so then we want to have some options here and this is where we specify the URLs for example so we just have an array here I'm just going to use um back ticks here and I'm going to access the URL amqp which is what we're going to get from our rabbitmq connection and then they'll allow us to use these template literals here to get the user to get the password and we can have the at um dollar sign post here so if you're looking at this it looks very familiar to how you connect to say a database or a postgres database or something like that because it's essentially the same thing you have a bunch of details user password host that you're trying to connect to another thing that I mentioned earlier is that um and this might have been the previous video but a consumer a producer produces a message a consumer uh consumes the message and when the consumer takes the message from the uh message broker it can acknowledge it and that's known as an ack so we can have something here that says no ack because by default rabbitmq will Auto acknowledge things um we want to set this to false because we want to manually acknowledge our our requests um and that sort of helps ensure that they don't get lost or anything like that so the next option here we want to have is a queue which we can just set to our queue and then we can have some corresponding Q options here I'm going to have durable here so durable uh basically what that allows us to do is uh the message broker won't be lost um or the data associated with the message broker won't be lost between restarts so we can go ahead and we can save that and then finally after all of that we can come here and we can say start all microservices so what we've done in our auth micro service we've sort of in the main TS file we've just sort of said that it is a micro service it's using the rabbitmq transport we're connecting to our um you know our rabbitmq server um and it's starting that and that means if we can configure our API Gateway or any other service we should be able to communicate to this micro service so basically let's just take a quick look at the auth module so if we have a look at the auth module here we actually might want to copy this auth module here because we're going to want to have those um we want the config module to be imported here let's go ahead and get that in there the rest of that can stay pretty much the same um but now let's have a look at our auth controller so we can see here um something we want to do is we want to be able to communicate to this um you know to here with the transport so with regular API calls you see things like um you know these annotations get get the hello for example um with transports oh well with micro Services we're going to have this message uh pattern annotation and the message pattern what we can do here is we can actually have some um CD sorry yeah CMD I was getting react intelligence for some reason um so we can actually call this something so let's say we want to get a user for example we'll just say we get Dash user so we're going to there needs to be a way for the API Gateway to talk to this particular microservice so we need some sort of um way to communicate and having this command get Dash user if we have that in the API Gateway similar to if you have a service or if you have a controller and you have a particular endpoint that you're using so this could be slash user for example the API request will make it into the controller and then it will look for that particular route and if the route matches then it'll call the corresponding method uh similarly the API Gateway it's going to detect that particular command or string or something like that that I can hook onto and then call if it finds the particular match so we can have a function here and I'm just going to call this um well it doesn't really matter what I call it but let's just call it uh get user let's say and basically what we need to do is we need to access the context and this comes from rabbitmq so we can access the context by having this context come in from the sjs microservices uh package which I don't know why that didn't import let's just grab it in and basically we can go ahead and set the variable context to uh rabbit mq context so we can bring that in as well let's get that okay let's get that um so basically we can just go ahead and we can get the channel so basically on the context object um that rabbit mq provides us um we can just go ahead and call this function here um get channel ref and on that particular functionality so I mean this is obviously built into the library you don't have to know that all the details but basically the channel um ref it allows us to get the particular message um from the context so we might be able to just get the message right so if we go to contacts get message that allows us to actually you know check that with um you know that it's the right command that we're looking for here and what we can do at this stage because the API Gateway if you recall has created the message it's gone through rabbitmq so we need to get some of the context from rabbitmq um to be able to get our message from the channel um and then at this stage we're sort of consumed the message so we can acknowledge that we've consumed it um by saying Channel dot ack um and then passing in the message that we've got from the context there and then finally we can just go ahead and we can just return so I'm just going to return some sort of um user here and user here so that's what the micro service looks like um but how do we connect that to the API Gateway so I think to be able to address that we need to come back to the module and we can actually copy some things potentially like these here so we come to the app module we've got our config module injected here I'll import it here I should say and then we have this provider we've got the app service but then we want to actually provide an auth service so this is the way we can communicate to our actual micro service so let's go here and we can say provide auth service um and basically we're going to use this auth service we're going to inject it into the app controller um and we're going to see exactly how this works and sends to the thing to the micro service so we have because basically what's happening is we've got the API Gateway it's not aware of the functionality in the auth stuff um so we want to be able to consume that from the Gateway and then forward the request to the appropriate service so we do want the API Gateway to be aware of the auth servers and handle the auth service um so we can just actually come here and we can say we want uh use Factory here um actually we don't want that but what we do want is to be able to say that the use Factory and we're using Factory because we want to pass in the config service so we can just go ahead and pass in the config service here um which is of type config service and then we want to go ahead and we want to copy and paste all those variables we had yeah and then what we can do is we can return a client proxy Factory and we can go ahead and we can firstly import that um so that's coming from the micro Services package um and that allows us to create um so we still need to have the transport here so we can have our transport which is going to be of the transport type or enum and it's coming from here and we're using rabbitmq so we can actually copy this once again from here actually we can copy pretty much all of this here so I'm just going to copy all these options here and we can see that we're connecting to the same URL got the Q here we can get rid of that because it's on the other side that we're acknowledging it um true okay so basically what we've done is we've got the API Gateway set up with microservices or able to connect it um so the final and missing piece that we need to complete this um is to come to our app controller and start to inject that service that we just created and able to communicate that so what we want to do is we want to um well the same way we inject services yeah we want to go ahead and inject the auth service so that name is coming from this custom provider that we have here and the auth service this should be private because we only want to access it from within the controller so I'm going to have the auth sorry the auth service and this is going to be a client proxy and then we're going to need to import that let's go ahead and save that some reason my imports aren't working smoothly we can go ahead and get rid of the app service here we can go ahead and delete this here and we should probably do this in the other Service as well we'll clean that up later you can get rid of this get here and now we just need to make use of this so let's go ahead and uh have a get um get request here so let's just go ahead and get let's actually add that back in um basically we want to have a method we'll call it get user here and this is going to return this dot auth service and then it's going to send and then it's going to send the CMD get user so it's a oh usually you send like some data um we just need to have a another object in here like that so let's go ahead and bring our Docker compose up and in another terminal here I want to firstly CD into API LS okay so I want to run npm run start Dev and you should always start your micro Services first so let's run this command here to start the auth microservice and let's go ahead and foreign [Music] not sure might have an error and then we want to CD in our API and Run npm Run start div API okay so it is looking like we're getting some errors here so this configuration for root it needs to be a DOT EnV file here so let's also check that is in the uh let's see here let's just check our files here mm-hmm okay so let's go ahead and try and run that again because previously we're getting failed to connect okay this looks better lots of green so let's try and connect our API Gateway okay there might be a similar sort of thing going on here so we can see also in our app module here we want to have dot EnV here and then let's try and start our API Gateway here so we can see in remote mq we've got this auth queue from that auth service but one thing we forgot to do is we need to actually inject the config service so we're using it here but we also need to come on this same layer here and we just need to inject that I can do that by calling inject for and have an array of all the injectables and calling that service so now if we run our API here we can see that it's built so that's a good sign we can refresh here come to localhost 3000 and this is happening really quickly if we had that no act thing off um it wouldn't process this um and you know taken off the queue so we can actually see we've got this user user here and we've done that with micro services with rev mq in SGS so let's just have a quick recap of what we've done here um well we've set up our let's just start from close folder so we've created the docker compose file um we're going to add in those two nest.js Services into the docker compose file in the next video uh in the next video we're also going to set up some authentication in the auth guards and protect different things so we're going to go dive deeper into that but here we see we've used Docker compose to get the rabbitmq server up and running we've got some environment variables here everything in this root level here including all the dependencies that are available in our apps we also have this Libs photo that we haven't used just yet but we're going to use that uh in the next video for our shared code and then the key difference is we've got apps here so we've got our API Gateway here the API Gateway this is accessible by exposing a port so through HTTP requests from the client to the server then in the app module here we've actually connected the auth service with the environment variables here so we've created a client proxy using the rabbitmq protocol so that means if we ever um you know if this HTTP request gets called It Can offload that to the um auth servers um and the auth service because it's connected with Avid mq it offload it transports that um to the appropriate micro service and then it does that by hooking on to this get user here um so if we just take a quick look at the API at the auth service the difference between the main files are that I'm we don't expose a port here um we connect using this micro Services approach and we connect with all those variables here um then we can come into the auth module give it the config that it needs and then we can come to the auth controller and the auth controller it has this at message pattern decorator uh so the rapidmq transport handles that and then it looks for this particular uh command here and then it's able to through the message Brokers through the context there it's able to hook onto the particular message and we're able to acknowledge that and then we can give back the message user so we've done a great job we've learned all about microservices in sjs um it's the same sort of thing for most of the other micro Services um so we've connected an API Gateway to an auth service in the next video we're going to continue to build out the docker file for the services for the nest.js services and we're going to build out some authentication so make sure you subscribe to my YouTube channel and stay tuned for that next video thanks so much for watching cheers
Info
Channel: Jon Peppinck
Views: 40,197
Rating: undefined out of 5
Keywords: webdev, app development, lesson, tutorial, coding, programming, full stack development, programming tutorials, developer, react, nestjs, nextjs, nodejs, microservices, docker, kubernetes, rabbitmq, kafka, containers, mircorservices architecture, node rabbitmq, nest.js, node, microservices training, microservices for beginners, microservices tutorial, library app with microservices, nestjs microservices tutorial, best nestjs tutorial, nestjs tutorial for beginners, what are microservices
Id: LJwZxSD1QOM
Channel Id: undefined
Length: 68min 29sec (4109 seconds)
Published: Thu Dec 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.