NodeJS Microservice Part 1 : NodeJS Monolithic to Microservice

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there welcome to my channel in this video tutorial we will see how we can work with the microservices in a practical way if you are looking for how microservice is working definitely this video is for you so i believe my little contribution can help you to understand how the how to kick start with the micro services architecture so what do you will do today today in this tutorial we will be building a couple of micro services which is going to be a serve our online household products order application so this application is already built in the monolithic architecture and today we will split up everything in a micro services so let's see what are the functional requirements available for this tutorial so first of all we will be listing out all the available products after that we will be will be providing user to sign up and sign in and and then we will be providing one picture which is going to be like you know user can provide user can add the products to the cart and add the products to the wishlist and after that place an order and the view orders so i'm not making these things very complicated so my intention is to like give an orbeez how these all the features i mean it is enticing together to serve the purpose and uh what if like if we are building these things and what are the complexity this be it will it will be in the monolithic architecture and what are the complexity we can resolve with the help of the microservices logic that is my main goal today in this tutorial all right so let's see how what is the architecture design pattern for this monolithic architecture so here is as you can see we have a mobile applications or maybe web application or something as a client and it will be client will be uh reaching to our backend system which is with the help of the http connections all right and backend system will be having some kind of database it may be nosql or maybe uh maybe relationship database or something else and it has a three uh different different services that's gonna be like our customer products and shopping right these are the services we are just segregated just for to uh make it responsible for uh performing certain tasks right so customers customer service will be having some kind of features this is going to be like a sign up sign in and we sleeves and address and all and the product service will be having the related all the features all the operations related to products let's say uh it's example uh listing of the products and pine products and ad products and the shopping will be a shopping service will be responsible just for the cart and orders and payment so all those things as you can see here it is enticing each and every service will be tightly coupled with the others like you know customer is depending on the products and products again depending on the customers and the shopping is again depending on the products and it's a vice versa so let's try to resolve this puzzle how we can resolve all those things with the help of the micro services architecture so as you can see this system very simple micro services architecture as uh in very beginning of the mobile applications or maybe web applications will be there and it will be just connecting with the api gateway with the help of the http connections so api gateway we will be uh deep dive a little bit in some different tutorial how api gateway is working just for others microservices or some other other stuff but the as of now we can you can just uh what you can say like you can imagine this is api gateway is a simply uh kind of aggregator which is uh redirecting like you know routing the stuff to different different services depends on the the endpoints now uh in the customer service as you can see in the monolithic way like it has only one single source of root like no database and that single database is a it's accessing from the various services like customer products and shopping so here in the microservices it's it's totally different like it is always like it will be use its own database and own stuff like maybe own memory management system and all those things okay i'm not discussing any memory management stuff but definitely it's a kind of memcache or something we can utilize just for specific service so let's discuss like you know what it is what are the what are the stuff exactly it can be make it possible by microservices architecture so it's very simple to develop and deploy which is increasing the productivity and secondly it's easier to build and maintain we did less dependency and micro services architecture is used to loosely couple uh with with the business logic and other services due to its isolations of nature as you can see it's isolated right the customer service is isolated and all these business logic and it's everything depends on the customer it's inside the customer only okay and it is not exposing anything to the products and all those things okay and secondly the microservices uses the cqrs pattern cqrs is a common query responsibility and segregation if you wish to know more about the security you can do a little bit google and you can get to know like what exactly the secures pattern so i'm not gonna discuss and brief the secrets pattern right here so this is the pattern exactly so it has the own set of database and responsibility and it is helping it is helping us to do certain i mean assuming certain tasks right and uh and one more thing is like you know easy to scale the the service and it features in the microservices way and in a single service failure of the uh of the application it will not go down the in the microservices like in the penalty architecture if we are uh if our one of the services is going down then definitely the entire system will be go down okay but the here in the microservices architecture once once of the once the one of the services is will be go down then it will not and get down the android application still that the other services will be running well or it it's going to be served the purpose so uh these are the basic stuff which is we will try to cover uh throughout of this tutorial and uh later on maybe in advanced stage we will be discuss about how how the microservices architecture communicate with each other uh with the help of the messaging queue or message broker so that's the advanced uh topic which we will discuss later but in the very first test like i will make you understand how we can just uh talk to each other in microsoft firefighter and forget the type of like http calls okay all right so let's understand the current situation of this project first we will understand the monolithic architecture the code flow and after that we will test it out like what are the end points it is available right there it is working perfectly fine or not with the help of the frontend or maybe with the help of the postman and then later on we will split it up everything the services in the microservices way all right so let's understand the design pattern which is we are going to follow to this video tutorial and here is the design pattern in the normal way so in a common node.js applications our mobile application or web application sending the request to the backend system and backend system will be having uh routes and the controllers and if it has some kind of the view engine then it will be having a view also and once it will receive the response by route the route is simply just routing this stuff uh to the controller which is exactly that specific function is responsible to handle this specific endpoint then right after processing some kind of data and business logic then it is giving back the specific response to the to the mobile application or web application okay it may be rest up your response if it doesn't have any kind of view and if it has some kind of debut engine then it will send some html response or something good and in this architecture or in this design pattern it's very hard to test this stuff because every time we need to keep it keep in mind like whatever stuff exactly we're writing it has to be testable and eventually if you having some uh data logic stuff which is need to change then you need to touch your business logic as well as if it has a separation of a concern then it's extremely tricky to handle this kind of project as soon as this kind of project will grow your complexity also it will grow right so let's uh discuss about the current design pattern which is we are going to follow uh in this video tutorial so our mobile application and web application will be send the request to our our backend system and backend system will be having our api and service and repository and then these these are all the layer so each and every layer will be having their own responsibility in terms of like api layer will be responsible for validating the request and all those things and after that the service layer will be having its business logic api layer only the only just responsible to get specific data only from the service layer because service layer will be having the business logic which is exactly need to process the data or what are uh what are the criteria of evaluation of this specific data which is exactly it's getting from the drill layer that means each and every layer will be responsible for performing certain tasks apillar doesn't know what kind of data it has residing in the reposed array okay and um api api layer is only responsible or only just dealing with the certain data okay and service layer only responsible for performing certain business logic right after getting the data so in terms of input or if you wish to change the database right now let's say we are using nosql here and later on you have decided like to use some kind of other data maybe rdbms or maybe postgasquail or something else then users to simply change the data structure or maybe simply change the data patching logic data logic right here the business logic will stay remain same and api also it's you you don't need to touch api at all okay so in this situation you know each and every layer is very clean in terms of like it's every it's an every layer it's a testable api layer so you can test the service layer also you can test even if you wish to test your repository still it can also be testable okay so this is the pattern pattern exactly we are going to follow here okay now let's go to the go to the source code here inside the source code so i'm going to install the dependencies first inside the source directory we have a bunch of directories here so now you can see this is the api which is api layer which is we have already discussed right here this is api service layer and repository layer the same way and this is api uh earlier where exactly all the endpoints and all those api stop is there okay and um here and inside the the database we are having the repository driller where exactly all those repository where we are accessing our data stuff here good and inside the service layer we are having all service layer service stuff what are the what are the operations we need to perform right here we are putting right here inside the service layer only okay so now let's understand the entry point of this application here this is the server this is exactly um index.js file is the kicking start of our server here but that database connection is simply like it's connecting the database and express app it's it's a it's a kind of express app which is uh dealing with all endpoints and all those endpoints and um uh the error handler and other stuff here so let's go to the express server here and these are the middlewares and these are our apis and this is our error handler which is we are handling each and every errors which is drawn by uh our customer products and shopping we are handling here in the centralized way so customer can or maybe client can have a kind of meaningful status code along with the error message here good so these are the apis as you can see this is the customer api where i'm we are just using from the api directory so um usually in my case uh whenever i am having some kind of uh apis or some kind of file access from inside the directory i'm not putting a specific file name because this is that that's a good thing like you can put the file name as well as but for convenient you know you can you can put inside the in the directory index file index.js file or index.ts file to export everything in such a way so you just refer to that specific directory you can get the get this specific file from here rather than climbing like uh it's on every file such a way okay so uh here there are there are three apis are there customer products and shopping okay each and every api are having their own uh stuff up like what is called on the endpoints so these are the end points exactly we are having here on the time of the instantiation of this api we are just creating our service class right here and we are using that service class here as a dependency and throughout the operation we are just accessing that this specific uh functionality from the service here as you can see here on the sign up uh if you are going to sign up this service is there customer service and go to the sign up then signup is handling from here and signup is taking the user input from here and it is performing some kind of logic here and again right after that it is accessing the repository on the time of instantiation of this customer service we are creating an instance of the customer repository as well as here this repository so this repository is a kind of like responsible for fetching the data for the customer service here customer repository so the customer repository is the dealing with the data stuff like where uh customer i mean need to insert the data or maybe delete the data find the data all these database operations are inside this custom repository as you can see here so in throughout of this application we are using nosql mongodb as a kind of database or maybe if you are thinking like if you wish to something else to use then you can fairly change here only in the customer repository and customer service once once the operation is done from the customer service customer service is sending some kind of the meaningful data which is going to be like maybe it has some token or it it may have some kind of other information also which is you'll be received from here in apic here this data so that means api will always receive receive the kind of prominent data which is going to be data right once our signup is done then we are receiving this data and we are sending this specific data to 3d to the client or from from the api good and if it has some kind of error then we are just throwing this error and once this is done then again here uh in this express app we are handling this error right here so let's see what are the end points are available uh for this project so then we will we can go to wacky throw uh it's on every api here right so these are the end points are available uh here the customer will be having sign up login profile and customer shopping details and customer ad address customer and wishlist and all and after that this is the shopping api shopping service and shopping service will be having shopping cart and create orders and shopping orders and all and product service will be having product create and product select a product product selected and the the products category product view product detail add two is let's add to cart and delete from cart and remove problem with this and all those things it will be here good and right now we are accessing the same endpoint like luca lost 8000 right so now uh now let's add some um data right here so at least we can quickly test it out this is these are the sample data which is i have added to create this stuff like i'm gonna quickly create the the products here all right so we have created plenty of products here so let's let's go to the in product view here we're going to call this product view and as you can see these are the products are available and these are the categories are available here now we are going to sign up from the customer here let's sign up this one all right so for so far we are using this kind of endpoints and later on we will change these endpoints whenever the macro services architecture it will be on plus good so now uh now let's see like we can see the user customer profile also here there's no any data and shopping details there's no any data oh yeah it's add with this and all those things are here that's good and um customer with this also yes no and products product categories we can call the kind of product category so these are the categories stuff is there then we can put oils here yes it's it's it's working and product view we will see like you know from the product details from here um get product view so this product details we will we'll paste it this product id here yeah it's working and at two is less we need to just need to provide the you know the product idea and i took card also the same way and delete from card and all those things are happening in the same way so these are the end points it is already available in in the api so i'm going to close this one here this one here this one also go to the api express up so a customer app these are the apis are available for customers login address profile and get shopping details and wishlist and the same way for the products also product create product type product get by id and i'm go what i get by ideas and whis list also delete this list card delete card and all those things right here and the shopping also in the same way stopping only the three end points are there which is you can see the shopping orders uh which is we can you can see the we can place an order here with the help of the shopping order and we can get the orders and we can uh see these shopping carts here good so now um what we are going to do here just we will be looking into all those things here yeah as you can see here and so these are the these are the products exactly it's available just like this way products view so i just uh don't go to the ui layout and all those things for um to the front end because i have just to build these things to or to to present you like you know to show the presentation how exactly it is gonna work in the in in the real-time application maybe you maybe you can make it better than this designer or something and i i just want to show the staff early so i'm going to log in this one so it's login once i once i have logged in here then i can see this card is empty which this is empty and order is empty and i can add my uh address also right here and once i go here i can see my products are available once i click on the products here then you can see like you know product category what it is and price it has some quality stuff like whatever the information is available here we can see we can tap on the wishlist and it's edit and again it's removed if it is added to the quiz list then you can just see from your customer profile and i can see this is one one one ids one item is added in the wiz list let's let's try to add some more i'm gonna add one more then we can see here yeah one more edit then i'm gonna remove one and you can see like this is one uh item is remote okay again again i'm gonna add this one then i'm going to add this product from here at right i can increase the product uh quantity also here two three and if i go if i'm going here then you can see there are three quantities are there right and with this also there are these are the two items exactly it is available in this list i can remove one from here remove and order we don't have any orders right and uh if i if i increase the item here then it has to keep it mine here and the down like it will increase the product price also and eventually we can place an order so let's add some address here okay so what i'm gonna do like so it's set right this is my address exactly for delivering the orders to the mumbai so um uh so we are going to uh place an order here if you if you're going here so i'm going to place an order here if i'm going to place the order order has been placed now we can see and the card is empty and we can see this order is available right here so forget about this order id and all those things but still we can see the order has been placed so if you are going to the profile right here the the same thing like see the address is there and which list is there and one product one order has been placed with this item so we will try to do the same thing in microservices architecture and microservices way so let's see i'm going to show you one thing here if i'm if i'm suppose uh something is going wrong with one of our service let's say i'm going to intentionally make some kind of mistake here and here in the shopping stuff let's say this shopping class or something or just i'm making it like such a way now uh now if i'm going to try to run this one refresh now see it's gone all gone right we cannot even do the login also it's done it's it's totally we are right so but the in terms of like microservices if this is happening for the shopping service then still we can access the user service from here and still we can uh view our orders or maybe we can we can add the or you can tweak our um what is called the wishlist or maybe cart or something else right so that is the thing exactly it's it's very useful for microservices okay so let's uh undo all the stuff here all right so this is the time to split our monolithic uh project to the microservices uh in this application already we have a couple of pictures all those pieces we will split up to microservices and those pieces are customer and products and shopping all right so there are three pieces we can split up to uh three micro services so that is we will do here so first of all i'm gonna create one root directory for our microservices so it's empty so we are going to create our services here first of all create a couple of directories that is going to be like so let's get into the let's see what are the directories we have created here customer products and shopping these are the three directories we have created for our micro services so let's create our new project inside these directories so we can run simultaneously to check it out like where are these services will be work together or not so uh first of all i'm going to uh get into the products directory right so and these are installed properly now what we will do here now uh i'm going to install the same thing for other other stuff also other directories also i'm going to ok so i'm going to open this uh this all whole root directory in a separate window so you can see here all right so initially these are the three services we are going to serve again in the same way as you can see here these are the services customer will be having customer products will be having product stuff and shopping will be having stopping stuff yeah that's good but the first we just add some code right here so we can check it out that up and running this uh services listening to a specific port so i'm gonna create one file here all right so here let's add some kind of code and inside the pick is the pick is the session file here we'll put start already we have installed the nodemon here yes holocomb customer right this is working perfectly fine so i'm going to minimize this one here here and again i'm going to copy the same code here and the product also just go and good so i'm going to split this uh this window right maybe vertically that's good here right before customer we'll just go out from customer and again get into the products here all right so npm start yeah it's it wasn't too and again i'm going to split split up this one here um presently and go out from product and get into the shopping mpm start so as you can see we have a three servers are running right there with thousand one eight thousand two eight thousand three right so we can we can try it out here from like 2001 it is running then a thousand two is from products eight and eight thousand three uh from solving but the this is this is perfectly fine but when we are we when we will call the same endpoints uh uh the endpoints from the front and then pronter need to change its it's like the base url right where exactly we have already did this kind of best url okay so if you are going to shopping here then you can see the product customer and shopping base url is the same only thing is like minor changes on the the what is called in the end point it's hoping you will be having that shopping endpoint and after that the disability whatever we are putting right here in the same way the customer will be having the customer and product it's a product will be having the products on that's good so these are the things exactly we will take care right here but how we can take care because it seems like uh to getting done in the front-end side we need to change our endpoints accordingly so for that thing we know we already i have mentioned earlier we are we will use some kind of aggregator as an api gateway right here which is going to be hook up our all the services uh based on the endpoint to different different services that means this aggregator or some service you will it will just play a rule to to redirect or routing the whole stuff based on the end point to different different services that is we need to add right here so let's say create this stuff also very quickly we have there are three pictures we have right so where what we will do here we're also going to create one more directory all right so we have already we have created our gateway directory so inside the gateway directory let's let's create our node project here so npm init why yes so now we will install couple of dependencies that it's going to be like should be inside the gateway and we will we will do the same thing uh okay so we have not installed the nodemon also here and so let it be installed so meanwhile we will create the file all right so let's add some code in this index.js file okay so here i'm going to copy some code from here copy and let's just paste it out here and yeah these are the things exactly not needed but this time we will be listening to 8000 only let's add some more code right here so what this proxy will do exactly this proxy will redirect our request which is exactly it's coming to the gateway and it will redirect the request based on the end points to our other services if it is coming with the customer then it will redirect to customer and if it is coming with the product and shopping then it will redirect the request to the product and shopping so that thing it will do uh by this proxy okay so in upcoming video tutorial i will i will try to post some kind of great tutorial based on the api gateway and how exactly it is working for microservices with the help of the aws and other tools also so uh for a while we will just follow this pattern all right so let's add some code right here so if it is coming to if it is coming a customer then we will redirect our request to our 8001 and if it is coming shopping then if we will redirect our request to local lost 8 000 tree and if it is not coming with any endpoint then uh definitely our root endpoint it will be definitely our products so this is our products products and this is shopping okay serving and customer all right so let's let's start this service also yes it is running right so now uh just have a try from here we will simply put a thousand only then it is by default it is falling back to the products right and if we are putting right here customer customer then it is it is coming the response from the customer and if it is we are if we are putting anything uh related to shopping then it is it is giving the response from shopping right all right so it seems it is working perfectly right so let's add the decodes add some codes to the to our services so at least we can we can achieve the final results just like you know all those endpoints we can start it from here so first of all uh we will get into the customer because the customer is is a kind of uh service which is exactly needed right before uh getting into the product stuff and all because the customer will be the end user which is going to be access dd products and shopping right so inside the customer um here we will just copy almost all the files from here inside the source directory just to copy the source directory copy and paste it inside the inside here paste right it's done now all we need to have this enb file also why this env file is needed exactly because this enb file will be it will be handling that the configuration related to whatever the configuration will be using inside the through out of this application so that is exactly it's mentioned right here so technically what it's happening in the live server we are passing the in a b file with the help of the secret credential management uh tool or maybe we can directly create this enb file on the time of deployment so but for for development purpose we will be using some kind of dot env.dfdb so this file will be needed so let's copy all those files also from here and paste it here along with this source directory i'm going to put it in in root directory here that's good so here uh this is uh this port number will be a thousand one for customer we are maintaining a thousand one here also it's eight thousand one here in the local house that's fine here we will put like and now we're going to fairly remove this this index.js file from here because that doesn't make sense already we have the source code which is we are copying from the monolithic architecture the same way i'm going to copy this one and i'm going to paste so i'm i'm going to paste inside the not in the gateway in the product as well as paste inside the product and place inside the shopping right now here i inside the customer we have already pasted it all the changes products also we have pasted and shopping also it's it's done right but the one thing we need to do here first let's uh simplify the customer service okay here this index file we are just going to delete here from here and now inside the source directory we have already index file right this one and this one we need to run it here so go to the package so inside the config file it will always look for the enb file like whenever we are running our our application then we need to add some kind of code to run in this specific environment so as you can see in the dot enb is taken care of like if it is not a production environment i need to look for specific configuration file and that configuration file will be residing um in the root directory and and the environment file will be having like a dot enb dot or whatever the environment name okay so in our case we have already e and dot enb dot deb so this is this same pattern we'll be following some uh for other services also uh if it is uh what is called if it's a production environment then definitely it will go right here inside the package.json file we'll be add one more code right here just try it out this customer service is working on this one is our customer service and pm around the vlf it should have to technically run on it for 8 000. all right so our and now now start our customer service so these are the dependencies exactly it's needed to install for our customer service the same thing we are going to do uh in the shopping and the products also so you might have wondering like you know why we are using this in different different databases as i told you in the diagram like you know we will be having their uh it's each and every service will be having their own segregated database stuff right the customer will be having customer database product will be having product database shopping we'll be having shopping database the same thing we are we are doing right here see uh this shopping service will be having shopping database which is going to be like solving ms uh microservice msms in terms of like microservice i'm just keeping the subway uh serving ms hopping here in the product service we are keeping the database name is the shopping ms products right and production also the same and for the customers also we are keeping it the same way uh here the stopping ms customer so this database name has to be same in the production as well as come on that's good now now it's perfect i guess right so let's run the application here and we can run it so go to the products here and go to source code database and connection here we'll just simply remove this one that's good and here also i think we need to do the same all right and customer already we did the same i guess yeah customer don't have this one all right so if it is um perfectly running and database is getting connected then it will show these meshes which is called db connected and after that the port if it is listening to report 8002 uh you can see this one this product product is listening to 8 000 right yeah that's good so and the same thing we will do for swapping also and p.m now what we need to do we need to just to segregate our code to responsible to maintain the same specific pieces only right now right now whole piece or in terms of um a whole code we are just pasting right here and it's in each and every service so uh let's get it done by one by one uh first of all we will get it done the customer service so let's let's understand what are the complexity we have in the customer service we have uh these these are the models exactly we have the address model and customer model uh and order and products right but the the order and products model is not belongs to the customer i guess right because the customer only will be having the customer related stuff in terms of like only the customer stuff we are we are going to handle here so as you can see uh the customer sign up with list address and all those things it has to it belongs to the customer only so now now we can see here the product stuff also there which is exactly which belongs to customers so that
Info
Channel: J Gogoi
Views: 5,246
Rating: undefined out of 5
Keywords: Nodejs Microservice tutorial, nodejs monolithic to microservice, node microservice, how to build microservices on nodejs, nodejs, nodejs microservice architecture, monolith to microservice, nodejs microservices tutorial, nodejs microservice api, nodejs api, nodejs grocery, grocery application, grocery delivery application, how to build grocery delivery backend, backend microservice on nodejs, nodejs grocery app backend, delivery app backend, node js event-driven architecture
Id: EXDkgjU8DDU
Channel Id: undefined
Length: 36min 32sec (2192 seconds)
Published: Wed Sep 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.