<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mandip Silwal</title>
    <description>The latest articles on DEV Community by Mandip Silwal (@mannik01).</description>
    <link>https://dev.to/mannik01</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F275418%2F4951c0d7-a00e-40e2-a24f-4d4a50890e47.jpeg</url>
      <title>DEV Community: Mandip Silwal</title>
      <link>https://dev.to/mannik01</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mannik01"/>
    <language>en</language>
    <item>
      <title>A simple implementation of Circuit Breaker Pattern in Spring Boot</title>
      <dc:creator>Mandip Silwal</dc:creator>
      <pubDate>Sun, 02 Feb 2020 20:21:48 +0000</pubDate>
      <link>https://dev.to/mannik01/a-simple-implementation-of-circuit-breaker-pattern-in-spring-boot-140c</link>
      <guid>https://dev.to/mannik01/a-simple-implementation-of-circuit-breaker-pattern-in-spring-boot-140c</guid>
      <description>&lt;p&gt;This article assumes that you already know basics of Spring Boot :)&lt;/p&gt;

&lt;p&gt;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 &lt;strong&gt;Spring Cloud Netflix Hystrix&lt;/strong&gt; library in Spring Boot to demonstrate this pattern.&lt;/p&gt;

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

&lt;p&gt;Now, let's download the project and create a &lt;strong&gt;InfoController&lt;/strong&gt; class that will have the endpoint which our client service will call. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S7Ov8UXa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kp7o9id7wzjids3ca492.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S7Ov8UXa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kp7o9id7wzjids3ca492.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this, let's create a &lt;strong&gt;InfoService&lt;/strong&gt; class, which has the method to return a string denoting it is a message from the server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sZClPG_2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jdja0nc5wua15gk2chw9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sZClPG_2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jdja0nc5wua15gk2chw9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;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, &lt;strong&gt;Spring Cloud Netflix Hystrix&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's create an &lt;strong&gt;InfoController&lt;/strong&gt; class here as well, like the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nUAPv3Lk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cz7zuhhes43n106szbrs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nUAPv3Lk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cz7zuhhes43n106szbrs.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, let's create an &lt;strong&gt;InfoService&lt;/strong&gt; class, like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z_MuCKIa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n32c1hnwmk6jxclbpgvt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z_MuCKIa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/n32c1hnwmk6jxclbpgvt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see above, we have added a call to the server in the "&lt;strong&gt;getServerInfo()&lt;/strong&gt;" 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 "&lt;strong&gt;getFallBackInfo()&lt;/strong&gt;". This method is referred in the "&lt;strong&gt;getServerInfo()&lt;/strong&gt;" method with the help of the "&lt;strong&gt;@HystrixCommand&lt;/strong&gt;" annotation. This annotation is the key that will get triggered if our server application gets down. &lt;/p&gt;

&lt;p&gt;Another thing we need to do is change the default port for this client application in the &lt;strong&gt;application.properties&lt;/strong&gt; file, like below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kQLVJkvM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rea546r09npwz1mhxodd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kQLVJkvM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rea546r09npwz1mhxodd.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason we are doing this is that server application we build at first is already running on port 8080.&lt;/p&gt;

&lt;p&gt;Another important thing to add is "&lt;strong&gt;@EnableHystrix&lt;/strong&gt;" in the main class of the client application, which will, as the name suggests, enable hystrix fallback in our application. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AdsGTRAq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7758r8oz2fod30yoxdj0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AdsGTRAq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7758r8oz2fod30yoxdj0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To see the normal flow of our client-server system, let's start both of the applications, and hit &lt;a href="http://localhost:8081/info"&gt;http://localhost:8081/info&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;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!&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>hystrix</category>
      <category>java</category>
    </item>
  </channel>
</rss>
