View on GitHub

Microservice Resiliency Workshop

Java/Spring Edition

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.

  1. Open pom.xml and uncomment the dependency for spring-cloud-starter-netflix-hystrix
  2. Open src/main/resources/application.yml and uncomment the properties related to Hystrix
  3. Make a new package microservice.workshop.movieaggregatorservice.service.fallback
  4. Make a new class MovieAwardServiceFallback in the fallback package. The class should implement the MovieAwardService interface, and return an empty List for the single method
  5. Make a new class MovieCastServiceFallback in the fallback package. The class should implement the MovieCastService interface, and return an empty List for the single method
  6. Make a new class MovieServiceFallback in the fallback package. The class should implement the MovieService interface, and return an empty Optional for the single method
  7. Open the MovieAwardService interface and alter the @FeignClient annotation to add the fallback implementation
  8. Open the MovieCastService interface and alter the @FeignClient annotation to add the fallback implementation
  9. Open the MovieService interface and alter the @FeignClient annotation to add the fallback implementation
  10. Open MovieAggregatorServiceApplication and make the following changes:
    1. Add the @EnableCircuitBreaker annotation to the class
    2. 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();
       }
      
  11. Start the movie-aggregator-service and the traffic simulator. Everything should look normal.
  12. Kill the movie-cast-service application. You should see that the aggregator service is still running, but not returning cast members.
  13. 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).
  14. 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.

  1. Navigate to https://start.spring.io
  2. Create a Maven project with Java and the latest version of Spring Boot (2.1.1 at the time of writing)
  3. Specify group: microservice.workshop
  4. Specify artifact: hystrix-dashboard
  5. For dependencies, add the following:
    • Hystrix Dashboard
  6. Generate the project (causes a download)
  7. Unzip the downloaded file somewhere convenient
  8. 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
  9. Open microservice.workshop.hystrixdashboard.HystrixDashboardApplication.java and add the @EnableHystrixDashboard annotation to the class
  10. Navigate to src/main/resources, then rename application.properties to application.yml
  11. Enter the following values in application.yml:

     server:
       port: 9090
    
  12. Start the hystrix-dashboard application
  13. Open the Hystrix dashboard at http://localhost:9090/hystrix
  14. The dashboard needs to know what stream to consume. Enter http://localhost:8080/actuator/hystrix.stream, then press the Monitor Stream button. You should see the Hystrix dashboard with the three circuits displayed. The movie cast circuit is open, the other two are closed.
  15. 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.