June 21, 2024
Hystrix Circuit Breaker Step by Step with Spring Boot4

Hystrix Circuit Breaker – Step by Step Configuration With Feign Client – Part 4 (Hystrix Dashboard)

This is part  4 of our Hystrx Circuit Breaker Configuration. In this part, we would configure the Hystrix Dashboard. This follows from Part 1, Part 2 and Part 3.

Hystrix Dashboard provides a graphical display to help you monitor the state of the circuit breaker.

 

What we would do

We could modify the ProductWeb microservices to enable Hystrix Dashboard. However, I would recommend that instead of tampering with the existing ProductWeb, simply create a second one and name it ProductWeb2.

  1. Modify the pom.xml File
  2. Modifying the Application Structure
  3. Change the ProductRestController
  4. Add Some Configuration
  5. On to Some Testing!

Let’s start!

 

1. Modify the pom.xml File

First, change the spring cloud version to Hoxton.M3 in the <properties> tag. So you’ll have a markup as shown below:

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.M3</spring-cloud.version>
</properties>

Then you need to add two more dependencies: the hystrix-dashboard and the actuator dependencies as shown below:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 

Next, you need to open the main application class and add additional annotations in addition to the @SpringBootApplication annotation. Here are all the annotations:

@SpringBootApplication
@EnableCircuitBreaker
@EnableHystrixDashboard
@Configuration
@EnableHystrix

At this point, try to run..

 

2. Modifying the Application Structure

Recall that in the previous architecture, we added a fallback to the ProductServiceProxy. So if a call made by this proxy fails to get through, then it falls back to the ProductServerComponent which then routes through the ProductAlternateServiceProxy.

In the new design, we would remove the fallback from the ProductServiceProxy. The fallback would now be handled by the Component class. For each method in the component, there’s a fallback method. The normal method uses the ProductServiceProxy to connect to the Product Server 1 while the second(fallback) method uses the ProductAlternateServiceProxy to connect to Product Server 2.

So you will annotate the methods with the @HystrixCommand annotation. This annotation takes a parameter, fallback which has a value of the matching fallback method (which doesn’t need to be annotated).

Therefore the content of the the component class would now be as shown below:

@Override
@HystrixCommand(fallbackMethod = "getAllTheProducts")
public List<Product> getAllProducts() {
    return productServiceProxy.getAllProducts();
}

@Override
@HystrixCommand(fallbackMethod = "getTheProduct")
public Product getProduct(String productId) {
    return productServiceProxy.getProduct(productId);
}

public List<Product> getAllTheProducts() {
    logger.info("Delegating to another Server...");
    return productAlternateServiceProxy.getAllProducts();
}

public Product getTheProduct(String productId) {
    logger.info("Delegating to another server...");
    return productAlternateServiceProxy.getProduct(productId);
}

 

Of course, you need to wire the proxies into this class. This is already know how to do: just declare two private variables of the two proxies, then create the constructor with the parameters.

 

3. Changes to the ProductRestController

In the previous ProductWeb, the controller makes calls using the ProductServiceProxy. In this modification, the controller interactions would now be through the ProductServerComponent.

This is shown below, after you’ve wired in the ProductServerComponent

@Override
@GetMapping("/products")
public List<Product> getAllProducts() {

    return productServerComponent.getAllProducts();
}

@Override
@GetMapping("/products/{productId}")
public Product getProduct(@PathVariable("productId") String productId) {
    return productServerComponent.getProduct(productId);
}

 

4. Add Some Configuration

Finally, add the following configuration to the application.properties file. Fairly the same as previously, but notice the last line with exposes the the endpoints. Actually the particular endpoint we want to expose it hystrix.stream.

server.port=8091
feign.hystrix.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000
spring.main.allow-bean-definition-overriding=true
spring.application.name=ProductWeb2

management.endpoints.web.exposure.include=*
#or
#management.endpoints.web.exposure.include=hystrix.stream

 

5. On to some testing!

To do the testing, fire up all the applications.

Visit http://localhost:8090/hystrix

You will see the Hystrix Dashboard as shown below:

 

Hystrix Dashbaord
Hystrix Dashbaord

 

In the field at the lower part under Hystrix Dashboard, enter the stream url

http://localhost:8091/actuator/hystrix.stream

In the second field, enter the text Product Web

Then click on “Monitor Stream”. The stream dashboard monitoring screen  is displayed as shown below:

 

Hystrix Stream Dashaboard
Hystrix Stream Dashaboard

 

Now go and bring down the Product Server and come back to check the stream output.

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] topic on client-side load balancing actually follows from the Part 4 of Hystrix Circuit Breaker – Step by Step Configuration With Feign Client .  Actually, the Feign client already uses Ribbon which is  a component of Spring […]