06.Understanding hystrix

22 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€66.35
List Price:  €94.79
You save:  €28.44
£55.27
List Price:  £78.97
You save:  £23.69
CA$97.91
List Price:  CA$139.89
You save:  CA$41.97
A$107.61
List Price:  A$153.73
You save:  A$46.12
S$94.02
List Price:  S$134.32
You save:  S$40.30
HK$544.76
List Price:  HK$778.26
You save:  HK$233.50
CHF 61.99
List Price:  CHF 88.57
You save:  CHF 26.57
NOK kr773.52
List Price:  NOK kr1,105.08
You save:  NOK kr331.55
DKK kr494.87
List Price:  DKK kr706.99
You save:  DKK kr212.12
NZ$119.04
List Price:  NZ$170.06
You save:  NZ$51.02
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,334.01
List Price:  ৳11,906.24
You save:  ৳3,572.23
₹5,905.12
List Price:  ₹8,436.24
You save:  ₹2,531.12
RM312.96
List Price:  RM447.10
You save:  RM134.14
₦117,219.95
List Price:  ₦167,464.25
You save:  ₦50,244.30
₨19,380.58
List Price:  ₨27,687.73
You save:  ₨8,307.15
฿2,429.14
List Price:  ฿3,470.35
You save:  ฿1,041.21
₺2,412.08
List Price:  ₺3,445.98
You save:  ₺1,033.89
B$405.06
List Price:  B$578.68
You save:  B$173.62
R1,267.31
List Price:  R1,810.53
You save:  R543.21
Лв129.73
List Price:  Лв185.34
You save:  Лв55.60
₩97,779.80
List Price:  ₩139,691.42
You save:  ₩41,911.61
₪261.55
List Price:  ₪373.67
You save:  ₪112.11
₱4,130.77
List Price:  ₱5,901.36
You save:  ₱1,770.58
¥10,901.93
List Price:  ¥15,574.86
You save:  ¥4,672.92
MX$1,414.26
List Price:  MX$2,020.46
You save:  MX$606.19
QR254.33
List Price:  QR363.35
You save:  QR109.01
P948.83
List Price:  P1,355.53
You save:  P406.70
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.50
List Price:  E£4,969.50
You save:  E£1,491
ብር8,583.46
List Price:  ብር12,262.61
You save:  ብር3,679.15
Kz63,877.12
List Price:  Kz91,256.94
You save:  Kz27,379.82
CLP$67,980.58
List Price:  CLP$97,119.28
You save:  CLP$29,138.70
CN¥507.18
List Price:  CN¥724.58
You save:  CN¥217.39
RD$4,200.06
List Price:  RD$6,000.34
You save:  RD$1,800.28
DA9,338.81
List Price:  DA13,341.73
You save:  DA4,002.91
FJ$158.92
List Price:  FJ$227.04
You save:  FJ$68.12
Q538.38
List Price:  Q769.16
You save:  Q230.77
GY$14,583.53
List Price:  GY$20,834.51
You save:  GY$6,250.97
ISK kr9,640.42
List Price:  ISK kr13,772.62
You save:  ISK kr4,132.20
DH696.78
List Price:  DH995.44
You save:  DH298.66
L1,273.82
List Price:  L1,819.82
You save:  L546
ден4,077.60
List Price:  ден5,825.40
You save:  ден1,747.79
MOP$559.09
List Price:  MOP$798.74
You save:  MOP$239.64
N$1,260.89
List Price:  N$1,801.35
You save:  N$540.46
C$2,566.52
List Price:  C$3,666.61
You save:  C$1,100.09
रु9,423.16
List Price:  रु13,462.23
You save:  रु4,039.07
S/264.65
List Price:  S/378.10
You save:  S/113.44
K280.62
List Price:  K400.90
You save:  K120.28
SAR262.75
List Price:  SAR375.38
You save:  SAR112.62
ZK1,926.57
List Price:  ZK2,752.36
You save:  ZK825.79
L330.17
List Price:  L471.69
You save:  L141.52
Kč1,676.98
List Price:  Kč2,395.79
You save:  Kč718.80
Ft27,187.34
List Price:  Ft38,840.72
You save:  Ft11,653.38
SEK kr769.78
List Price:  SEK kr1,099.74
You save:  SEK kr329.95
ARS$70,112.88
List Price:  ARS$100,165.56
You save:  ARS$30,052.67
Bs483.65
List Price:  Bs690.96
You save:  Bs207.30
COP$308,470.35
List Price:  COP$440,690.82
You save:  COP$132,220.46
₡35,450.59
List Price:  ₡50,645.88
You save:  ₡15,195.28
L1,762.11
List Price:  L2,517.41
You save:  L755.29
₲543,153.02
List Price:  ₲775,966.15
You save:  ₲232,813.12
$U2,992.07
List Price:  $U4,274.58
You save:  $U1,282.50
zł287.83
List Price:  zł411.21
You save:  zł123.37
Already have an account? Log In

