NestJS Microservices with Redis: A Beginner's Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will be discussing microservice architecture in N CH using radies so I have created a new application called APA Gateway inside the source I will remove controller test file controller as well as service also the in app app module remove the reference cuz we will no longer use these files now we will generate a module using nestjs C so in this scenario we will use Nest G for Generate M for module and our module name which is orders this will creates a orders folder in our source directory within inside this directory we can see this module is created by the name of orders uh we will also create a controller using the same way using Nest CLI for orders module this will create orders controller and update it in our orders module we can see in app module orders module is imported let's remove the test file and inside the orders controller we will create a route which will be act for creating orders so this create orders M out will accept as a body customer property and the order n property which we will use for testing so for now we both will be isting and we will return this as as it is now a route is created we can test our route using Postman so let's start our application development mode in Postman we will make a request to Local Host for 3,000 SL orders using post request in payload we will provide customer as a email and Order n as a string after that if we make a request we can see it's successfully resolved so that now our API gway is working fine and we have a route now we will move into notification service I have created a application using NJ CLA and by the name of notification service in notification service in the first I will remove those part file which will not use like app controller app service and also its reference from App module to use n CH micros service we have to add two dependencies one is nestjs microservices and for redes we have to install I RIS package after those package are installed successfully we will convert or make it compartible our app you to micros service we'll use app. connect microservice method which his type is microservice option and this function is accept some properties like transport in this scenario it will be radi and also in the options property we have to Define port which will be our currently running radi Port so let's get this port from radi CLI we can see it's running on P 6379 if you don't have radi install you first need to install it and the host will be Local Host after that we have to restart our micros service using start all micros service asynchronously so a everything is configured fine we will change our Port default 3,000 to 4,000 cuz our API G is using same put 3,000 now we start this application in development mode to check everything is working properly we can see it's working fine so now we will move into our API Gateway to connect our micros service inside the API Gateway so that we can emit an event whenever a order is created in API Gateway we have to first install njs micros service package as well as I packes to make it connect with other micros service after installation we will open orders module and inside the Imports array we will use client module from NJ micros service and use its register method which accept array of object inser the object we will first Define a name for this micros service let's call it notification service and inside this option we will provide the port as same as our notification service options where is Port is and the host is Local Host let's fix the spelling now that we can connect this micros service using the inject decorator so inside the order controller Constructor we will use inject decorator from njs common and provide this name which we Define in configuration and use the client and type will be client proxy from njs micros service after that we can emit an event this do client. emit method and this will accept as a eting which is event name and the second parameter is the payload which we will send when event is imem so let's send customer and order now one thing I forgot to add in client module register configuration which is transport so we have to Define transport as a radius so now we can see everything is fine and our application is running perfectly so let's get back to notification service and create a module let's call it mailer as well as create a controller for mailer module inside this controller we will first remove the test file and inside this controller we will Define a Handler route as well as the event pattern decorator so the event pattern accept as a string which we have Define in imit function and after that let's create a method let's call it handle order created this will accept the parameter which is data this data will came from when the event is emitted for now we'll add conso log and let's start this application in development mode to test so if I open Postman and make a request we can see in notification service a console is printed if I hit it again with another payload we should see there another console log is printed so that now when order is created a event is emitted to radi and inser the notification service we can see we can get this data whenever event is emitted and we can fform our logic inside the notification service based on the event and and its data so that's it about implementing radi microservice in newj thank you for watching see you in next video
Info
Channel: Jobayer Hossain
Views: 1,277
Rating: undefined out of 5
Keywords:
Id: EaQGVOcQMSE
Channel Id: undefined
Length: 9min 9sec (549 seconds)
Published: Wed Oct 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.