Use Hystrix and Hystrix Dashboard from Spring Cloud Netflix in the Reactive Web Service application with Spring WebFlux

I have introduced you to Hystrix and Hystrix Dashboard with the problems that they solve in a Microservices system. For Reactive Web Service applications, using Hystrix and Hystrix Dashboard will be a little different. Currently, the @HystrixCommand annotation will not work with the Reactive Web Service applications, we have to use the HystrixCommands class to solve our problems. How is it in details? In this tutorial, I will show you how to use Hystrix and Hystrix Dashboard from Spring Cloud Netflix in the Reactive Web Service application with Spring WebFlux!

For this example, I will use the Reactive Web Service I created in the tutorial about Reactive Web Service using Spring WebFlux.

I will expose an additional URL request in this Reactive Web Service:

I will create another application using WebClient to consume the Reactive Web Service with Hystrix and Hystrix Dashboard as follows:

Use Hystrix and Hystrix Dashboard from Spring Cloud Netflix in the Reactive Web Service application with Spring WebFlux

Result:

Use Hystrix and Hystrix Dashboard from Spring Cloud Netflix in the Reactive Web Service application with Spring WebFlux

Next, I will add a new URL request using WebClient to consume Reactive Web Service in this tutorial. My controller has the following content:

Since the Reactive Web Service is currently running on port 8080, I will add the server.port property in the application.properties file of this application with the value of 8081 so this application runs on another port.

Run both applications and the results when I request to the address http://localhost:8081/consume-hello will be as follows:

Use Hystrix and Hystrix Dashboard from Spring Cloud Netflix in the Reactive Web Service application with Spring WebFlux

OK, now I will use Hystrix to implement Circuit Breaker, when Reactive Web Service is not available, our application will return to your default value.

To do this, I will add annotation @EnableCircuitBreaker into the main class of the application as follows:

As I said, @HystrixCommand annotation currently does not work with Spring WebFlux, we will use HystrixCommands class as follows:

As you can see, using HystrixCommands we will create a fallback from the returned Flux or Mono object. Command name here is the name that will be used to distinguish, so we can know which calling request URL is having problems.

If you now stop the Reactive Web Service, restart the application and request the URL http://localhost:8081/consume-hello, the result will be as follows:

Use Hystrix and Hystrix Dashboard from Spring Cloud Netflix in the Reactive Web Service application with Spring WebFlux

Now let’s try to configure you to use Hystrix Dashboard.

We need to add the @EnableHystrixDashboard annotation in the main class of the application as follows:

Next, we will configure the Actuator enable API to provide data for Hystrix Dashboard in application.properties file as follows:

OK, now try accessing the URL http://localhost:8081/hystrix, enter http://localhost:8081/actuator/hystrix.stream, press “Monitor Stream” and then request to the address http://localhost:8081/consume-hello, you will see Hystrix Dashboard display as follows:

Use Hystrix and Hystrix Dashboard from Spring Cloud Netflix in the Reactive Web Service application with Spring WebFlux

Start the Reactive Web Service again, you will see the following result in Hystrix Dashboard when requesting to http://localhost:8081/consume-hello:

Use Hystrix and Hystrix Dashboard from Spring Cloud Netflix in the Reactive Web Service application with Spring WebFlux

No errors are displayed, but it seems that with Spring WebFlux, Hystrix Dashboard has not yet displayed the number request of success.

Add Comment