A simple implementation of Circuit Breaker Pattern in Spring Boot
Tags: springboot, hystrix, java
Published: 2020-02-02 20:21:48

This article assumes that you already know basics of Spring Boot :)

Circuit Breaker pattern is a way of preventing failures in a software system caused due to failed remote calls to another service. In this article, we are going to see how to implement **Spring Cloud Netflix Hystrix** library in Spring Boot to demonstrate this pattern.

First of all, we need to setup a service which is going to be called by a client. We can go to **start.spring.io** to bootstrap a Spring Boot project with Spring Web as a dependency. 

Now, let's download the project and create a **InfoController** class that will have the endpoint which our client service will call. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kp7o9id7wzjids3ca492.png)

After this, let's create a **InfoService** class, which has the method to return a string denoting it is a message from the server.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jdja0nc5wua15gk2chw9.png)

At this point, we are basically done with the server implementation. If we start this service and hit http://localhost:8080/info in the browser, we see "Hi, this message is from a Spring Boot service." as the response.

Okay, now, we need to create a client service to consume the endpoint response from above service. Same as above, let's bootstrap another Spring Boot service but with an additional dependency, **Spring Cloud Netflix Hystrix**.

Let's create an **InfoController** class here as well, like the following:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cz7zuhhes43n106szbrs.png)

Also, let's create an **InfoService** class, like below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/n32c1hnwmk6jxclbpgvt.png)

As we can see above, we have added a call to the server in the "**getServerInfo()**" method. This will return "Hi, this message is from a Spring Boot service." response, if the service is working fine. However, in case the service does not respond, due to variety of reasons, we have defined a fallback method "**getFallBackInfo()**". This method is referred in the "**getServerInfo()**" method with the help of the "**@HystrixCommand**" annotation. This annotation is the key that will get triggered if our server application gets down. 

Another thing we need to do is change the default port for this client application in the **application.properties** file, like below. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rea546r09npwz1mhxodd.png)

The reason we are doing this is that server application we build at first is already running on port 8080.

Another important thing to add is "**@EnableHystrix**" in the main class of the client application, which will, as the name suggests, enable hystrix fallback in our application. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7758r8oz2fod30yoxdj0.png)

To see the normal flow of our client-server system, let's start both of the applications, and hit http://localhost:8081/info endpoint in the browser. In this scenario, we will see "Hi, this message is from a Spring Boot service." as the response. Now, to see the Circuit Breaker in action, we need to stop the server application. Once we do this and call the above endpoint from the client, we will see "Hi, this is the fallback info from the server." as response, which is defined in our fallback method in the client application.

That is all! This is supposed to be a naive application of hystrix library from Netflix, so I have kept all the project structure simple. I hope this article is helpful!