Hystrix Exercise
In this exercise, we will add Hystrix support to our three individual web services and enable a gracefull fall back in the case of errors. We will also install and configure a Hystrix dashboard that we can use to monitor the health of our circuits.
Enable Hystrix
All changes will be made in the movie-aggregator-service
project.
- Open
pom.xml
and uncomment the dependency forspring-cloud-starter-netflix-hystrix
- Open
src/main/resources/application.yml
and uncomment the properties related to Hystrix - Make a new package
microservice.workshop.movieaggregatorservice.service.fallback
- Make a new class
MovieAwardServiceFallback
in the fallback package. The class should implement theMovieAwardService
interface, and return an empty List for the single method - Make a new class
MovieCastServiceFallback
in the fallback package. The class should implement theMovieCastService
interface, and return an empty List for the single method - Make a new class
MovieServiceFallback
in the fallback package. The class should implement theMovieService
interface, and return an empty Optional for the single method - Open the
MovieAwardService
interface and alter the@FeignClient
annotation to add the fallback implementation - Open the
MovieCastService
interface and alter the@FeignClient
annotation to add the fallback implementation - Open the
MovieService
interface and alter the@FeignClient
annotation to add the fallback implementation - Open
MovieAggregatorServiceApplication
and make the following changes:- Add the
@EnableCircuitBreaker
annotation to the class -
Add three methods, each annotated with
@Bean
that will return a new instance of the three fallback classes. For example:@Bean public MovieAwardServiceFallback movieAwardServiceFallback() { return new MovieAwardServiceFallback(); }
- Add the
- Start the
movie-aggregator-service
and the traffic simulator. Everything should look normal. - Kill the
movie-cast-service
application. You should see that the aggregator service is still running, but not returning cast members. - Open a browser to http://localhost:8080/actuator/health. You should see a report from Hystrix that the movie-cast-service circuit is open (meaning that the target is down).
- Open a browser to http://localhost:8080/actuator/hystrix.stream. You should see a never ending stream of data from Hystrix about the status of the circuit breakers. This is hard to grasp, so we’ll use the Hystrix dashboard.
Hystrix Dashboard
There is a simple Spring Boot starter that will stand up a Hystrix server with almost no coding required. We’ll use the Spring initializer web site to create the server.
- Navigate to https://start.spring.io
- Create a Maven project with Java and the latest version of Spring Boot (2.1.1 at the time of writing)
- Specify group:
microservice.workshop
- Specify artifact:
hystrix-dashboard
- For dependencies, add the following:
- Hystrix Dashboard
- Generate the project (causes a download)
- Unzip the downloaded file somewhere convenient
- Add the new project to your IDE workspace
- Eclipse: File->Import->Existing Maven Project
- IntelliJ: File->New->Module From Existing Sources…
- VS Code: File->Add Folder to Workspace
- Open
microservice.workshop.hystrixdashboard.HystrixDashboardApplication.java
and add the@EnableHystrixDashboard
annotation to the class - Navigate to
src/main/resources
, then renameapplication.properties
toapplication.yml
-
Enter the following values in
application.yml
:server: port: 9090
- Start the
hystrix-dashboard
application - Open the Hystrix dashboard at http://localhost:9090/hystrix
- The dashboard needs to know what stream to consume. Enter
http://localhost:8080/actuator/hystrix.stream
, then press theMonitor Stream
button. You should see the Hystrix dashboard with the three circuits displayed. The movie cast circuit is open, the other two are closed. - Once you feel comfortable with the information shown on this screen, restart the
movie-cast-service
application. It may take a minute or so, but you should eventually see the circuit close in the dashboard and see movie cast information being returned in the traffic simulator.