A circuit breaker code example

As you may have guessed, the circuit breaker is a very important pattern when it comes to handling service failures. It is worth spending some more time understanding how to implement it. Let's take a hypothetical problem; say we have a movie recommendation service. Users will make a call to our service, which in turn gets the recommendation for the current user from a sophisticated machine learning implementation. In case the external recommendation service goes down, we still want our users to be able to get recommendations.

We will try implementing the following solution:

To get started, we will create a couple of Spring Boot applications, as will do in the rest of the book. Here is the recommendation engine service:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/***
* This class is responsible for applying machine learning algorithm for
* the current user and find out personalized algorithm.
*
*/
@RestController
public class RecommendationController {

private final RestTemplate restTemplate;

public RecommendationController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

/***
* This method calculates and return movies recommendation
* @return Movies
*/
@GetMapping("/movies")
public String getRecommendedMovies() {
// Applies number cruncher algo and come up with movies for logged in used
// For sake of thie example we are returning fixed set of movies
return("Movies Recommended for you. 1. Jumanji: Welcome to the Jungle. 2. Inception 3. The Dark knight.");
}

}

But we are more interested in the calling method, where we will implement our circuit breaker. We will include a Netflix Hystrix implementation to use the circuit breaker:

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

And here is the controller API and service class that implements the circuit breaker:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@EnableCircuitBreaker
@RestController
@SpringBootApplication
public class ServiceoneApplication {

@Autowired
private RecommendationService recommendationService;

public static void main(String[] args) {
SpringApplication.run(ServiceoneApplication.class, args);
}

@Bean
public RestTemplate rest(RestTemplateBuilder builder) {
return builder.build();
}

@RequestMapping("/movies")
public String getRecommendedMovies() {
return recommendationService.recommend();
}
}

The following is the for the recommendation service:


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@Service
public class RecommendationService {

private final RestTemplate restTemplate;

public RecommendationService(RestTemplate rest) {
this.restTemplate = rest;
}

@HystrixCommand(fallbackMethod = "reliable")
public String recommend() {
URI uri = URI.create("http://localhost:8081/movies");
return this.restTemplate.getForObject(uri, String.class);
}

public String reliable() {
return "Top 3 Movies for the month. 1. Batman Begins 2. Interstellar 3. Justice League. ";
}

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.30.236