Hystrix Circuit Breaker With Real Impatient
In a distributed environment, failure of any given service is inevitable. Hystrix is a library created by Netflix designed to control the interactions between these distributed services providing greater tolerance of latency and failure. It was created with purpose of solving the fault tolerance in a distributed system.

At high level just understand that Hystrix is some fault tolerance framework similar to how we have catch block, in a try catch exception block . Similar to that Hystrix has some fall back mechanism with which we can do some fallback.
Scenario:
Consider an example where we are accessing web API through browser and the here the API.
Consider this API is entreating with services A, B, and C
From browser we are hitting API from API its calling, Services A, B, and C
SO if one of the services goes down the entire application is going down.

What we would like to do is stop failures from cascading down, and provide a way to self-heal, which improves the system’s overall resiliency.
Hystrix is the implementation of Circuit Break pattern which gives a control over latency and failure between distributed services.
When services is not available it will call fallback and its provide the data.
once services is up again it will be going to be services. so this
implementation provides by Netflix with hystrix API.

Use of Hystrix:
- Stop cascading failures in a complex distributed system.
- Isolating the points of access between the services.
- Enable near real-time monitoring, alerting, and operational control.
Let’s do some practical
To demo circuit breaker, we will create following two micro services where first is dependent on another.

Student Micro service:
Which will give some basic functionality on Student entity. We will call this service from School Service to understand Circuit Breaker. It will run on port 8098 in localhost.
Create A Student Services:
Download dependencies from below website
Select dependencies (Web, Rest Repositories, Actuator) and provide Group

Unzip and import the project into Eclipse as existing maven project. In this step, all necessary dependencies will be downloaded from maven repository.
Server Port Settings:
Open application. Properties
and add port information.
Create APIs
Now add one controller class
called StudentServiceController
and expose one rest endpoint for getting all the student
details.

Student.java

Open browser and type http://localhost:8098/getStudentDetailsForSchool/demoschool
.
It should show the below output in browser –

School Micro service –
Again a micro service where we will implement circuit breaker using Hystrix. Student Service will be invoked from here and we will test the fall back path once student service will be unavailable. It will run on port 9098 in localhost.
Create A School Services:
Download dependencies from below website
Select dependencies (Web, Actuator, Hystrix, Hystrix Dashboard) and provide Group and Artifact name

Unzip and import the project into Eclipse as existing maven project
Server Port Settings
Open application.properties
and add port information.

Enable Hystrix Settings
Open SpringHystrixSchoolServiceApplication
that is the generated class with @SpringBootApplication
add @EnableHystrixDashboard
, @EnableCircuitBreaker
annotations.

Add REST controller
Add SchoolServiceController
Rest Controller where we will expose /getSchoolDetails/{schoolname}
SchoolServiceController.java

StudentServiceDelegate.java

Build and Test of School Service:
Start the student service as described above and then test school service by opening browser and type
http://localhost:9098/getSchoolDetails/demoschool.
It should show the below output in browser:

Hystrix Circuit Breaker Demo:
Now let us stop the student service
This time it will return the fall back method response, Here Hystrix comes into picture

Hystrix Dashboard
Click in this URL http://localhost:9098/hystrix we will get visual dashboard is visual dashboard :

Now add http://localhost:9098/hystrix.stream in dashboard to get a meaningful dynamic visual representation of the circuit being monitored by the Hystrix component.
