DEV Community

Mandip Silwal
Mandip Silwal

Posted on

A simple implementation of Circuit Breaker Pattern in Spring Boot

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

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

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

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

Alt Text

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

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

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!

Top comments (1)

Collapse
 
ashgia profile image
ashgia

great article!