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

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

In this tutorial, we would proceed on a step-by-step development of a Circuit Breaker Architecture using with Spring Boot. We would use the following tools:

  • Spring Boot for developing the servers
  • Feign Client for Microservices Communication
  • Hystrix Circuit Breaker for handling failures
  • Angular for building the UI application

Our architecture would consist of four microservices. See the Architecture here.

  • Primary Server(ProductServer1 port 8081)
  • Secondary Server (ProductServer2 port 8082)
  • Web API Application (ProductWeb port 8090)
  • UI Application (ProductsUI port 8091)

 

We’ll proceed using the following steps

  1. Build the two servers
  2. ProductsWeb: Build the Web API Application
  3. ProductsWeb: Setup the Model Class(Product.java)
  4. ProductsWeb: Modify the pom.xml (important)
  5. ProductsWeb: Configure the properties
  6. ProductsWeb: Create the api(ProductService)
  7. ProductsWeb: Create the Feign Clients
  8. ProductsWeb: Create the Component
  9. ProductsWeb: Write the Controller
  10. Build the Angular UI application
  11. Perform Some Tests

 

1. Build the two servers

Create a Spring application using IntelliJ IDE or Spring Tool Suite. This application would stored data in MongoDB, so add the MongoDB and RestRepositories dependency.

Create the following files:

  • Product.java: this is the model file annotated with the @Data annotation
  • ProductRepository.java: an interface that extends MongoRepository and annotated with @RestRepository annotation
  • ProductRestController.java: contains the getAllProducts() and getProduct() methods and annotated with @RestController
  • InitializationComponent.java: a component file containing the inti() method which is annotated with @PostConstruct.

In the application.properties file add url for the MongoDB database as well as the server.port=8081

Server 2: Repeat the same procedure to build the second server. The port should be 8082

Start up both servers and make a get request to http://localhost:8081/products. Do same for the second server.

 

2. ProductsWeb: Build the Web API Application

We’ll spend a bit of time with the ProductWeb. Also note the the Feign Client and Hystrix is done here. We’ll modify this file in he next part to be able to see the Hystrix Dashboard.

So create a new application named ProductWeb. The project can be in the same directory as the two servers you created.

 

3. ProductsWeb: Modify the pom.xml (important)

First add the spring cloud version in the <properties> section of the pom.xml. The version should be Finchley.RELEASE like so:

<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

 

Add the feign and hystrix dependencies like shown below:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

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

 

Then you also need to add the dependency management section as shown below:

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>${spring-cloud.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

 

Finally, add the repositories section as shown below:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

 

4. ProductsWeb: Configure the properties

We would add a number of conguration settings to the application.properties files. I have numbered the linesĀ  of code to help you understand the the each of the parameters.

1
2
3
4
5
6
server.port=8090
feign.hystrix.enabled=true
hystrix.command.default.execution.isolation.thread.
timeoutInMilliseconds=2000
spring.main.allow-bean-definition-overriding=true
spring.application.name=ProductWeb

Line 1: Sets the application to run on port 8090

Line 2: Enables hystrix use of feign clients

Line 3,4: Sets the timeout after which the caller would observe a timeout and then falls back.

Line 5: Allows for definition of a bean within the application context which has the same name as another bean

Line 6: Simply sets the name of the microservice

 

5. ProductsWeb: Setup the Model Class(Product.java)

The Product.java is exactly the same as in the Server 1 and 2 earlier.

 

6. ProductsWeb: Create the api(ProductService)

This is a service is an interface that contains the methods that are available to clients. Here we’ll have two definition of two methods getAllProducts() and getProduct(). This methods are overriden by by the Proxies that extend this class.

The content of the ProductService.java is shown below:

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

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

 

I would like to continues from Part 2 where we would create the services and the components. Then in Part 3, we fire up the services and perform some tests.

Do leave a comment if you have any challenges.

Thanks.

4 1 vote
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

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

trackback

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

trackback

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