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
€96.21
List Price:  €134.70
You save:  €38.49
£79.93
List Price:  £111.90
You save:  £31.97
CA$143.79
List Price:  CA$201.32
You save:  CA$57.52
A$160.48
List Price:  A$224.69
You save:  A$64.20
S$135.83
List Price:  S$190.17
You save:  S$54.33
HK$777.07
List Price:  HK$1,087.93
You save:  HK$310.86
CHF 89.45
List Price:  CHF 125.24
You save:  CHF 35.78
NOK kr1,141.95
List Price:  NOK kr1,598.78
You save:  NOK kr456.82
DKK kr717.64
List Price:  DKK kr1,004.72
You save:  DKK kr287.08
NZ$177.52
List Price:  NZ$248.53
You save:  NZ$71.01
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,994.74
List Price:  ৳16,793.12
You save:  ৳4,798.37
₹8,496.63
List Price:  ₹11,895.62
You save:  ₹3,398.99
RM450.75
List Price:  RM631.07
You save:  RM180.32
₦155,628.43
List Price:  ₦217,886.03
You save:  ₦62,257.60
₨27,934.96
List Price:  ₨39,110.06
You save:  ₨11,175.10
฿3,444.45
List Price:  ฿4,822.37
You save:  ฿1,377.92
₺3,518.54
List Price:  ₺4,926.10
You save:  ₺1,407.55
B$617.53
List Price:  B$864.57
You save:  B$247.04
R1,837.80
List Price:  R2,572.99
You save:  R735.19
Лв188.31
List Price:  Лв263.64
You save:  Лв75.33
₩144,872.25
List Price:  ₩202,826.95
You save:  ₩57,954.69
₪364.89
List Price:  ₪510.86
You save:  ₪145.97
₱5,883.01
List Price:  ₱8,236.45
You save:  ₱2,353.44
¥15,672.80
List Price:  ¥21,942.55
You save:  ¥6,269.75
MX$2,028.91
List Price:  MX$2,840.56
You save:  MX$811.64
QR366.34
List Price:  QR512.89
You save:  QR146.55
P1,382.59
List Price:  P1,935.69
You save:  P553.09
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,091.27
List Price:  E£7,127.98
You save:  E£2,036.71
ብር12,764.56
List Price:  ብር17,870.89
You save:  ብር5,106.33
Kz91,790.82
List Price:  Kz128,510.82
You save:  Kz36,720
CLP$99,155.08
List Price:  CLP$138,821.08
You save:  CLP$39,666
CN¥729.81
List Price:  CN¥1,021.77
You save:  CN¥291.95
RD$6,104.52
List Price:  RD$8,546.58
You save:  RD$2,442.05
DA13,454.60
List Price:  DA18,836.98
You save:  DA5,382.37
FJ$232.11
List Price:  FJ$324.96
You save:  FJ$92.85
Q773.21
List Price:  Q1,082.52
You save:  Q309.31
GY$21,000.46
List Price:  GY$29,401.48
You save:  GY$8,401.02
ISK kr13,903.60
List Price:  ISK kr19,465.60
You save:  ISK kr5,562
DH1,007.40
List Price:  DH1,410.40
You save:  DH403
L1,841.78
List Price:  L2,578.57
You save:  L736.78
ден5,922.50
List Price:  ден8,291.74
You save:  ден2,369.23
MOP$803.42
List Price:  MOP$1,124.82
You save:  MOP$321.40
N$1,834.70
List Price:  N$2,568.66
You save:  N$733.95
C$3,694.32
List Price:  C$5,172.20
You save:  C$1,477.87
रु13,665.58
List Price:  रु19,132.35
You save:  रु5,466.77
S/374.71
List Price:  S/524.60
You save:  S/149.89
K406.86
List Price:  K569.63
You save:  K162.76
SAR375.72
List Price:  SAR526.02
You save:  SAR150.30
ZK2,777.96
List Price:  ZK3,889.25
You save:  ZK1,111.29
L478.81
List Price:  L670.35
You save:  L191.54
Kč2,418.25
List Price:  Kč3,385.65
You save:  Kč967.40
Ft39,800.47
List Price:  Ft55,722.25
You save:  Ft15,921.78
SEK kr1,103.66
List Price:  SEK kr1,545.17
You save:  SEK kr441.50
ARS$102,190.76
List Price:  ARS$143,071.15
You save:  ARS$40,880.39
Bs693.61
List Price:  Bs971.08
You save:  Bs277.47
COP$437,799.12
List Price:  COP$612,936.28
You save:  COP$175,137.16
₡50,480.33
List Price:  ₡70,674.48
You save:  ₡20,194.15
L2,548.03
List Price:  L3,567.35
You save:  L1,019.31
₲784,126.06
List Price:  ₲1,097,807.85
You save:  ₲313,681.79
$U4,460.41
List Price:  $U6,244.75
You save:  $U1,784.34
zł409.96
List Price:  zł573.96
You save:  zł164
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.