Transcript

In this session, let us learn about Netflix is fix which is our fault tolerance library. Netflix is used for playing circuit breakers for potentially failing method calls. What is this attitude record? In a real time scenario, your circuit breaker is an automatically operated electrical switch. Basically it is used for protecting electrical circuits that is overlaid of current flow automatically the electrical switch will trip down that they are opening the electrical circuit that bank is protecting the electrical circuit. Similarly, hystrix accessor circuit breaker for spring micro services.

It is basically used for protecting the application. Let me explain this with an example. As to my overview micro services as you A, B, C, D and E. In this case c calls b b calls a, d equals D and D calls, what will happen if microservice a fails, automatically service B and D also will fail, because they will not get the expected output. Now, as you I have got yes and right which are complete clients trying to connect to these micro services, yet send requests to C C to B B tries to call even they micro service a has failed be pumping into micro service Yay. The reason is we don't have a virtual breaker it is kind of over two years. To avoid this situation.

We need to add a circuit breaker in the client part. If there is an exception in any of the micro services in A or B or C or D or D automatically attached That should call the fallback method. That is, I'm trying to call method like get our hotels. If any of the micro service fails like it is down are the databases down, if it is not able to send the hotel list, in that case, the circuit breaker should automatically call the fallback method. This is achieved with the help of hystrix. Now, let me explain what actually we are going to do in this application.

Already we have created two micro services, one is Eureka client. The other one is your portal service, client and hotel service they have registered themselves to the registry. They are discovery clients, so automatically they are available for other micro services. That is why I was able to call hotel service from Eureka plan. Now what I'm going to do is I'm going to create another micro service another service at Eureka hystrix You can view any name, basically, microservices will be like Inventory Service car service, hotel service book service. Here we are learning the concept of I have given it a fury coastline.

And here we are going to work with hystrix I have given the name of Eureka is six. If you see, Eureka client is a client for hotel service. And it is a service for Eureka hystrix my flow is going to be like this, I will create a micro service which we'll call you're a client, you're a kotlin intern as to call hotel service, say something like C calling v be calling a internet going in for a micro service. You can also have a web application because we are learning with micro service I will just go at micro service only how to access from one application to another application or from one service to another service. That's what we are going to see. Okay, what are the annotations that we are going to learn now or use now?

The main class should be annotated with accurate enable circuit breaker. Meaning to say that we are going to go in for a fallback method up any of this micro service fail, they need to add at the rate is six comma avati potentially failing methods. In our case, we are trying to call the method pop up a clock line. In your a cost line we are having four methods winter is calling hotel service. That's what we have done. So, we will take of three methods and create fallback methods for all the three we will try to solve.

I will go back in new spring starter project. I have given the application name of Eureka has six packages combat hotel app, given the description of hystrix client accessing Eureka client, click Next. What are the dependencies that we need to add now because we are working with this fix that is correct If his fix and is fixed dashboard, if you want to see whether the circuit is open or closed, how your application is behaving, that is if you want to monitor is fixed, you need hystrix dashboard. So select both the dependencies, then this application is also a micro service, which will register itself to Eureka server. So select Eureka discovery. These are the three dependencies, click finish, the application is getting created.

It is ready. Let me open it first and go to Palm dot XML. Let us see the dependencies. These are the dependencies Eureka is 606 dashboard. Now, let me go into the main class SRC main Java, the application or the file name is very big. So I can rename it or shift r for renaming and just going to have it as a six application.

It's not mandatory though. We open the main class I have an add to annotations at the rate enable discovery client and enable circuit breakers. This application is a micro service, it assigned to access another service. So this is a client, we need rest template class to access Eureka client. So let me ask you a compliment I need to create a beat. And also I need to add accurate load balanced annotation.

Let me copy it from Eureka client directly. So I'll open SRC main Java Eureka client application, we have this Ctrl C, I will come to these six applications Ctrl V. Let me save this. Again we are going to add a photo service. So we need hotel class OSI model class, I will copy combat photon app dot model and add it over here. Then, we need the controller from the controller only we are going to access this Eureka client by giving the warrant let me copy the controller class also controllers See sorry Ctrl C Ctrl V and open the controller. Let me change the name of the controller also on shift up, I want the name of six controller from the DMA controller, we are going to access Eureka client that is we will be calling the methods of Eureka play in this application, we are using hystrix So, we need a direct fix command remember as Barrett is six commands can be added above the method only in a class which is annotated with our direct service or at the rate component.

