Hystrix Dashboard | Fault Tolerant Circuit Breaker Design Pattern Example in Spring Boot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have you used any fault-tolerant api which prevents your code from failing have you seen a code which doesn't return any exception but it does some fallback operation in this video we are going to see how to use hystrix and implement the circuit breaker design pattern and how we can use hystrix dashboard to see what is happening or monitoring your application internally [Music] press the bell liken on the YouTube app and never miss any update from tech primers hystrix is a library created by Netflix it was created with the purpose of solving the fault tolerance in a distributed system how Netflix uses hysteresis they use hystrix as a library in order to handle failures and do some fall backs when there is a failure at a high level just understand that history is some fault tolerant fair work similar to how we have a catch block in a try-catch exception block so inside the catch block we catch some exceptions or unknown errors and then we do something with that particular exception right similar to that hiss takes as a fallback mechanism with which you can do some fall backs additionally you can configure these fall backs by using the circuit breaker design pattern circuit breaker design pattern is something similar to how a secured works in your electrical board if you are familiar with the electrical circuit board you have different states like open closed and half open let's take a typical example of switching on an electric Cube light if you have your circuit open then the current doesn't pass through the circuit and your light is always switched off so this is the state where the circuit is open which basically means something has broken if the circuit is closed that's when your electricity will pass through the circuit and the light is going to glow so typically in a successful scenario the circuit is going to be closed so always the circuit is going to be closed initially and it is going to get open when there is a failure so when there is a failure the circuit will be opened and we can see that from the hystrix dashboard so how the circuits are closed or how many circuits are closed what are the different circuits which are closed or which other circuits which are open and we can even see the statistics of how many requests came and how many requests are failing as well so hystrix consolidates all these circuit breaker design patterns and shows up in the hysterics dashboard so when does the circuits get open automatically and when does the circuit gets closed automatically you can control that using configurations where you can configure saying if let's say there are 100 requests coming in and if there are fire requests which got failed open the circuit and for the next continuous fire request don't allow these requests to come in by default failed if you have noticed any high-performing websites let's say for IRCTC website you would have notice that if you try to refresh or if you try to click the refresh button again and again sometimes it immediately fails this happens because there is a fault tolerant or a circuit breaker which has been implemented which will break the circuit it will not send you till the server it will say that my first request failed so I'm going to fail you for the next 5 continuous requests until I know that the circuit is going to be functional so the 6-3 course would be now allowed the 6th circus might take few more seconds and it will try if the circuit is going to work or not if it fails again then it will again open the circuit so this is how circuit breaker works so let's see that with an example let's take a fix ecommerce website example right this is a architecture of a small ecommerce website where we have a mobile app and a browser the requests are coming in from different sources here and there are different micro services which are hosted here what happens when the shipping service goes down the mobile app will crash right that is when we need to create a fault tolerant API where the mobile app should not crash instead it just returns an empty response or it just returns an error message to the user saying something is going wrong and you should not crash the UI if some services failed so this is a typical example of how you can implement fault-tolerant systems let's go to our usual website start at spring dot IO and we can create a spring boot application which uses spring cloud hystrix and we are going to use spring cloud hystrix dashboard the group I would say is calm tech primers our usual group and I'll just say hystrix dashboard example the dependencies which we would require are just spring them or spring MVC framework then we need the history library which is nothing but the circuit breaker implementation from Netflix we also need the hystrix dashboard so that is another dependency in order to support these history dashboard we need to create the actuators as well so we are going to use the actuators actuators are nothing but some endpoints with which you can monitor your application monitor your spring boot application we'll see that why we need actuators as well so let me generate this project I'll just unzip it from the terminal I'll also open it with IntelliJ let me maximize it now if you see the palm rot examine the from dependencies which we have are the actuators the spring boot web the spring cloud hystrix and the hystrix dashboard that's all we don't have anything else we're just going to create a simple hystrix implementation and we are just going to use hystrix dashboard to view that in order to enable hystrix you have to add the annotation at enable hystrix so this will enable our application to stream hystrix data so this is going to stream some data to the dashboard so this is going to be a client and also we need to enable the dashboard because we want to see the dashboard as well right you want to have our application dashboard embedded into the same application so we are going to use dashboard ansible if let's say you have multiple micro services you can host a separate dashboard service and then host only the dashboard and you can use a tool called turbine which can merge all these different historic streams and then you can see it in the dashboard in our case we are going to use the dashboard and the hystrix implementation in the same process so that is why we enabled both hystrix & hystrix dashboard here you can even do enable circuit breaker that is another annotation if you go inside the circuit breaker annotation you will be able to either use the hystrix implementation or whatever circuit breakers which are configured in the spring boot application so in our application we are using hystrix there are other implementations as one which you can use from different vendors but here we are using hystrix from Netflix I'll create a simple rest resource just to show how we can implement hystrix it I'm just going to mark it as a rest assault rest resource and just consider that the URL is going to be just rest I'll also add I get mapping I'll say hello string hello typically what we will do is we will do a hello world isn't it so what happens something goes wrong here right how can we introduce artificial failures here I'm just going to use a random utility I'll just use random utils dot next boolean this is just going to give the random integer or random boolean value where it will decide whether it is true or false and I'm just going to throw an exception so that way we are going to introduce artificial failures in the function so what I'm doing here is randomly I'm just throwing an exception this is going to introduce an artificial exception so in case of a normal application if we don't have a hist --ax implementation enabled what happens is we get a failure message right the exception shows up in the UI so the UI response will have four not four or whatever type of exception it is let's say 500 internal server exception so your rest endpoint will return an exception back to the client however I don't want to do that in order to do that I'll enable hystrix in order to mark this particular method to be fault tolerant I can had this a notation called hystrix come on and I can give a fallback method fall back is nothing below but a cache block or a failure scenario which will be triggered if there is any exception in this particular method I'll just make a fallback hello fall back hello and we can give a command key we are going to call it as hello and you can give a group so you can give a group name I'll also say hello for that this is just to identify the particular method from the hysterics dashboard so that you can easily uniquely identify which is the place which is going to fail now we have created a fallback method we need to implement it here the signature of this particular method should be exactly the same as the method behavior adding the hottest tricks annotation the name of that should be this the hello fallback whatever method name we have mentioned here it should be the same here here I'm just going to return saying fallback hello initiated so this way whenever we hit the hello rest endpoint if there is a failure we will know that there is a fallback if there is no failure we will be getting a hello world as a response so what else do we need that's it so I just have implemented a simple hystrix command I don't want anything else I am going to start this particular project in a different port so I will just say eight zero nine two also I want to use Java 8 in my project I will just change that you and let's start the project so this is a spring boot application which is just having one particular rs10 point cauldre slash hello and we have implemented some historic s-- implementation or hystrix command which can do something let's see what it does so it is the port failed I think I have not given the correct I think I gave the wrong name server dot port I just need to read on this so this should start this big boot application in the port eight zero nine two since we have enabled actuators actuators create some health check endpoints and also it creates an end point called hystrix stream so if you notice here these are different actuator end points actuator creates all these end points for example health dump trace refresh all these are created by actuators and also it has something on his trick stream his trick stream is the rest endpoint which is going to stream the information about the health of the method which we added here so it is just going to stream the health of the rest resource implementation whatever we did here so the health of the fallback hello or the hello group will be streamed via this particular endpoint and using the hystrix dashboard we are going to see that the hystrix dashboard is present in slash hystrix so it will be present in slash istic so if you see here it is going to present in slash hystrix and slash his tricks dot stream will have all the streaming information about this particular JVM let's go to the browser localhost colon 8080 to slash let's check if the process is up ready up it's up this is a spring boot application so it shows that particular error if you go to hysterics this is going to bring up the hystrix dashboard so this is the hiss takes dashboard here bear you can provide a URL of a different application as well so you can monitor any application in this particular dashboard by just providing this URL here we are going to use the hystrix start stream we're just going to use hystrix torch in here I'm not doing anything else I'm just hitting the east historic start stream which has the streaming information about our JVM so right now it shows nothing I'll just open one more tab and then I will show you by hitting the ok this is just coming out let me minimize this guy I think I know how to show it separately let me show that I'll minimize this as well okay so in the back if you see there is nothing right now let's hit the rest endpoint which we had created the rest lash hello right that's what we had created this should return hello world if it is successful and if you notice here in the background when we hit the rest endpoint the circuit dashboard started showing something so it is showing the hello this is the group which we created and it shows up the request and it shows up some dot here so the number of times we call this particular method it is going to call this see see this right now fall back happened here so we got a response saying fall back hello initiated and you can notice that this is showing up red because there was a problem and if you see here the circuit is still closed because we have not configured anything so by default it has a default configuration and it is using some default configuration and it is making the circuit to be closed still but you can configure the number of requests which gets failed and you can open the circuit based on that so I'll just try calling this n number of time right I'll just try to call this from different different you are also again so you can see the number of records which are getting hit right see that it is getting increase it shows how many percentage succeeded and you can see the failure rate as well so this is the failure rate it goes there right so to fail three failed except etc so this is how whenever a request happens whenever streaming issues happen you can easily monitor using the hysterics dashboard so why is the dashboard so big and there is nothing else right this is because we can configure multiple functions or multiple Hastings commands in the same dashboard let's try creating a new method in order to show that case right I'll just copy/paste exactly the same thing in this case I'll just say hello whitey hello YouTube right I'll just create one more group called hello whitey so that we know but I will just call the same for back method so I'll just say hello YouTube I can even call the same method so that is what I am doing here let's restart this application so once you refers the hystrix dashboard the existing information goes away and it is getting the data via the hist extort stream that is why the data gets refreshed without even being refreshing this URL because the URL which it uses the hist extremes is a streaming data so you get messages every now on them without even having to retry so let the spring good application come up I think it is up let's try refreshing our dashboard so there is nothing in the dashboard because we have not initiated any requests so I'll just hynny she ate at a coast called hello rest hello and we also have the hello YT right so and just trigger that as we'll see that once i trigger the hello YT a new option came up here we can do the same again so I can do this all day long right I can just try doing this and then you can see the response here isn't it if there is a circuit which is open the circuit open happens here so the circuit will be open and all the subsequent requests will be failing all this so this can be configured by different configurations you can take a look at the sprink allowed documentation there are different configurations which you can configure in order to handle the hystrix in your own way right so this dashboard gives us a high level picture of what's happening inside the application how many requests are passing on how many requests are failing etc so this is very good when you are writing for intolerant systems and you want to monitor this fault tolerance from a dashboard so you can use hystrix if you are already using a spring application you can use spring cloud hystrix without even having to deploy in cloud you can notice that I have just run this application locally I am NOT doing anything else I just installed it locally it's not even installed a set is just running inside the application so you can use hystrix anywhere in any application even if it is a monolithic application you can use it you can monitor the fault tolerance from a dashboard so this is what hystrix provide it is majorly used when you are using micro services design patterns and fault tolerance when you want to handle fault in the microservices world that's all I wanted to cover in this particular video I hope you guys understood what is history and how we can use hystrix inside a spring boot application if you are not using any spring boot application you can use directly the hystrix library and you can directly use it without the annotation instead of the Mystics commander notation you will be using the hist --ax command classes which Netflix directly provides you can directly use the Netflix history API and do that if you are not using a spring boot application I hope you guys understood hystrix and you might be using hystrix in the future if you liked the video go ahead and like it if you haven't subscribe to this channel go ahead and subscribe to it meet you again in the next video thank you [Music] [Music] you
Info
Channel: Tech Primers
Views: 30,940
Rating: 4.874126 out of 5
Keywords: techprimers, tech primers, hystrix, fault tolerance design, hystrix example, circuit breaker design patterns, microservices fault tolerant, hystrix dashboard, hystrix tutorial, hystrix dashboard tutorial, spring boot hystrix, spring cloud hystrix
Id: SxJ2uLZWOyw
Channel Id: undefined
Length: 20min 13sec (1213 seconds)
Published: Mon Feb 19 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.