June 21, 2024

Hystrix Circuit Breaker – Step by Step Configuration With Feign Client – Part 2

This is Part 2 of our Hystrix Circuit Breaker configuration. This follows from Part 1.

In this part we would cover the following:

  1. ProductsWeb: Create the Feign Clients
  2. ProductsWeb: Create the Component
  3. ProductsWeb: Write the Controller
  4. Perform Some Tests

 

1. ProductsWeb: Create the Feign Clients

The Feign Clients are classes that derive from the ProductService.java classes and have the @FeignClient annotation.

The ProductServiceProxy.java class is given below. You can see that a fallback component is provided for ProductServiceProxy. Requests handles by this proxy route to the url provided. If it fails, then the fallback component takes over.

@FeignClient(
        name="product-proxy",
        url = "http://localhost:8081",
        fallback = ProductAlternateServerComponent.class)
public interface ProductServiceProxy extends ProductService {
}

 

The ProductAlternateServiceProxy.java is the proxy that would be used when the ProductServiceProxy fails.

@FeignClient(
        name = "product-alternate-proxy",
        url = "http://localhost:8082")
public interface ProductAlternateServiceProxy extends ProductService {
}

Notice that the url for this client is the url for Product Server 2 we built earlier

 

2. ProductsWeb: Create the Component

This is the component that implements takes care of routing the calls to the ProductAlternateServiceProxy. This class implements the ProductServiceProxy and has the same methods. However, these methods are implemented using the ProductAlternateServiceProxy which is wired into it.

Here’s the annotations for this class:

@EnableFeignClients(basePackageClasses = ProductAlternateServiceProxy.class)
@ComponentScan(basePackageClasses = ProductAlternateServiceProxy.class)
@Component

Note that for the @EnableFeignClients() annotation, you need to provide the service to be used as fallback. @ComponentScan annotation would scan the packages for beans of type FeignClient. However, if you don’t provide any arguments, the the base package and subpackages would be included in the scan.

Then you do to do a constructor injection of the ProductAlternateServiceProxy into this class

Finally, you have below, the two method implementation as shown below:

@Override
public List<Product> getAllProducts() {
    logger.info("Delegating to the Indian Server...");
    return productAlternateServiceProxy.getAllProducts();
}

@Override
public Product getProduct(String productId) {
    logger.info("Delegating to the Indian Server...");
    return productAlternateServiceProxy.getProduct(productId);

}

I have added a logger message to indicate that the a fallback to the alternate service is taking place.

 

3. ProductsWeb: Write the Controller

Now, we need to write the RestController. This class should implement the ProductService. This is because the method we that are exposed are defined in the ProductService.

As you know, this is a very important piece because, this is where request coming from a UI app would first hit. The @CrossOrigin annotation tells this controller to allow requests coming from external hosts (in this case, the Angular UI app we’ll build in Part 3).

The @RestController, this you already know. Below is the complete annotations:

@RestController
@CrossOrigin
@EnableFeignClients(basePackageClasses = ProductServiceProxy.class)
@ComponentScan(basePackageClasses = ProductServiceProxy.class)

Then you need to do a constructor injection of the ProductServiceProxy class.

Finally, implement the two methods as shown below:

@Override
@GetMapping("/products")
public List<Product> getAllProducts() {
    return productServiceProxy.getAllProducts();
}

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

 

4. Perform Some Tests

At this point, we have completed most part of the architecture. So you can now test the whole system.

  • Fire up ProductServer 1
  • Fire up ProductServer 2
  • Fire up ProductWeb

Once all the services have started up and running, browse to http://localhost:8090/products. You will see a list of products returned as JSON. Leave the page open

Next, go to ProductServer 1 and shut it down. Then go back to the page(http://localhost:8090/products) and refresh it. You will notice that data still comes back, but this time from Product Server 2.

Then take a look at the console window of ProductWeb. You will notice that the text “Delegating to another server…” is displayed.

If you got here successfully, then congrats! You can then move on the Part 3 so we can build the Angular UI application.

If however you have challenges, then:

  • let me know it a comment
  • watch the video and follow along with me
0 0 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] ProductsWeb: Create the Feign Clients […]

trackback

[…] Configuration. In this part, we would configure the Hystrix Dashboard. This follows from Part 1, Part 2 and Part […]

trackback

[…] This if Part 3 of or Hystrix Circuit Breaker microservices architecture. It follows from Part 1 and Part 2. […]