So, let us create a service class. So, let me select SRC main Java right click new class. So, this is the service layer I need to give the classes hotel service combat hotel app dot service click finish now from here only I am going to access the methods of Eureka client. So, what I will do is I will copy all the methods From his fix controller, I'm not going to cut it, I'm going to copy this Ctrl C, come to water service Control V. This is not a controller so we don't need request mapping. Let me remove the request mapping first. Okay, so here we have got four method I'm going to work only with three methods find out find by ID and grief.

Let me remove this. Next before adding at the rate is six comma let us change the path. So in stock photo service, this is going to be your a cop line. I hope you can understand the flow Eureka hystrix is going to call you recognize Eureka client is going to call photon service you have to do the proper part. If you don't know the path just open the corresponding controller. So in my case, it is flying hotel controller Sirois slash hotels slash hotel slash hotel ID then slash say hello Let me add all the three, I have changed the URL for all the three methods.

Next, I need to add a direct hystrix command. So, it is a great hystrix command. Within this I also give the fallback method, this fallback method will be called if you're a car client or any other service in that particular flow is getting throwing an exception or down. Now, let me give it a fallback method equal to I have to use a method name, the fallback method should have the same signature as part of the original method. So here I will just use the method name as fallback find all then I will copy this method Ctrl C Ctrl V. So here I have to give the name of fallback find on I don't need this. You are so it is hard.

Give it us arrays.as list now within which I need to create a hotel object new hotel it takes four parameters and just giving it us no hotel no city, you can do any dummy value also have done this I have created a fallback method for the original method, the fallback method should have the same signature as the original method. So, I am returning list and I mean less similar to the original method. Okay, let me do the same thing for find by ID and greet. I need to ask for his six comma above find by ID and they can give it as fallback find one and let me copy the same method then I will change the content. So, instead of find by ID it has to be fall back find one the parameter also should be Say we don't need a URL. But we need to return a hotel object.

So let me just copy the hotel object that I have created earlier. Okay, so I have created the fallback for find by ID also, next for greed to be followed by greed. I will copy the method and create this fallback method and have the messages come later. So what actually we have done in hotel services, we are trying to access Eureka client, which is a string micro service. We have annotated the methods with a direct hystrix command. If any of the micro service fails or ever throws an exception, I don't want to throw the exception to the user.

Instead, I'm going to call a fallback method. So for each and every method orginal method, I have created a fallback method with the same signature. Now this is a problem Class find it annotated with a direct service. So, this is also done completed, let me go into the controller his fix controller. In the sixth controller, we are trying to automate a rest template it is not needed because this template auto wiring is done this hotel service. So here we need hotel service as a reference reference, we are going to call the method of hotel service hotel service is now responsible for accessing the client.

So I don't need the Ura and other stuff and simply going to say hotel service dot find out and again, we don't need the second method we are not going to get the hotel space city and then in this case also I don't need the URL Ctrl D Ctrl D v dot find by ID and then Don't read this is that what about the request mapping if you see the client or the controller is also having the same mapping to six controller also having the same mapping that is not an issue at all that is one micro service which is fine. So, except the methods this particular endpoint controller is another micro service which is trying to expose a method with this particular endpoint. If you want we can change it over here as new hotel Okay, now, I have changed the request mapping also to make it very clear if you want to access Eureka client directly on localhost colon 8080 to slash hotels, if you want to access hystrix In that case, you need to go in for slash new hotels.

That is the thing. Now, this is done completed. I one more thing is this, we need to go into our application documents. So let me just copy it from your a Kirkland. Ctrl C here in SRC main resources control D. The first thing that I need to change is the path a port number, say 8083 Hotel services running in 8081 Eureka client and 8082 and Eureka hystrix and the 8083 Let me change the name of the application also it is fix that we saved. So, I will do a quick recap of what actually we have done in this application.

We are selected hystrix hystrix dashboard on Eureka discovery as dependencies. Then in the main class, we have annotated with the Enable circuit breaker because this is a micro service we have annotated it with the Enable discovery client. Then we have created a rest template objects using a grid beam and load balance. Actually we are not creating it we are asking spring to create the object for us. Next, we have created a hotel service class from the author service class. Normally we are going to access your a client, the class should be annotated with address service, identify the methods which we made, which are which may cause potential failures, annotated with addresses fix comment, and also specify the fallback method.

