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 in the REST Template Client Project
All changes will be made in the movie-aggregator-service-rt project.
- Open the
MovieAwardServiceclass and- add a method that returns an empty list of movie awards
- add the
@HystrixCommandannotation to thefindAwardsForMoviemethod and specify the fallback method you just created
- Open the
MovieCastServiceinterface and- add a method that returns an empty list of movie cast members
- add the
@HystrixCommandannotation to thefindCastMembersmethod and specify the fallback method you just created
- Open the
MovieServiceinterface and- add a method that returns a null movie
- add the
@HystrixCommandannotation to thefindByIdmethod and specify the fallback method you just created
- Open
MovieAggregatorServiceRtApplicationand add the@EnableCircuitBreakerannotation to the class - Start the
movie-aggregator-service-rtand the traffic simulator. Everything should look normal. - Kill the
movie-cast-serviceapplication. 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.
Enable Hystrix in the Feign Client Project
All changes will be made in the movie-aggregator-service project.
- Open
src/main/resources/application.ymland setfeign.hystrix.enabledtotrue - Make a new package
microservice.workshop.movieaggregatorservice.service.fallback - Make a new class
MovieAwardServiceFallbackin the fallback package. The class should implement theMovieAwardServiceinterface, and return an empty List for the single method - Make a new class
MovieCastServiceFallbackin the fallback package. The class should implement theMovieCastServiceinterface, and return an empty List for the single method - Make a new class
MovieServiceFallbackin the fallback package. The class should implement theMovieServiceinterface, and return an empty Optional for the single method - Open the
MovieAwardServiceinterface and alter the@FeignClientannotation to add the fallback implementation - Open the
MovieCastServiceinterface and alter the@FeignClientannotation to add the fallback implementation - Open the
MovieServiceinterface and alter the@FeignClientannotation to add the fallback implementation - Open
MovieAggregatorServiceApplicationand add the@EnableCircuitBreakerannotation to the class - Start the
movie-aggregator-serviceand the traffic simulator. Everything should look normal. - Kill the
movie-cast-serviceapplication. 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 Gradle project with Kotlin and the latest version of Spring Boot (2.2.0 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.ktand add the@EnableHystrixDashboardannotation to the class - Navigate to
src/main/resources, then renameapplication.propertiestoapplication.yml -
Enter the following values in
application.yml:server: port: 9090 - Start the
hystrix-dashboardapplication - 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 Streambutton. 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-serviceapplication. 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.