Load Balance

Spring Boot: Build Your Website Real Quick Micro-Service With Spring Boot
18 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.23
List Price:  €130.53
You save:  €37.29
£79.94
List Price:  £111.92
You save:  £31.98
CA$136.79
List Price:  CA$191.52
You save:  CA$54.72
A$153.20
List Price:  A$214.48
You save:  A$61.28
S$135.94
List Price:  S$190.32
You save:  S$54.38
HK$782.80
List Price:  HK$1,095.96
You save:  HK$313.15
CHF 91.30
List Price:  CHF 127.83
You save:  CHF 36.52
NOK kr1,094.90
List Price:  NOK kr1,532.91
You save:  NOK kr438
DKK kr695.39
List Price:  DKK kr973.58
You save:  DKK kr278.18
NZ$167.81
List Price:  NZ$234.95
You save:  NZ$67.13
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,958.03
List Price:  ৳15,341.69
You save:  ৳4,383.65
₹8,331.80
List Price:  ₹11,664.86
You save:  ₹3,333.05
RM477.70
List Price:  RM668.80
You save:  RM191.10
₦126,689.32
List Price:  ₦177,370.12
You save:  ₦50,680.80
₨27,798.13
List Price:  ₨38,918.49
You save:  ₨11,120.36
฿3,702.20
List Price:  ฿5,183.23
You save:  ฿1,481.03
₺3,249.97
List Price:  ₺4,550.10
You save:  ₺1,300.12
B$514.66
List Price:  B$720.55
You save:  B$205.88
R1,901.16
List Price:  R2,661.70
You save:  R760.54
Лв182.41
List Price:  Лв255.39
You save:  Лв72.97
₩137,421.64
List Price:  ₩192,395.79
You save:  ₩54,974.15
₪379.55
List Price:  ₪531.39
You save:  ₪151.83
₱5,778.57
List Price:  ₱8,090.23
You save:  ₱2,311.66
¥15,552.99
List Price:  ¥21,774.81
You save:  ¥6,221.82
MX$1,705.36
List Price:  MX$2,387.58
You save:  MX$682.21
QR364.16
List Price:  QR509.84
You save:  QR145.68
P1,384.82
List Price:  P1,938.81
You save:  P553.98
KSh13,448.65
List Price:  KSh18,828.65
You save:  KSh5,380
E£4,789.16
List Price:  E£6,705.01
You save:  E£1,915.85
ብር5,677.33
List Price:  ብር7,948.49
You save:  ብር2,271.15
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$95,258.47
List Price:  CLP$133,365.67
You save:  CLP$38,107.20
CN¥724.52
List Price:  CN¥1,014.36
You save:  CN¥289.84
RD$5,872.31
List Price:  RD$8,221.47
You save:  RD$2,349.16
DA13,431.87
List Price:  DA18,805.15
You save:  DA5,373.28
FJ$229.18
List Price:  FJ$320.86
You save:  FJ$91.68
Q776.12
List Price:  Q1,086.60
You save:  Q310.48
GY$20,901.55
List Price:  GY$29,263
You save:  GY$8,361.45
ISK kr13,994.60
List Price:  ISK kr19,593
You save:  ISK kr5,598.40
DH1,013.23
List Price:  DH1,418.56
You save:  DH405.33
L1,780.93
List Price:  L2,493.37
You save:  L712.44
ден5,743.61
List Price:  ден8,041.28
You save:  ден2,297.67
MOP$805.67
List Price:  MOP$1,127.97
You save:  MOP$322.30
N$1,914.68
List Price:  N$2,680.63
You save:  N$765.95
C$3,675.06
List Price:  C$5,145.23
You save:  C$1,470.17
रु13,310.19
List Price:  रु18,634.81
You save:  रु5,324.61
S/370.43
List Price:  S/518.62
You save:  S/148.18
K379.55
List Price:  K531.39
You save:  K151.83
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,608.51
List Price:  ZK3,652.02
You save:  ZK1,043.50
L463.96
List Price:  L649.56
You save:  L185.60
Kč2,349.11
List Price:  Kč3,288.85
You save:  Kč939.73
Ft36,594.60
List Price:  Ft51,233.91
You save:  Ft14,639.30
SEK kr1,086.27
List Price:  SEK kr1,520.82
You save:  SEK kr434.55
ARS$87,315.45
List Price:  ARS$122,245.13
You save:  ARS$34,929.67
Bs689.99
List Price:  Bs966.01
You save:  Bs276.02
COP$389,137.18
List Price:  COP$544,807.62
You save:  COP$155,670.43
₡50,081.85
List Price:  ₡70,116.60
You save:  ₡20,034.74
L2,464.83
List Price:  L3,450.86
You save:  L986.03
₲741,742.01
List Price:  ₲1,038,468.49
You save:  ₲296,726.47
$U3,834.62
List Price:  $U5,368.62
You save:  $U1,534
zł402.27
List Price:  zł563.20
You save:  zł160.92
Already have an account? Log In

Transcript

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

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.