Hey, welcome back friends in this video we will talk about a very important and rotation and we will also talk about load balancing. Now friends, there could be a scenario where we wish to add couple of more macro services in order to load balance for example, we wish to implement a jet another Samsung micro service which can balance the load for the application. Can we do that? Well, we can let me show you so in here, if I navigate to the store folder, we have the Samsung macro service, let me make a copy of it. And I will simply give the name it as Samsung two. And now let me open up IntelliJ IDEA.
And here I will click on file, click on open and select the Samsung to folder and click on OK and I will open this in a new window. Then I will click on window and select Samsung. So now, this is what this is nothing but an exact replication of our Samsung application, you can also check the path here. And if I show you here, it is an exact replica, but if we try to run this there would be an error because the port, the port of the application will be same. So first thing first let us update the port and we will give the port as 8085 maybe. All right, and that's it, we can straight away and this application.
So, we will open up the file Samsung application and we will run this application. Now this application will run indefinitely. And it will be like checked by the Eureka server again and it will be also registered. Right. So name of the application is Samsung again and let us open up the huge A panel. and here if I refresh taken note of this zones, it will show up zones to there are two zones now available, and it will load balance based on the requirement.
But for example, we straight away try to run our application that would be a crash. For example, here, I refresh this a store application click on Samsung, it will not show up anything, this will simply give us an error. Now, here is this thing. When we make use of the rest template, it will register things for the very first time but it will not refresh. And there would be some issues and even for Apple our application will crash. And this is the importance of the load balance and rotation.
This will fix all of these issues but there are like few very important configuration which we need to do into every store application. Now friends, why our application is crashing and How to fix this is something which we will check. So, let us open up our store application and here let me open up the store dot java file Alright. Now friends when it comes to store dot java file taken note on these three references, initially we have worked on these three references, which was cool, but we need to further fix it. For example here if we talk about devices reference devices result what we can do, it might happen that this reference will be useful for other methods as well. So first optimization which we can do is we can bring this outside outside the method and make this as a private thing.
All right, apart from that, take note of this result. reference this result ref. As getting initialized using this method, we are not making use of a new keyword. So now, bringing this outside the method is a good optimization and that said we need not to do anything else. Apart from that we can also like bring this model and view object outside. This can be useful again, when it comes to utilizing this reference for other methods.
This can be done as well, good enough nice, no problem, we can use this same reference and we can initialize with a new view and load up things. And as we have a new reference, can we get rid of this new keyword? Indeed, we should not why because for every method, or for every request mapping, there could be a different view file. As a result, we have to create a new object of it as a result only keeping the reference Outside as good enough Moreover the only optimization which we can do is bringing this particular model and view outside this switch case and bring it here, because for both of the cases we are loading the same view right. So, we can do this this could be a jet and at optimization you can and based on your business requirement and if there is no requirement of model and view references for other methods you can keep these inside this method as well it is up to you, but the most important thing is this addressed template taken note of it, when it comes towards us template we are doing what we are creating a new object for both the cases all right, and for every call a new rest template object will be created.
Now, is there any special thing about this rest template object nothing. It is just a Plain rest template object, there is no parameters passed for the constructor as a result what we can do we can get rid of this particular line entirely. And we can simply we can like cut this line and again mark this as a private entity and paste it in here. But what will be the outcome, our application will throw an exception because how we will initialize this for initializing model and view we have the keyword new for initializing device object we have this method this method will return an object of devices type and this result variable will contain the devices object cool enough but for the rest template, there is nothing there is no mechanism which is initialized Test. If you have an idea about a spring framework, we have something known as auto wire, right.
So in here we can auto wire stuff, but auto wire is a consumer. And we have to pass an object via the spring container. Now how that will happen. In order to make this work, we need to create a bean. So let us quickly open up store application dot java. This is the most appropriate place to create a bean and here we will create a bean and we will also create a method, public method.
Return type would be a rest template name would be a rest template again, and that this will simply return an object so it would be like return new is the template. All right now Springville smart Use this object to whenever there is a requirement of a rest template object and spring will optimize our application This is cool, but still we are not making use of load balancing what happens if there are more than one instances of microservice, how we will handle it and in order to handle it, we need to load balance in our app, which is consuming the micro services take a note of it we are consuming the micro services. So, the application which is consuming the micro services, we need to load balance that now, this rest template is the way how we are consuming the stuff as a result we need to load balance this addressed template beam and if you try to search for load balance, there is nothing that is a no and rotation by the name of load balance and why there is no rotation because we have to add a dependency See now what is the dependency the dependency is very simple the dependency is exact dependency which we have added for the Samsung application, the Eureka client dependency, yes, we have to also add the Eureka client dependency into our application.
So let us quickly to tat here I will open up the pom dot XML file. So, first of all, we will add this dependency Eureka client in our store application. Here we go. Let me save the changes. Then I will also add this dependency management stuff. So, these are like minor things, but very important things.
So take a note of it, it is showing up as some kind of error. Let me check we will fix the errors and some time we will update the project and that that should fix the problem. From that, we need to also add the property, this property cloud version. All right, let me stop the web server. Let me update the Maven project. So these are small things but definitely very important.
And this should fix the pom dot XML file. This is good enough. And now we will son will Rhonda Spring Boot app, no friends, some sometimes SDS or Eclipse shows this Red Cross for the pom dot XML. Just ignore it. Don't worry about this red cross at the beginning of POM dot XML. All right now let us refresh and here because Because we have added the Eureka client dependency, the Store app will also register itself as a client.
So that is one small little thing, but that is nothing to worry. And now let us simply check whether it will work or not. It is again showing some kind of error this time, let me check what's going wrong. All right, so somehow it is not registered. I guess I need to restart a couple of things. Let me check what's going wrong, or I have missed out somewhere.
All right. So can you guess where we have made a mistake that is said here that we have missed on adding the load balancer notation we have added the dependency but we have missed using this one. This is silly. very silly mistake, though. Let me rerun this application that will I'm pretty sure that this will fix the error this annotation is very important, take a note of it, don't miss on it as your application may through edit, and this time the Edit is because we haven't stopped the web server and, and we need to stop the web server first. And then we have to rerun the application, then we'll leave our application will run.
Alright, so everything is working properly. Let me rerun the application. Let me click on Samsung. Here is our application. Let me click on Apple, here is our application working. And we have also registered the instances let me minimize this.
All right, and you can definitely also give the name to our application. Let me quickly do that. So here, let me open up application dot properties. And we will give the name to the application and it would be what it would be like a spring dot application dot name equals maybe store. That would be a good name. Let me save the changes.
And let me stop the web server first and relaunch the application. Let me refresh the Eureka server. And we can easily make out that the store application has been deployed it is listing and the unknown application is down. And it could be I guess, removed in some time. You can again, do one small little thing, you can restart the Eureka server to get the updated list. But that is usually not a very good idea to restart the Eureka servers.
All right. This is cool. All right. So our application is working properly. All right. My laptop's The battery ran off.
So I need to like restart my system. And as a matter of fact, I also had to restart the Eureka server. As a result, you are not seeing the foot down application. But Matter of fact, here is the updated list of applications which are registered as a client under Eureka server, which is okay, but usually restarting. Eureka discovery services are not a very good idea. All right.
This is cool. This is nice. But Matter of fact, there is one more thing. The thing is that whenever there is a Eureka server, all the applications are dependent on the Eureka server for finding the clients finding the resources finding the consumer finding the providers. So there is also caching maintained and usually there is some level of fault tolerance just in case the Eureka server is down for some time. cache can help you to reach the destination.
Moreover, for example, one of the micro services not life in that case, the Eureka Discovery Service will keep on sending some signals in order to check whether the micro services live or not. And if the microservice is not alive, in that case, it will show some kind of error for example, we have already seen the down error, it was showing that the macro service was down Alright, so, these were a couple of things. Moreover, the concept of providers and consumers let me quickly also touch on this. So, here is our application where we are consuming the resource from a micro service right. So, we are consuming a micro service as a result of this this particular application is a what it is a consumer application where we are consuming other micro services. Likewise, the micro services like Samsung and Apple are providers.
These Map service are providing something to this consumer application. All right. Now finally, here is your task. For example, you can do what you can simply navigate to a Samsung application, the Samsung application, if I show you. Here, you're simply making use of a hard coded list. Moving forward, just a task, just a practice exercise, try to create a database table, keep the data in the database table, connect with the database and extract information from the database.
Moreover, you can do something extra in here, for example, all of these devices. For example, note nine is a smartphone, but there is some kind of a specifications for this smartphone. Right? So you can create a couple of more micro services. For example, a micro service for Apple devices, a micro service for Samsung devices, where you will keep the specification For these devices and this, this Samsung and Apple macro service may consume the specification micro services Furthermore, so you can go one step further and you can create an application where your list of devices will also contain specifications and you will get the specifications from some other micro service. So, you can definitely like to develop architecture, you can definitely try to build some advanced level of call, certainly a next level of call, which will again help you to understand the entire application of building microservices based application.
So, that is just a suggestion you can definitely do that that will definitely help you out if you have some time, try to make this application even better. Alright, so, that's it for today. I suppose you guys enjoyed this video. Thanks for watching. Have a nice day and take care