Handling partial failures

The microservice-based application is a distributed application, but as there are many microservices that run on the cloud or different VMs, we have to design an API gateway so that it can handle the problem of partial failure. Note that, in a distributed system, one microservice calls another microservice.

It might be possible that a microservice is either responding slowly or is unavailable, but its calls are not blocked by an API gateway over its non-response. Instead, the API gateway must sometimes handle these failures.

For example, let's say an order details page also includes a shipping company's ratings
generated by another rating microservice. If that rating microservice is unresponsive in the order details rendering, then the API gateway must return the rest of the order information to the client. In this case, the rating of the shipping company can either be empty or replaced by a hardcoded value. If any important microservice, such as an order service or account service, is unresponsive, the API gateway should return an error to the client.

Netflix provides a library called Hystrix, which is a very useful library for handling partial failure in a distributed application. Spring Cloud supports Netflix's Hystrix library, and we can easily use this with the Spring Boot-based microservice application.

In the Hystrix API, you can also set a timeout property that specifies the threshold for exceeding. If this timeout is reached, a circuit breaker will open and stop the client from needlessly waiting for an unresponsive service. After reaching a defined threshold for a service's error rate, Hystrix will also open the circuit breaker and all requests will fail for a specific time. You can define your logic for the Hystrix's fallback method so that the value is taken from a cache or returned from a default value.

The following code demonstrates how to use Spring Cloud with Netflix's Hystrix library to handle partial failure:

@Service
public class AccountServiceImpl implements AccountService {
@Autowired
@LoadBalanced
RestTemplate restTemplate;

@HystrixCommand(fallbackMethod = "defaultAccount")
public Account findAccount(Integer accountId) {
return restTemplate.getForObject("http://ACCOUNT-SERVICE/account/{accountId}", Account.class, accountId);
}

private Account defaultAccount(Integer accountId) {
return new Account(0001, "Rushika Rajput", "Noida", "972XXX2323", "[email protected]");
}
}

In the preceding code snippet, AccountServiceImpl from our online bookshop application uses a circuit breaker to handle failures when invoking a remote service. The AccountServiceImpl class has a method, findAccount(<accountId>), that must be executed using a circuit breaker by annotating with the @HystrixCommand annotation. Another method named defaultAccount(<accountId>) is also defined as a fallback method for the circuit breaker.

The circuit breaker functionality is enabled using the @EnableCircuitBreaker annotation on the OrderServiceApplication class, as follows:

package com.dineshonjava.bookshop.orderservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@EnableEurekaClient
@EnableCircuitBreaker
@EnableHystrixDashboard
@SpringBootApplication
public class OrderServiceApplication {

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

The preceding main class annotates using the @EnableCircuitBreaker and 
@EnableHystrixDashboard annotations. The @EnableCircuitBreaker annotation is used to enable a circuit breaker in the application, while the @EnableHystrixDashboard annotation provides a circuit breaker in the Hystrix dashboard.

Let's now move on to the next section and look at how to build an API gateway using Spring Cloud's Netflix Zuul proxy.

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

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