How Microservices communicate? API BEST practices | Java | Spring Boot | REST API #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] [Music] hello everyone can you guys hear me can you please confirm yes good morning Felix good morning everyone [Music] all right we're all set right cool so let's get started let me start sharing my screen okay you guys can see my screen right can you confirm yes yes is this a good yeah visible fine okay so let's get started so what we will be doing today so we'll be doing we'll be doing some fun today exactly not only fan client will I'll tell you about I'll tell you around three clients to you now this is going to be your choice which one you should be using but I'll be recommending you one right so in this lesson we will start learning about how we will be communicating with different restful web services that we have created in the last session and what are the different options that Java provides us so Java provides us actually various different options right can you guys tell me few that you know already let's say I'll tell you to make a rest call using your Java application so what you will be doing and how we'll be approaching this particular problem yes please rest template right so this is 2022 still we are using rest template so astronomic set they are using rest template so you know what from Spring 5 rest template is kind of deprecated and they are telling us not to use it but that doesn't mean that you should not learn rest template because still there are projects which are heavily using rest template so rest template is one of the best option to go but I will tell you not to use it I'll also tell you the alternatives for it because in future if the rest template will be deprecated and of course it can be deprecated at any time but so you should be knowing why rest template is getting deprecated so this should be one topic that that we should learn and we will be learning it today and also there will be one more thing you got to ask yourself if rest template is deprecating then what we should use okay so I'll be telling you the alternative Forest template as well and as you guys know in the last session also I have discussed that open fame or the faint client just like you have rest client just like you have web client like that you have a faint client and that has been provided by the spring Cloud projects and that will be pretty much beneficial if you are using microservices and if you're communicating um with different micro Services fin client is also one of the best options to go for so with this discussion like we have a rest template with which can work as a rest client using what you can make rest calls you have a fin client provided by Spring Cloud using what you can make rest calls you also have an option called Web client using what you can make rest call it's not only rest call you can make you can you can also make asynchronous rest calls oh big words right I'll tell you you know what what I mean by all these things but this is what we have done so far we have created a employee microservices in the last class and we have created an endpoint slash employee slash ID we have deployed it into its own server and it has a database all right and we also have created a address uh service and basically it has a endpoint slash address slash employee ID using what we can basically look for a address based on a employee ID and we also have deployed it into a separate Tomcat server and also practically the address service should have a database of its own but in relations that we have you know practice in the last class we have just used one database for both of the microservice but later we will be improving our code we'll be improving the database structure of our application as well and this is what we are aiming to do right now we are basically planning to make a rest call to this endpoint which is one of this last employees last one where one is my employee ID and now this should be giving me not only the employee data also the address data right so this is something that we should be like you know developing today or implementing today in the last class I think we got like whenever we are hitting this endpoint we were getting the employee ID the name the blood group and email and today our Focus will be to get this address data whenever somebody basically hits this endpoint and make sure that this address data that you have over here will come from a different microservice call or different rest call we have to make because this is one server from one from this server we need to make a call to This Server now to this endpoint to get the data from this database and populate it over here but yeah as I said in this election we are just focusing on one database so we have data in one database that's fine but whenever somebody makes this call what we will do we will not coding in the database rather whenever somebody makes this call we will internally make a another rest call to this particular application to this particular endpoints and this is gonna get us back the data and the same data we will be returning to somebody who have hit this particular endpoint in this way okay so I think we are all set our stage is set so let's just get started with our implementation so I will be going to my spring uh you know uh applications the couple of micro services that I have created first of all guys if I'll be starting my employee service so let's start my employee application which is a micro service that we have created and right now I can also imagine that this is running in one Tomcat server because you know that the moment I started automatically the code has been deployed into a tomcat server and the Tomcat has started on port number 8080. there is no context path right now has been set but we will be setting one and also right now imagine I have another server where I will be basically using this particular service so if I'll start it right now if I'll run it as spring boot app do you think it will run or it'll fail and if you if and if it is going to fail then what is going to be the solution for it and why it is selling you I think you need to change the port exactly because the employee service by default is running on port number 8080 and whenever we started this service is also started on port number 8080. so let's just do a few uh you know Hands-On right now first of all let me just stop the other server as well so first of all let me go to the employee service and also the address service and will do configure the uh you know Port information so I'll go to the resources of the employee application and I will open this one and right now in this properties file let me just uh first of all let me change the port number how to change the port number server.port let me say that employee application will run on port number 8080 and I will also give and give a name to this particular app for what I'll call to this particular app I'll say uh application dot name sorry application dot name I'll tell it as employee app let's say and also let me just set a context path to this and what do you mean by a context path by default whenever you are hitting an URL you are doing localhost 8080 slash employee slash one slash employee slash one is basically your endpoint or URL that you have created in the rest controller right here in the controller this is what you have created right so if I want to have a context path to make it more you know real time like employee API or employee app slash API then this is going to be my context path right so this context path let me also configure it over here in my application.preportees file and how to set a context path just write context path here right and hit the first option and put your context path right here okay so we have this three information let me copy out these three information and let's go to our other application which is our address service and also let's open the properties file of that and let's configure that application to run on a different port so I will go to uh open with and generic editor and right here right now I will be pasting it and this is going to be my address app and it is going to run on port number 8081 and I I will say it is address app slash API is going to be my context path let me save this let me save this let me close everything let me collapse everything let me start this application right now run a springboard app so I'm starting my address application right now and this should start this should be started on port number 8081 and right now it has a context path set which is cool because this is what we have done and also right now we will be running our employee service so we have right now couple of a couple of our micro services that has been up in this one is up in 8080 and this is our employee app slash API which is the context path so right now if I'm gonna click on off click on of this option now I can see all my microservices are right now up this Arrow you can see right now the services are up in port number 8081 and this is running on port number 8080 I hope this is making sense so right now let's just test this out before we do anything first of all Let Me Close let me just copy this endpoint first of all and can I close everything I don't want to save anything anyhow how many tabs I have close force close current APP force close all tabs Force Clause okay now I will open a new request and this is my employee app I am running right now you can look at that I am adding my context root over here and my employee app is running on port number 8080 and this is my endpoint that we have developed yesterday and whenever I'm doing a send I'm getting back my employee information I will create a new tab and write here I'll copy paste the same URL so that I don't have to you know type it and I will test my address app right now 8081 port number it is running address app slash API slash address and for number one employee I want to get the address of number one employee and there you go so I'm getting my address result right now I don't give a damn to this micro this this endpoint right so imagine this I'm I'm just closing it I'm not going to save it anyhow don't save so imagine this is your module this is the module that where you are working with right now your project manager said that hey okay you are working with this module so right now do me a work or do me a favor whenever you are working with this application and whenever you are hitting or developing this endpoint called employee controller right now whenever you hit this endpoint right just do me a favor I not only want my employee response here I also want the address data okay so let's say imagine you have the private address response do we have the address response inside the employee service I think we do not have it over here right so yeah because this is a different app right now imagine you have you will create a class called address response I will not create it anyhow I will copy it from the other one that we have created in the last class address response I'll copy this guy imagine you forget about this guy you can think like this is someone else is working on this particular service you are working on this service so now I'll go to the responses I will design a response here right now I'm not designing it I'm copy pasting it so imagine you you created this particular you know address response pojo and inside your employer response this is the response object you are basically returning whenever somebody is hitting this endpoint and this is the data that you used to get right now but as your project manager wants you to have the address detail of an employee you want to have it so address response and address response here right so right now you got the address response let me have the getter and setup for this one and right now do a control s and your changes will be automatically reloaded as we have the spring boot tab tools installed and now if you want to click on send now you've got the address responses now now your job for this lesson is going to be you are going to basically fill in the data for the address response so how you will be achieving that and make sure that imagine right now you are working on this employee service and this employee service is a all total micro service in this project what I did I have created my address and employee both inside one database schema and both inside one database but imagine the scenario that I have shown you previously you can think like right now this is our example but you can think like if you have some scenario like this you are working on this module you have only I have access to this particular database and inside this database only employee data are present not address data present address data are present in this database but this is whole total a new a different set of application and it is a third party API or some other team is working on this and they do not want you to touch their database so if you want address data what they are saying okay if you want address data in this application you do one thing you call this endpoint and this endpoint will make a query internally and will get the data address data from this database so you said okay so I will be doing that so what you will be doing right now to fill in the data right here where you are where you are basically returning this particular object you are returning this object when somebody makes a call right here can I minimize this can I zoom this a little and can I bring this down so that I have more space to work with and look at that so right now this is the employee response I am returning right and this employee response has a new field right now called address response that need to be filled with data so right now this data where I'm getting I'm making a service call here you can see employee service that I have defined right here and I am making a service call right here let me go inside my service application service class and right here you are making a database call and you are getting this data from the employee repo which internally makes a database call and you are getting your employee data right now whenever you are building out the employee response here you are basically setting this employee data that you are getting an employee has only this much of information blood group email name and ID all right and this is what we had right there blood group name email and ID but right now this information the employee database does not have right you are not getting the employee address related information right here so what you will be doing inside this employee response your job will be employee response that you are returning here in this you need to also set the address data okay and how you will be setting the address data right now how you will be setting the address stuff this address response this this information where to get the data for this okay and you will be getting the data for this either by making a database call or right now you have no option because you have no option to connect to your employee database so you will make a rest API call and what your goal will be your goal will be like you know the address response data let's say add what is that a Ctrl shift or address response address response new address response and this data you will be bringing in and you will be setting it over here but right now the address response is the annual object it's a fresh object now we need to insert data to each of his field that we have like ID Lane 1 Lane 2 Jeep State this data how we'll be getting it for this particular employee that we are getting okay let's say somebody is giving the ID as one we are finding the id1 employee details but how will be finding the ID 1 address detail as well for the same employee okay so this is something right now this address response I'm setting it over here but now this address response data we should also set now our job will be for to this address response object that we are creating over here set data by making a rest API call okay now if you're going to control s obviously you can see right here all our data should be filled with zero zero zero zero zero because obviously you know this is a phrase this is a empty object that I have created there and set it over here because just to show you that right now everything is null now we need to fill in the data for this response and for that let's make a rest API call and it's some mixed say it right now we need to have a rest template to make a rest API call so rest template coming from org.spring framework web.client and you can do rest template and right now you can make it auto weird so Auto where and right now we using this rest template to set the address response here you need to get the address response data right so to get the address response data you can have a rest template dot you need to make a get call get for object why you are making a get call because how you will be getting the data you will be getting the data by making a call to this and this is a endpoint that we have in a other application which is a get request if you don't remember it go to the address service and inside the address service we also have a controller and the third party people will give you this particular endpoint that hey make a call to this endpoint if you want to get the address related information just the way that we have done previously we have previously made a call right address call and that data we we want right now uh so basically this data right now we want go over there Ctrl V make it 8081 this is address app and employee one send now okay address API or address one address is my API endpoint and this is the ID of the employee and still we have a problem 8081 address API there is a typo here do Ascend now this is what we want so this is the end point we want to call okay this is the end point we want to call you can see this is the complete endpoint this is the this is the endpoint that we have inside the address app and we will call to this so very simple copy this URL go to your employee application now use save everything and apart from this you close everything close others okay now you just collapse everything now where you are you are inside the employee service right inside the employee services we have created our rest template we are going to make a get object call so we're gonna do get for object and right now use one of the method even though you don't know rest template just understand this it's pretty simple you I have AutoCAD the client and let's use this one and here we need three parameter one is going to be your url another one is going to be the response that you will be getting another one is going to be the URI variables right so right now what is going to be your url your url is going to be I have copied it this look at this the URL is going to be this now this is going to be dynamic so this is going to be dynamic ID okay this ID that we want this ID this particular variable I want it to be replaced with this ID that we are getting if you remember this particular method is our service method and it is getting called from your controller so whenever this service method will be called from the controller controller will capture the ID from the URL and give you this ID that ID will be capturing here and your goal is to replace this ID over here and for that you just put this ID over here now whatever ID someone will give that ID will be replaced with this URL okay and at last you will be getting a response back so what response you need you you know that whenever you're gonna be hitting this end point what you will be getting you will be getting this okay and this is what this is what your address response is and you have already designed a class for this so you will be just writing here in the third parameter your response object that you have designed to convert those Json data to your Java object so you're going to be doing address response dot class and right now this guy is gonna get you the address data so assign statement to a new local variable but for now I have already created a address response I can directly assign it to this so instead of creating a new object like this I already have created a object so let's just assign that to this okay and once you assign the data to this address response set this address response with the employee response that you are returning and this is the employee response you are returning over here and to this employee response right now you are setting the address response let me not make it complicated let me just remove some lines of code from this so employee here you are getting your employee here you are converting your employee to employee response and inside this employee response you got a address response we need to save this data by making a rest API call now here we made a rest API call and we will get the address response here so I'll say address response why we are getting a address response because this is where we are mentioning whenever we are making this API call we'll be getting this address response back and now once we get this address response back we'll be setting it over here with our employee response and we're returning back the employee response right now from where this particular method is getting called do a control shift I oh sorry Ctrl shift G and right now from where this particular method is getting called from the controller click on this okay and right now you are moving back to your controller now from this is where the method is getting called from your controller and it is going to give you a employee response back and this employer response you are sending it in your response body uh whenever somebody Max a rest API call using this so right now if I'm going to control s and I think let's see that let's see our console first of all let's go to our console and you can see our employee app Employee Service application is right now having a problem why because there is some problem is happening in the employee service because the required being of the rest yeah rest template is not configured as I said you from the spring 5 rest template has been deprecated and there is no Auto configuration is getting made for your rest template right so what you will be doing uh basically this rest template um it will not be automatically configured for you right because of the auto wear annotation for this res template injection is not happening because spring boot is not automatically configuring it for you so that's why you got to configure it and there are a couple of way to configure your rest template one way I have told you during your spring Boot and spring framework course and one way I told you I'll be telling you guys later the first way you guys know you will be going to the configuration file and inside the configuration file you'll be simply creating a beam and the way you'll be creating the bean called public rest template and a rest template okay and right here you will be making it a bean and right here you will be returning a return uh return a race template object new rest template and there you go you have created your rest template object right now do simply a control s and right now your application should your employee service application should work fine and started on port number 8080. now if you're gonna go to your Postman click on the send oh sorry we are in the address part right let's go to the employee let's hit this endpoint let's hit this endpoint the employee employee employee employee controller this endpoint if you're gonna get and then you will be getting back a employee response and in this employee response we have right now set the address response as well so along with this data we'll be also getting this data and this data we are setting inside our service class with a rest call right sure if we're gonna say it in detail this is where we are getting it through a rest call and this is where we are setting the data with our employee response that has been returned by your controller to enter and now all this data has been filled in so look at the Json and just let me know if you guys get the thing guys clear yes okay any questions so far anyone any questions so what happens at this service is blocked address Services down yeah then you have to do retrying and that is entirely a different concept one is what we'll be learning later how to create fallback and this is one of the reason why often pain is more useful because it that is more easier to you know configure the retrying mechanism when a address service is down how do you want to give a response back in that case this response will like you know this will be giving you problem this will be giving you exception and if some service is down if the address service is down whenever you are hitting that service you are not getting back the data there are many things we can do we can keep retrying for few times we can we can have a fallback page we can do certain thing that we'll be learning next okay so please hold on to that Manis for next two three classes when we'll be talking about circuit breaker resilience we'll be talking about all those things okay yeah any other questions so if I'm talking about exception handling yes yes let's not talk about exception handling right now we'll be talking back in the resilience 4J and whenever we are discussing your uh what we call that fall back fallback mechanism and retrying mechanism yes yes I'll be telling you right now I'll be telling you right now why sunivas right now for now I'll tell you why it is bad okay but more thing about this like how we'll be configuring I'll also tell you how to configure reactive uh stuff here right now but more thing because if I'll start talking about reactive that's it entirely different thing I'll tell you right now what's universe is asking what is the question is all about just in a minute okay but before we go for this just remind me after five minutes Universe again so before we go for this once like you know let's just try to improve our code this is pretty bad coding guys okay let me let me not configure the URL like this okay so now let me just keep it in a properties file first of all so what and I'll tell you another way of configuring your rest template so if and one more thing guys this is 2022 uh which month um September this one August almost done we are in 2022 guys I'm recording this video in 2022 whenever I'll have a new recording with a micro Services patch you'll be getting it but I I suspect that whenever I'll teach next also I'll be teaching this template only first this is this is deprecated from Spring end but it's not like you know you duplicate something and people who are using the application which has been built years back it's not like you know there will be start changing their code immediately for many companies rest template is still doing wonders they are still using rest template they are not in favor of reactive because they don't need it but yes why okay why I'm calling this word reactive again and again um I'll tell you the reason but yeah just just beard me uh for five minutes and I'll come back to this particular topic but yeah you also got to have a clear understanding on rest template I told you one way to make a get request call just like that you can make post food delete I'll not go into that right now I'll not make people confuse on that but yeah rest template is separately a different topic and you can also learn about that in case your company are using it a company people are using it okay but for now I'll tell you to in a good way to configure this thing so this is actually our end point we are hitting but this is nonsense this is our best best URL we don't want to let's say I'm making 200 rest API call in all my service class I have 200 different methods where I am making service calls I don't need to copy paste my URL every time whether I can go to this employee service and I should be having my properties file here and inside my properties file I'll open the file with with a generic editor and inside this editor I will be basically setting off this URL with a custom property and let's say I'll say student what is this not student what is this employee Employee Service dot base dot URL and this is going to be my best URL and after this URL only something will be configured like employees or employee slash one or employee Slash 2 or only employees slash something slash something so this is going to be my end point but this is going to be my base URL right so my base URL is this so I will be taking this guy right here and it should be addressed yeah that yeah Base address only and you can also call it URL it's the root URL or URI isn't it Manish yeah it is exactly so right now uh we will go to our employee service this is a [Music] local no no no we have configured this guy no here context path yes Employee Service they say it's an address service dot base URL oh sorry sorry exactly it's the address oh sorry sorry yeah yeah when is I just got confused because of the address word right right you guys are right yeah address service.base.url this is my address URL so right now thank you so let me uh this application that properties belong to employee services Employee Service only employee service only I'm here okay okay because in the employee service I am making a rest call to get my addresses isn't it so whenever I'm making a rest call here in my employee service which is here which is here inside the employee service application right here where I'm making a call I am making a call here inside the employee service right here I'm having my rest template and in my service method I'm making a call here all right so sorry making a call here so here only this one will not work right we need to also concatenate our base URL so that base URL right now I will be reading it through a empty value and you guys know it how to do it just write add value and then you just keep your address URL inside this guy and you just wrap it off with a question mark sorry uh with a column press and you put a dollar sign over here because this is a dynamic value and you inject you just read this value right here and inject it insert this value to a private string address base URL or address root URL whatever you say okay address root URL and now this guy is just concatenate right here okay there we go to a control s let's see that whether we are able to read the value and application started fine looks like it does let me have a call right now and let's see that whether it is working fine and it's working good and right now can we still improve it can we still improve it this is concatenation can I set my best URL somewhere to my rest template so that I don't have to concatenate again and again yes you can still can you can do it so guys you know what where you are basically setting off your s template right now inside your configuration class of your employee right Employee Service just right here okay inside your employee service inside your config class you have configured the rest template if you are not going to configure it is going to tell you like autoware fail wherever you are Auto wearing in your service class right share and it will not work right and right now I'll tell you another beautiful way to do it so instead of Auto wearing this one right now I will be Auto wearing this guy through a Constructor okay so how I can do it so right now this rest template also there is another way to configure it right now for my employee service I am going to configure address template and for that do a control space have your Constructor for the employee service create a construct doctor right here and inside the Constructor you initialize your rest template right now the address template is null isn't it this is null now let's go ahead and initialize it and if you want to initialize it there is another class here called rest a rest template Builder so let's try to do that rest template Builder and Builder and you guys know with the name right it's build up that means it's going to help you to build something okay what it is going to help you to build it you can just do build off Delta dot build and you can see this build is going to give you back a rest template object so this rest template right now it can initialize you can just say this dot rest template is equal to Builder dot build okay and right now this is going to be done right this is going to return your estimated object and obviously this particular Constructor will be invoked right now by the spring whenever it creates a employee service it will be invoked to create a employee service bin and whenever is creates this the rest template Builder this particular guy you are having in the reference and manually you know initializing the rest template now let's see that whether our application starts are fine and looks like it does and right now if I'll go to my Postman do a send and it's still working fine okay now one good thing that you can do is you are using a builder to configure your rest template wherever you are making your rest template call right here you are doing a rest template dot get for object you are concatenating your address base URL plus this is the URL that you are concatenating now this base URL you can simply remove from here okay you just copy this guy here and remove this plus go to wherever you are configuring your rest template with this Builder you have a another Builder method just do Dot and maybe that is root URI yeah root URI and here you just provide your best URL okay now what you are doing your for this service called Employee Service you are building a uh you know rest template right here and for that for this rest template already there is the address URL configured address base URL configure and this is basically your best URL that you are reading from your properties file which is this one let's see that whether it is working but this will not work why let me also print just let let you know about why it is not going to work let me also print the URI can be rebooted configuration of press templates already removed already removed okay Cinemas Cinemas this is what you are asking yes yes I have already removed it yeah now let's go to the employee service write your we are right now configuring our rest template here let me print our URI just to make sure that whether our URI is getting the value if I'm gonna do Ctrl s right now you will see let it restart you can see the URI is null why because whenever the service is getting created whenever the service is getting created that time it is using this Constructor and by the time it's used the Constructor to build the rest template this URI value is not getting loaded so this this way you cannot use it so this way this is why this is null and if you are setting the base URL as null whenever you're gonna make a call to this the base URI is not here so you will be getting maybe some exception maybe some URI not valid or absolute URI or something URI is not absolute because that URI is null so what I can how I can fix it basically in this way you can you can right now do one thing you just copy this guy copy this variable Ctrl C instead of that you paste it here okay and the value annotation you use it before it like this okay so from the non-static variable or the instance variable it will not be able to load it rather you just use like this and now I think you the best URI URL will found it will be found yeah you can see we're able to capture our base URI from the application.putes file whenever our Constructor is getting involved and now I can simply um you know remove this this out because this is just there for logging and now look at the code what I have it over here is pretty simple I have this class called Employee Service I am creating a you know uh what we call this Constructor for this and right now first of all I'm getting my best URL loaded from the application dot properties file and also I am having my rest template Builder because from this I'll be initializing my rest template and I am doing Builder dot build and also the root URI I am reading it from this variable which is basically right from the properties file and now with this I am setting up my rest template client or rest client for my employee service application now if I have 200 methods doesn't matter right this should work right now I just can say rest template Dot get for object push for object I can make get calls post call and all the calls we will we will make like this this will go to where wherever we have the base URL setup like now all the uh all the call will be appended after this okay this will be appended after this whatever you you will be writing it will be appended right after this particular thing I hope this is making sense now if we have done our setup correctly this should work fine that was not working like when you have declared that because because because this value was not getting loaded by the time the Constructor executes The Constructor execute first because see uh basically first of all let me let me let me first run this then I'll come back to this to send and I'm getting my data things are working fine done period full stop okay coming back to some mix question now yeah the thing is whenever this particular component will be created this is what this is a bean all the beans will be Auto aware but this value will not be auto air this is not a bean right this is that a value that we are reading from the properties file and this will be done after the bean creation and right now uh you know what we are doing we we are basically setting up this bean and whenever we are setting up this beam there is no default Constructor has been found so spring will use this Constructor to create the bean and at that time it is basically creating our rest template instance and it is looking for this particular value and by the time it is looking for this particular value the value is not exactly head so it is not so it is null so what I have done whenever this Constructor has been used at that time then only you load this particular value by using the add value inside the Constructor only right making sense so make okay cool but we will not be using yes yes yes obviously whenever you run the Constructor only the auto adding will happen right now we are not basically we are basically autowering everything all these things basically created by your spring boot only right or springboard and this thing we have configured manually so nothing is there here we are manually configuring our rest template object by doing builder.build because the build method will give you a rest template object and I'm creating it manually but the problem is whenever I'm creating my rest template object I also want to set my best URL with that so that I don't have to concat it again and again and that's why to load this one I need this particular value and for that I have used the add value over here making stem stomach okay now the question now let's come back to srinivas question right and [Applause] for that you will have for for that there are two options you can create a different rest template for that you you can create a different service for that department service or something isn't it yeah yeah so for right now this approach only use Santos because for each service we are setting up one rest template okay now if you have one more service inside that Constructor the setup that department service base URI over there create another slash template for that Santos make complicated yeah a client the red this is a rest client it will make more sense right now we'll start discussing about the thing but for each service that you are creating you are creating a department service set up a different rest template for that set up your best uh you know URI accordingly just the way we have done it for employee service you will have your department service Constructor inside that you set the department best URL Santos yeah okay okay now coming back to the questions why it is bad why we should not use it why the heck I teach you this one if you are not using it yes can you go to that address response address [Music] yeah in the address controller let me first of all come out of this let me go to the other application you are asking about the address controllers and here yeah ask me entity right exactly exactly so you want to get back the response entity you can you can do it you can absolutely do it whenever you're gonna make a call you can you are making a call make it rest if you want to capture the but I told you how to get the response entity how to get the rest response entity using the rest template I told you there is some exchange method do you remember yeah get for entity method do you remember let's not go into that instead of get for object use get for entity right and this is gonna get you back the response entity let's not mix it up this is gonna confuse people okay we have already covered it so now spring springboard hibernate uh bootcamp okay uh yeah just refresh those Concepts but no need to refresh you know this much will be enough tell me guys so far everyone is clear or not or there is any questions ask me first then we'll go next so far things are clear or not clear or I made it complicated okay can I get some other um you know responses Kiran uh Lakshmi player player no my word good okay fine fine thank you thank you okay exactly so it is called multiple times then the irrepossible yeah it will yeah if it will be called multiple times where y1 to create objects manually of the employee service you are giving the responsibility to the spring to create the employee service object so there is no need to call the employee service Constructor and in case you want to create a different Constructor for different purpose you overload The Constructor and have your implementation right there and whenever there is a need to call the employee service Constructor you use that Constructor to create the object for that okay but for now in Spring basically we we're giving the object creation uh you know functionality to Spring to deal with isn't it hers yes yes exactly but if you need more Constructor you can absolutely can create okay but this Constructor I'm reserving it for the rest template Builder but I can overload the Constructor if I have my needs yes absolutely you can use the model mapper but I am directly getting my address response back so I am setting it with my employee response I'm not getting an address object I'm getting address response object isn't it yeah exactly okay now let me close everything if your doubts are clear let's go and understand why rest template is bad why we should not use it what we should use if you get if you have uh 15-20 minutes of more time I'll tell you not only this I'll tell you what is the solution for this do you have 15 20 minutes more I need an answer from all of you then I'll continue we need the solution solution also I'll give something yeah so and one more thing guys tomorrow uh I have a call with with my YouTube manager so I will not be able to attend my session so I'm also okay for extended session tomorrow but I will not take more time if possible I'll record it around eight o'clock I have a call with her so I thought it is today but it is actually tomorrow eight o'clock so yeah so she will be giving me some KT on something maybe I will be there so I will not be able to take your session tomorrow so the next session will be on Wednesday and it will go till Saturday okay next week so this week what we can do right now if you have some more time I can sit with you because I don't have to go to uh you know Bank to drop my sister so I have time because we have some strike is happening right now here so I cannot go out also so if you have you know 20 30 minutes we can cover up the rest template and also I'll tell you about a little bit about the web client so all of you are okay if somebody has office you guys can drop also and you can hear out the recordings you have three four days of time and I'll get you some of the assignments as well okay but if you have some around 30 minutes then I'll cover open fan the discernment is of the rest template and also uh what is the solution of this so what you are telling me just tell me right now and accordingly I'll proceed yeah please sir okay how many thumbs up so how many can get me a go ahead like okay fine okay not bored huh you're not bored right fine okay thank you aparna okay okay thank you Anusha cool cool all right all right thank you let's let's do one thing uh let's let's just try to understand okay why it is bad right now let me have some water first do we have a class on Wednesday it is uh uh song uh [Music] uh Lakshmi um difference Festival on 31st yeah okay so okay let's do one work then Cinemas I'll let you guys know uh maybe we can have a class on Tuesday okay then we'll have Wednesday off I I let's let me see if I can schedule my this schedule my client calls okay I'll let you guys know okay Lakshmi Tuesday will be fine right I hope so Festival is this is okay okay okay okay I know okay my mom my mom would have killed me if you heard me like you know I don't I don't I don't understand this Festival name but yeah it's something it's a little South name but we call it Ganesh Puja Ganesh chaturthi okay cool all right so so okay coming back to the class okay now let's understand why race template is bad why it is bad why we should not use it there should be some reason right and why I'm telling you for microservices don't at all use it you use yes the other way is I have already told you can use the Constructor um okay that Builder yeah there is also one more way using a customizer rest template customizer and uh let's not talk about that uh okay two-way are fine okay I know you know you will not be using it pretty much so let's first understand right now why rice template is bad and why it has been deprecated from Spring 5 and why spring people are telling us let's not use it use something else even they have given you an alternative for that and what is the reason behind it and as the world is going forward to the micro Services architecture they are telling us hey don't use rest template this is going to block your thread what I mean by that so let me tell you with some example first of all guys you know before I before I draw something here I'll go to the service class okay I will expand this up to a control s okay maybe it will preload the changes library and that's fine cool so now let me tell you guys why rest template is bad so right now can you tell me in this service class how many calls are going to external server now we are inside our Local Host how many call we are making how many DB call we are making how many external API call we are making inside the service method we are inside our Employee Service how many DB calls are here looking into the code and which line we are making a DB call anyone 45 and 47. code number 43 right number 43 this is which call DB call and where we are making a external rest API call um 47 yeah 47. 47 here oh sorry 47 here this is where I'm making a external rest API call right so here we are making a DB call let's say our DV is giving us response in 10 seconds then what will happen whenever the code runs whenever the method execution reaches here inside the get employee by ID whenever this this one is you know this line is getting invoked and your DV is responding after 10 seconds then the code will be here for 10 second or it will be moved to here don't move it will move to here it will not move to here it is synchronous it is synchronous is the first thing second thing is that after only the DV call completes you will get the data then only the code will move to here after this lines of execution this will go to here after this line executed it will go to here the code goes step by step right and what about the D week call that you are doing your database server is slow and is responding back after 10 seconds okay and then what about here we are making a rest call in the line number 47 right here you say external rest API call you are calling to this address using a rest client what about this is also taking you 10 second okay then what will happen first this will be called blocked for 10 second got the response now this line will be executed now this line will be executed again it will be blocking for 10 second because why because this end point that you are hitting the call is going to a different server as I have told you if you remember the keynote yeah see you are from this server you are making a call to the other server isn't it now look at this this Arrow Mark you look at this this is a different server this is a different server now whenever you are making a call there might be a network latency isn't it the might be the network can give you the response back after 10-15 seconds then what you will be doing okay so it's simply blocked for 20 seconds now do you imagine the value of 22nd and why I'm saying you that let me just prove it with something let me just go for a annotator and let me just go and open up my whiteboard okay and now let me just tell you something guys you know what happened basically guys tell me this is your client imagine this is this is a client okay um first of all let me just decrease the size of my brush and let me just take a different ink imagine this is your client and this is your Tomcat Surfer okay this is your Tomcat okay Tomcat server right now inside the Tomcat server your app your app has been deployed your application is deployed here okay so the client will make a request to here to the server and the server will right now process the request okay now imagine now imagine uh one thing how this request has been processed now client has make a server call now basically what happens after that how your request basically processed from here anyone when a client makes a request to the server how the server takes up the request and process the request any idea exactly it will not create a thread AI it's kind of create a thread because internally we have a thread pool I would have told you this thing internally exactly every server will have a trade pool imagine this is r113 and these threads are going to help a person or help a client to process a request now one request is coming so one trade will be used Imagine One thread will be used and using that thread the request will be go to your server or the server will process the request and this request right now imagine there is some database call is happening there is some DB call happening now this is the thread which is going to take the response the responsibility to capturing the request processing the request getting a response and getting a response back to the client and once the response comes back to the client then this thread will be deallocated like you know this thread which has been attached to this particular client right now this will this is this is done with the responsibility now it is going to come back to the pool right and then it is ready for taking off a another request so basically the way the things happens here whenever there is a request comes to your server your server will be having a bunch of threads okay let's say thread one three two three three three eight four like how many threads and you cannot have thousands threads you cannot have two thousand threads it's not possible so let's say in Spring boot application by default there will be 200 threads can you maximize it can you make it 400 yes can you make can you make a three thousand no you you your application will go out of memory okay so you have limited amount of threads inside your server and right now you think about a scenario okay now imagine these are some threads let's say this is thread one this is thread two this is thread three this is thread four there are four threads here okay now as I said imagine that is your there is a database here okay there is a database here okay now there is one client is hitting a request now let's say this request has been go to thread one and thread one has processed the request and is getting some data from the database now once the database will give the data back maybe after five seconds Ten Second 20 second 30 second it doesn't matter it will simply set ideal the thread will be IDL and it will sit like like it will not do anything and once it's get a response from the database then the thread will get the response to the client and then only the thread one will be deallocated and ready to take off new request okay this point is clear what I am trying to say yes or no tell me or any questions guys I need a response yes no don't don't put it on the chat just tell me yes or no yeah okay yes right now next thing right now next thing it might be a database it might be a database or this might be a external server external server in the sense this might be a restful application also right imagine there is instead of a database we have a rest API we have a rest application this is your rest API some third party API imagine this is your address service okay this is your address service okay now this is your imagine this is your employee app server this is your employee app okay now you are querying for address now imagine somebody has centered there is a new request com then one trade will be allocated to this the thread 2 will be connected to the rest API now this is going to give you the response back after 10 seconds now this trade will just sit ideal okay and if it is going to sit ideal for 10 second for 10 second this will not take up any other request it will just simply sit ideal and after 10 seconds once you get the request it will leave the response to the client and then only as job is done and then only it can take off new request and if there is another request coming then only it can take a new request only once is processed the previous request now this leads to the problem this leads the problem now imagine what is basically happening now okay once again now what is basically happening guys if I clear this off you might I'll give you an instance right imagine this is your Tomcat server okay and imagine you have right now four threads you have okay you have three threads imagine you have a thread pool of three threads now in like you know in in springboard I said you have 200 threads but for an instance you can think this is your Tomcat server you have a thread pool of three threads okay that means right now at a time right now there are three concurrent requests are coming at a time to the server first request second request third request okay now this is let's say this is your employee service okay and this is your employee service and this is your address service this is your rest app rest stop and this is your address service okay now you think what will happen now somebody is hitting slash employee slash one somebody is hitting slash employee slash two and somebody is hitting slash employee slash three this client is wanting the employee three data there is a client He is wanting the employee two data there is a client who is wanting the employee one data and all these requests are coming in same time in a particular time only it is coming now what will happen there are three threads no problem thread one picks it up thread two picks it up three two picks it up and now what will happen these threads are gone once again now what will happen these threads that we have inside the pool is gone why because everybody are right now occupied right now what will happen now the thread one will process the request thread 2 will process the request and call to the address service 33 will process the request and call to address service why because the employee will get the employee data as well as the address data of that same employee and for the address data now my employee server is depending on another server that we have where our rest application is deployed and this guy is giving response after 10 seconds now all these threads are right now sitting ideal isn't it these are right now ideal they cannot do anything now imagine this is avilash okay this is me I am another client I am hitting a request EMP slash 4 and the request is going here now what will happen now tell me what will happen you guys tell me no what will happen right now so what will happen exactly so simply this will not be processed simply your application will not give any response back because your server is not responding your Crosser will keep on rolling keep on rolling you know there are lots of website where the process keeps on rolling okay they will not give the response back because they do not have a good server so whenever you are paying to someone you are paying to a vendor you want a good server where like you know the server will lessen the server that you get is right now 1GB of RAM and there is two core CPU and let's say you got a configuration like this right and you got let's say 40 threads now that means at a time 40 threads can handle 40 concurrent request all right 40 concurrent request is coming so 40 threads will be used no problem but what will happen when the 41 request will come and that time the this particular threads are going to be used and blocked by the rest calls that you have made and waiting for a response from the rest restful API call that you have made and that time the the new request which is coming in they will have no threads to process their request and you know this will keep waiting till one thread is getting free so that it can be allocated to this request and with this idea now look at this okay I'll tell you the solution for this okay maybe maybe you you okay you might some some smart people will be there because I will be getting this questions from different uh you know people they will tell me what you know what I will ask you told me about scaling out a application for next for an example guys I'll give you an idea right now imagine okay uh do control s so you said avilash this is where now can I can I can I can I minimize it now you told me what this is where you are making a call right you are making a rest call here in the line number 48 right here okay now this is going to be blocked for 10 second you are saying okay so cool no problem so let's say there are 30 requests are coming okay my server is not able to handle it so I am right now building micro Services only right this is not monolithic application uh so you know it it is not heavy in nature what about I'll create 30 instance of this or 10 instance of this and I will be deploying this particular service in 10 different server how for an example you know for an example if I go to employee service right here you know I have given let's say I have given port number 8 as 8080 that means port number will be 8080 only so right now if I let's say right now there are two applications are running right can I go over here see there are two applications or employee service is down right now let's say I'll start up the employee service now see if I'll start the employee service it will be starting in port number 8080 now let's say right now I want to I am having more load I'm having one more instance of Employee Service can I run this application again do a run as application it will not work okay right now port number eight zero eight zero is used so what we'll use we'll create the ports dynamically and to create the post dynamically you just change this port number to zero okay now you just start creating the application now see a dynamically a port has been assigned 63092 so whenever you are making a call right now you use this port 60392 do ascend oh what is that six three zero nine two six three zero nine two do Ascend yeah you are getting back the response now how many things are running there are two applications are up one is address service running in port number eight one eight one employee service is running in six three zero nine two there are lots and lots of concurrent requests are coming and one server is not able to handle it let's say there are 300 people trying to access the server like it trying to access the employee service at one time and I cannot handle this request I will create one more instance of this by running another spring boot application and right now this will be give this will be started in a different port number you look at that started in port number six three one two two okay now you can also do same thing Ctrl C this go there and run this six three one two two no this is the request do send see you are getting data from this port number this is one instance where your employee service is running and another one if you're gonna go over here 63092 do Ascend this is also running now if you're gonna click on this boot icon you can see 63092 is running it's not showing it over here I don't know why but how many consoles that I have over here right now there are three consoles right it is uh there are two instances of Employee Service running can I run one more instance run is spring boot app okay now there is another instance will run and this is running in port number six three one four nine there are three instances of Employee Service you can see there are three consoles of Employee Service three instances of Employee Service is right now running I don't know why it is not showing me over here maybe I will let you guys know but yeah there are three instances of this Services right now running so with this endpoint is also going to work now if I'll hit another request can I copy the end point from the other one Ctrl C go over here paste it and change this port number to this one 63949 okay here also you'll get your employee response back do ascend okay you are getting your response here six three one four nine right here six three one two two this will also give you the response back go to the first one six three zero nine two you are also getting the response back so what some people tell me okay I will ask right now I have created three instances right so what's the problem you have taught me about the load balancer right so what I will do right now I will create some modern day application what I will do right now I have I have more requests coming in right I have more requests coming in let's say there are n number of requests coming in for slash employee slash something right and all these people once employee data and one my one one one let's say I have only one Employee Service application which is running right now inside this Tomcat server this is not able to handle all the load so what I will do I will create three different employee server instance and I will deploy them inside three different Tomcat server and you know what this will do now I have three different employee application running three different Tomcat server I will have a load balancer here here I will have a load balancer load balancer okay I have a load balancer here now all these requests coming to this guy this load balancer will balance the load some request it will send to here some request it will send to here some requests it will send to here so what if you know there is a external API this employee service are calling some external API I don't I don't give a damn let them let them return back after 10 second only okay so I right now I got 200 threads here 200 threads here 200 threads you have to solve all this request right now if I'll get more request I will have one more employee uh you know application deployed deployed and one more Tomcat server and I will connect them with my load balancer so my load balancer have many instances to deal with so what's the problem and guys the problem is for all this server this is not going to come free this is going to be very much cost heavy right you are still using the still the you know call is going to be blocked only right so what will happen here the problem is still remain the same because all the server that you are scaling up internally your code is using same synchronous call only the synchronous call means once a particular thread is allocated that means that will be only respond back whenever the thread gives back the response and until then it will sit ideal till it did not get the response from the external API and now that's the problem so the problem is here you don't use your debut skill rather you try to fix the code that we have over here now the problem is here the problem is where the problem is here where right here in the line number 49 let's say we're talking about the rest template let's say the problem is here here you are making the rest template you are making the rest call using the rest template and the rest template is blocking in nature and people who are hearing this recording right now keep this in mind you'll get this question in your interview what is the difference between a blocking call and a non-blocking call and rest template is a blocking call why we are calling it to the blocking call because the the moment this particular line will be executed it will not go to the next line it will it will it it will block the thread the thread will be blocked for for the time being you will not get a response once you get the response then only the thread will be released but till the time you are you are using rest template one thread will be used to make that particular call and to process your request back to the client until then that particular thread will be set ideal so rest template is blocking in nature it will basically uh in in your case you know this is in in our code is like this we have a server okay now our server is connecting to a database we are having one database call inside our service and our service is also making a external rest API call so now this call is also blocking and this call is also blocking in nature okay so in case there is one request coming one thread is used for this this thread will this rate will basically process all this thing and then only it will do the stuff okay one second please okay so by the time it will not respond back there will be problem okay that's why this is a blocking call and this is the disadvantage of the rest template because this is blocking in nature is it making sense so far guys what I'm trying to say here yes no yes yes please can we use few deleteable feature here for the rest template column what what is the future this is an API uh no like the use in Java or the future oh yeah the the Java future is Java 9 future you are talking about I'll tell you that there are different there are different API okay I'll tell you one thing there are different API now this is what we what we have seen this is called synchronous in nature okay so to make asynchronous call you have different API like RX Java Java 9 has some feature okay I think Java 8 also has some feature which has been operated by the Java 9 what 11 is talking about there is a framework which has been heavily used called project reactor okay project reactor these are the library or API which has been developed if you want to make asynchronous call okay what do you mean by asynchronous call even you know if you talk about spring wave flux the wave blocks of spring basically build on top of project reactor so I will not teach you RX Java I will not teach you the Java 9 feature I'll teach you this one project reactor in your spring web flux course because this is the prerequisite to learn this web flux and using webflogs you can make asynchronous call and what do you mean by asynchronous call asynchronous call means let's say I told you if you are using normal web MBC let's say you are using some calls using webmdc then if there is a request coming here then one trade will take that request makes a call to the external API until the time this response bonds will not go back to here this this is basically gonna block the thing because spring web MVC is not not asynchronous but for asynchronous call you use wave flux okay you can use wave flux instead of where the MVC so that let's say you are making a call and now this call is the the your web blocks application where flux using web like using webmbc you are creating your app let's say I have built my app using web clocks right now in this case whenever there is a request com the thread will be taking that request making a call right now to the external service the external service is giving 10 second to respond back doesn't matter it will basically right now this trade will not keep on waiting till the request has been processed at that time if there is any other request is coming this thread will take up this request and again make a call to the external service until the time this guy applies trade will pick it off and process that request and that thing we're going to be seeing in our web flock session which is going to be asynchronous call and we will not be going mad with this particular thing right now so the previous problem can be resolved if we are going to make the asynchronous style of programming or coding instead of the instead of using rest template if you're going to use reactive spring program if you want to see I will just quickly do it for for in in a minute I'll go over here and I will go to the employee service let's say here only we are making a call so in our palm.xml I will do right click spring adds data to convert this project to a reactive spring project with a search for reactive okay spring reactive wave you just click on this click on next okay and click on this form.xml it will update the form do a finish let me stop all my instances can I go over here let me just stop this guys okay and right now all my instances are stopped now I will just do a I'll go to my pump.xml let me check whether my web flocks has been added and it has been added over here springboard started with flux now my application is going to support reactive programming let me do a map and update map and update project and click on course update do a update and this is going to update the thing and now guys you can start building uh you know where in a reactive applications you can make a reactive call or asynchronous call where you can maximize your application performance by reusing your threads trades are basically right now you know this is going like you know trades you need to reuse the thread one thread is going to take 40 MB of Ram uh sorry yeah 40 MB from the ram imagine you have 1 GB of RAM if you are not reusing your trade one thread is coming if you are using thread per request model then if one thread is going to serve one request that is going to take 40 MB space in your RAM let's say you have 1GB of ram then if you're handling 20 requests so 30 request is 30 into 40. that much is only going for your that much of ram you just is only going for your thread handling or request handling and the resource thing is also pending now you can think how your server will perform so the best thing you can do you can reuse your server you can do sorry you can reuse your threads and the way you can reuse your threads by writing some reactive code and that will be learning in the reactive programming but by using webflogs you can start writing if you're gonna get me 10 minutes I'll just show you a sample I realize that replacement of that rest template that are we going to use that print line yeah Thin Client I am not talking I'm talking about the replacement of the rest template what they have given us right now now they have given us this web flux and using this web clocks we can use our web client did you guys hear about this what web client yes exactly so you know they have deprecated rest template just because of this stupid web client and this is not stupid web client is Gonna Save You from us go to the method once in the service layer yeah okay yeah or where we can make it asynchronous we can make it asynchronous where is my service external service exactly we are calling right now the external service using what then another trade will take a new request or another set will execute the new life ah that thing again there are a lot of things involved in that uh Summit I will be coming back to that in your because see there are a lot of things to understand how the call is going how we are getting data are we getting the entire data or are we getting data in chunks what is back pressure how we are handling the back pressure a lot of things are making because everything will be messed up suppose and we need the data like whatever we will be getting exactly and another thread will work 40 on nine or for in the fifth time so then that yes absolutely and what is the solution for that and how it is getting handled that's what we'll be learning in the web clocks course right I have announced the batch already Yeah so basically so make there are there are a lot of things are involved here there are a lot of concepts are involved here let's not get into that otherwise we'll go out of the reactive topic will be go to the reactive topic and going out of this particular topic right so let's not make it complicated though and we will be I'll be answering all of your questions I'll be teaching you in much more detail so guys if you want to switch off the video right now you can switch it off but just for sake of completion I will be showing you guys how to how to write this same piece of code using the new reactive way of programming that spring 5 is recommending us to coding in that way without using rest template but neither in this course I'll be using rest template nor I'll be using web client I'm just telling you as few of my people here they are telling me right now to implement it so I'll just show you how to use web client but I will not be using it so what I will be doing first of all to I will be basically writing now click on control 1 and move this to a different method and I will tell this method is calling address service using rest template right now I will not be calling the other service using the rest template I will be removing this method I just kept this particular method right here just for your you know understanding now let's try to configure web client and let's make a rest call using the web client and for this inside the employee app config right now what I will be doing I will be going to my employee app config and I'll be configuring a web client and just like you have the rest template I'll be having the web client so I'll do what public web client okay when web client web client and just have a web client method and right now you just configure a web client right over here by introducing a bin and for the web client what you need you basically will be needing a web client here so web client dot Builder and you're gonna be build the web plant object and whenever you are you will be this will be returning your web client object right just like your rest template just like your friend client that I was talking about you have web client to make the reactive calls which will not be blocking in nature right and with this web plant you will be setting off your base URI I think after build off you can set up the best URL base URL and your base URL I have just written some here no let's copy it this is going to be my best URL let's go to my config file and let's paste it over here okay and I will be resuming this and I'll be on commenting the s this is going to be my best URL put it over here okay do a control shift o to have the import and this is where you have set up the web client just the way you have set your restful rest templates right now the same thing you can do using the web client right now you can make the call just the way you are making the call with the rest template so you can comment out the rest template as you will not be using it and now you can comment out this uh you know Constructor will not be using it because this Constructor was used to configure the rest template and right now this rest template method also you can comment it right now here we need the address response and this address response will be getting using a web client and for that right here you can have a private web Client First web client and web client and now you can just Auto add it just make sure that you have already created the bean and I will not be explaining more like you know what are different components has been told over here but I'll just tell you how to call how to get the address response so you can just do web client dot you need to have a get request get request to which URL you'll be having a get request to this URL so copy this and have it over here and you will be having a get request to this URL right now can I remove this one from here one from here and this ID we will be concatenating let's say we are getting the ID right here this ID will be concatenating so address plus ID this is how our request will go and now we are saying web client we're gonna be having a get request I just messed it off you can just do get get is a call with everyone please be on mute so it is going to be a get request then uh we can set up the URI okay you are I right here and this is going to be my URI so what we understood from here we are planning to make a web client request this is going to be a get request can I put it over here okay and this is going to be the URI of our get request and the next thing that we need we need to retrieve the data from here okay and we need to retrieve the data from this URI so dot retrieve and once you retrieve the data from this URI you're gonna be placing it inside some of the response in which response will be placing the data the data that has been returned as form of adjacent format where we'll be placing the data inside this address response right so we can just do there is a method called body to mono and body to flux I am going to use body to mono and just place your object right here address response dot class I am going to be placing the things now right over here and the next thing that I'll be doing I'll be using this block method this is going to get me back the address response right maybe you will have question what do you mean by this address to Mono whenever you're going to be working with the reactive programming which is going to be a entirely different copy guys don't mess it up with spring Cloud microservices don't no need to understand right now there is things like mono and flux and this mono you can see this mono and everything are coming from the reactive packets we'll be learning it in detail right now don't give a damn just think like this is where we are making a web client call and this is going to be our asynchronous call this is what spring is recommending us to do right now instead of using a rest template and this is the replacement of the rest template which was blocking in nature and this is non-blocking in nature and right now I'll do Ctrl s right here and now this should help me to get my things back if everything has been configured fine so if I'll run this run as a spring boot application and if I will run this run as a spring boot application let's see that whether both of my applications started fine this is my address service this is right now has been started and up let me go to the other terminal this is my employee service this is also up let me confirm it once both my instances are up so can I go to postman okay what is my I have changed my port can I go to my employee service this is my port which has been dynamically generated why it got dynamically generated because inside the employee service I have changed my import number I set it to zero if I set it to 0 this port number will be taken by Spring boot dynamically so I'll be using this port number right now to make the call to my employee service so I will make a call to my employee service using this port number and this one is internally whenever you're gonna be making a call to this as you guys know the call will go to the controller and this is my controller and inside this controller right now this one is going to hit your service internally and inside your service you are making a another rest call to a third party API but using your web client which is non-blocking in nature and this is the reactive way of making a call and if you're going to do a send oh you're not getting the data why is that because did I make any changes uh control Employee Service no I changed what number I changed no 633 oh sorry sorry a library load port number sorry 11 yeah okay I think we should stop right now to send yeah I'm getting the data you understand it and forget it right now we don't have to give a damn because why we will not give a damn because whenever you are going to work with spring cloud and microservices what spring recommend don't use web client it can use your plant anyhow don't use rest template that is for sure there is a another alternative that's called open frame or a fan client and why you will be using fan client what is the features you'll be getting out of it and why it is recommended when you're going to be writing microservices just use fin client and choose this on top of everything that you have why this is recommended we'll be learning it in the next session okay so in the next session we'll be actually starting with the communication using fan client then we'll be going with the service Discovery and the Service registration and we'll be talking about different spring Cloud feature from the next week I'll confirm you the call if you have it on the Tuesday if I can cancel it I will be cancel my client call on Tuesday and will reschedule to Wednesday if I cannot cancel it then we'll start on Thursday we'll go to go for Friday and Saturday okay Thursday Friday and Saturday will be the next call otherwise but I'll try to cancel my call on Tuesday just to have one more extra session so that we can have four in the next week okay so far you guys are clear how many of our Earth still 13 are here we are already one and a half it's already ran for one and half hours or more than that or two hours I don't know so is it making sense guys I like like or I did I did bore you guys enough I want to understand that how that trade will work when you will all right so right now let me just give you a very broad answer to the question that Tomic has asked me and I will be making it a you know really broad answer I will not narrow down my answer otherwise it will create a lot of confusion and we will be seeing this topic in depth whenever we'll be going for the reactive programming but for now what we will be doing let's understand that you have an application and in this application uh basically you are making some rest call and this rest call is going to an external service right and imagine this is your external external rest API call okay or external rest API where your application this is your own application which is making a call using a rest template right and this is blocking uh the thread execution so right now imagine there is a client is hitting a endpoint let's say slash EMP slash one then let's say this is basically internally calling this uh you know this is again internally calling a another restful endpoint and this is basically blocking your you know thread execution and making your trade seat ideal like you know if this is the thread which handle the request now this thread will sit ideal till the time this particular response comes back and once the thread basically gives the response back to the client the execution is completed but in the non-blocking world what happens is let's say there are two application let's say app one and there is app to okay now you can you can think like the app one is calling to the app too and whenever it is calling to the app to obviously it uses a trade and that thread right now makes a call right and right now irrespective of the app 2 gives the response back or not let's say the app 2 is taking 10 second to reply back to this particular application or to this particular trade at that time this thread will not seat ideal imagine right now what happened is the client has make a request and that request has been processed for the app one need to depend on the app to to get back the response and this has to do with this particular thread as it is going to make the call internally and right now it used to be blocked in in the uh like you know in the normal scenario that we have seen before but in this scenario this app2 will it it'll just make a request and there will be a immediately it will you know there will be a response back from the app too okay and now this trade will be released and now this thread will be keep on doing and keep on assisting the other requests which are coming it may picks up any other request and start processing this request and in this way this does not wait till the app to respond until till the time the app to respond is Gonna Keep processing all these requests right now okay and let's say right now the app to is ready with the response and it is trying to give the response to the application one now the app to will give the actual response and make this trade like the thread which called it previously the thread which makes the initial rest call that trade will be notified that hey I am done with my processing right now and here is your data and then application one will be processing the rest part it will execute the rest part of the code so initially it will not sit IDL once it basically makes a rest call it will be simply uh it will simply be released because this is going to be a non-blocking call and in right here after that the trade will be keep on processing the other request and when the application 2 is ready with the response it will simply notify This Thread so that this thread basically can you know execute the rest of the core that has to be done through this particular thread I think it's Max makes it clear right I will just give you a very um like you know a another example that might uh make the like make the understanding a little bit clearer we can think like you know I'll I'll just clear this out you can think like um you know or let's say you are calling customer care right let's say this is you and this is let's say some xyg company x y g company now you are calling to this company right but right now the representative of this company they are BG assisting the other customer and right now your call has been placed on in a queue right so whenever this particular let's say there is a representative setting here but he is talking to some other client right now over the phone and once this client goes away right now you you are going to be connected with this particular customer uh customer care representative but till then you are blocked right this is basically the blocking call okay but there is another another way uh that things can be resolved you can think like this is you and this is some xyg company okay and right now you are making a phone call to the XYZ company and right now the customer care representative who said here he is busy assisting some other customer but right now immediately you'll get a response right now that okay right now you can do the other work and whenever I'll be ready with the you know I'll be ready or I'll be available uh to have a call I will call you back because I got your number because you have given me a call so right now I can keep on doing some other thing you can keep on watching TV you can keep on scrolling your mobile right and whenever this guy is available he is gonna call you and let you know that okay right now we can have a discussion and tell me how can I help you and this is what a non-blocking style of coding whenever you are basically doing any i o operation or like you know whenever your application is talking to a database or talking to a file system or talking to any restful endpoints that you have all these things are called i o operations and though there are different style of you know performing the i o operations like blocking non-blocking synchronous asynchronous non-blocking Plus asynchronous and all this thing we will see in our future lesson where I will let you guys know that all these things have happened through the events and the events is basically happens through a publisher and a subscriber model and we will be talking about that thing later in the course like you know it's boring so make uh so far we are okay with everything or we are yeah yeah I am financing uh can I know guys you know so far it is all clear uh Manish uh Lakshmi chandrakanta Anusha can I get some response from you guys yeah yeah anything from your end yeah okay okay did I did I bore you guys enough today yes I haven't used it okay great then thank you very much I know guys you know this is like you know that that's we we always there in our office you know with employees so what a manager manager okay that we will have the meeting okay but what actually you want you just ping me that personally on my Skype chat if you want to scold me for this extended session but anyhow I'll put it in one goal all right thank you very much guys I'll see you guys on Thursday if I cannot reschedule the call to Tuesday otherwise Tuesday then Wednesday off then Thursday or Friday Saturday otherwise Thursday Friday Saturday next week okay thank you very much and in the next week we will actually start with spring Cloud I'll upload the code I'll upload the recordings enjoy okay and practice bye bye guys have a good day a code I'll upload to your repository on digital okay you just go to the material section of the course you will be getting your core you will be getting back your you know code right there okay sure fine okay and the first three four videos guys I'll make it available for free of cost uh for demos right maybe four or five I'll give them for demos and maybe we'll have it on YouTube or in our beam your repository but I'll open it I'll make it public after after I record uh after I do the editing right and then we will we will just start uh you know referring to your repository will have around 50 60 hours of content on microservices will complete it but we will do step by step first we'll learn spring Cloud then secure RS 10 Saga then some design pattern then oath will bring oath with her micro services and will start doing lots of interesting thing but for the next three weeks my goal is to complete the spring Cloud stuff in a detailed way and then we can make it more detail in the coming weeks in the next month okay go to the plan or anything you want to add okay with the plan everyone [Music] you guys only told me not to taste Kafka right now like so many I will not teach you guys Kafka right now something let me complete the spring Cloud first then I'll obviously do the Kafka as well okay fine and Kafka and Kafka is important so Kafka is very Kafka plus Revit mq both are important for your micro Services okay so before we huh lavanya everything will be included I will not wrap up the course love in there you whenever I told you no I know how to start a course I seriously don't know how to end it when you guys fed off you guys go okay all right cool thank you guys uh thank you guys yeah anything loving you you are asking maybe in the weekend yeah sure I'll I'll have a try I'll have a try loving you okay and I'll let you I'll let you know but on Sunday we have match no so make you know they started messaging me you know sir you know get ready I said okay we'll see I'll talk to you guys after the match oh yeah yeah that's actually a good thing but it's not like you know he's not there so that we we will just win just because of his not there is not okay so if my people from Bangladesh Sri Lanka you know uh like you know Pakistan if you guys are hearing then yeah I'll see I'll talk to you guys after the match and if we lose uh there is one day off just because we'll try to forget about that thing all right prabhat you follow cricket for what you are hearing [Music] you do follow it or you don't follow prabhat okay yeah yeah that's fine no that it used to be a super strong team for sure yeah but yeah let's see okay we'll have the discussion after the match my match is over I don't know if we'll be qualifying for the final or not but once we qualify uh let's see will happen [Music] yeah yeah Sunday and yeah you know if you're gonna pay me you know two cross also I'll not come and teach you guys anything okay okay thank you samik have a good day and have a good day everyone and take care enjoy the match okay and irrespective of the result just enjoy Cricket goodbye take care thank you thank you [Music] foreign [Music]
Info
Channel: Selenium Express
Views: 71,709
Rating: undefined out of 5
Keywords: Selenium Express, Microservices, Java Microservices, spring boot, Microservices communication, RestTemplate, Spring Cloud, Rest, API, rest api, springframework, java microservices, spring boot microservices, spring microservices, spring microservices project, spring boot rest api tutorial, rest api spring boot, microservices architecture java, spring cloud microservices, microservices architecture, microservices example, java spring boot microservices, spring cloud tutorial, java
Id: a2EZxkwMrjU
Channel Id: undefined
Length: 109min 54sec (6594 seconds)
Published: Sat Oct 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.