The start method should have the same signature as that of the original method. So, that all we have completed, then let me come to the controller in the controller I'm auto wiring service using this Amina then I am given request mapping using the service reference I am trying to call the method of service. I hope it is clear. Now, let us try running the application one by one. First thing is we need to run Eureka server. So, go over here start with Eureka service.

Eureka server started. Let me also open the browser and keep it side by side. So, here you can see, I have bought a browser, let me refresh it. No instances available Now next I need to run hotel service from here so I'll select total service, it has started running the application started successfully, you may record cover you can also see that it is registered register instance Auto Service. Let us get this up and click automatically I am getting Auto Service. Next, I need to run Eureka client.

Let the record client run. I will come here let us check whether Auto Service is running properly. So here I have to use slash all hyphen hotel. This was the path. I'm getting the list of hotels right now I'll come back here What happened to my Eureka Eureka client also started up properly in port number 8082. Let me just check it out in Eureka server registry refresh.

Now I have bought the Eureka client. Now I will also run this in 8080 to one this capacitor slash hotels it's a pleasure centers Now see it is giving me the list of hotels, you're a client is calling hotel service and getting the details already we have seen till the spot. Now the third one is I need to run Eureka is six. So the next Eureka is six right click run this Spring Boot app application is getting started. Yeah, perfect. It's a start it has started in port number 8083.

Let us check in the registry. Refresh. Yeah, cool. That's come Can you see this now are there are three micro services running in this Eureka service registry. Each service can call out other service because we have annotated them with at the rate enable discovery client. We are check how hotel services working how your a client is working, both are working perfectly fine.

Now let me open this Eureka hystrix instead of info. What I need to do I have to give new hotels actually and kaposi give new high for hotels we have not given that I have got the output what actually is happening in the Can you record is six is accessing Eureka flight Eureka client is accessing hotel service hotel surveys is only returning the desktop hotels and let me just go to eclipse I will maximize it maximize console you can see this part Eureka client This is your hystrix Eureka His face is connecting to a zero a two Can you see the spot then I will come to what you recognize from Eureka client is this it assigned to connect to a hotel service. Can you see this the bottom? Okay, now again, let me go back to the browser. Let us try with other things also new hotel slash 125.

Let me press enter. Yeah, just working perfectly fine. Can you see the output and also the last one was Hello slash right. Hello, slash ROM. Let me present. Hey, I am just doing mean there is some error let me just check it out.

Give me a problem let me check in 8082 so I'm checking for your a client it has to be slash se hyphen Hello slash raw. This also is giving me a problem. So here I will check it is add eight one slash greed slash rough. So service one is running fine service to rake our clients. Let me check your records client to client hotel controller, say hello slash username. I am removing this producers part.

Let me save this and I will relaunch Eureka client. Now let me take it out for you a client say hello slash Rob. Welcome Rob. Okay. This is what fine now I will come for you record hystrix status to be Hello. Yeah.

Now there is some error. Okay this part I will take it off now we will come to the six. What I'm going to do is explicitly I'm going to make you a client down. Okay that is I am shutting down this Eureka client can you see and going to shut this down but to stop we will go back to the browser. We will check in the registry This is the registry. Let me refresh.

What are the two applications that are running now Eureka has six and hotel service. Your data is fixed wants to connect to your a car client but it is down. Now like we will go to a zero a three slash new hotels. We'll see what is happening Let me press enter. Can you see it is calling the fallback method. Same way we will try for new hotel slash 1236 No hotel again because for both methods, we are given the same hotel details and also we will try with say hello I mean Hello slash up okay this part is giving me an error.

So, that is not an issue understand this what actually we have done this we are trying to connect from Eureka hystrix to Eureka Eureka client talks to hotel service when the flows flow there is no issue at all. But if one of the micro services fail in that case, the calling micro service will get an issue or the calling application will get an issue to a wider issue what we have done, we are gone for two six, we are added at the rate hystrix command and we are trying to get the fallback method. So five four find that we had the fallback method as fallback find on similarly for find by ID and pay for the error greet method. I hope you understood the concept stuff is fixed, what actually we have done this, when you are trying to access across multiple micro services, if any of the micro service fails the exception should not be thrown to the client instead of that you must show a fallback method.

That is what we have seen till now, in this is fix itself we are going to last till more, we were talking about the circuit breaker when the circuit will break open till now it is closed only when it crosses the threshold level, the statute will break open. So, let us understand about this concept of his face and his six dashboards in the next session. Thank you

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.