Adding the Hystrix annotation in services

Netflix Hystrix provides an annotation, @HystrixCommand, which we can use at the service layer to add the functionality of the circuit-breaker pattern. Let's see the following code:

package com.dineshonjava.customerservice.service; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.cloud.client.loadbalancer.LoadBalanced; 
import org.springframework.stereotype.Service; 
import org.springframework.web.client.RestTemplate; 
 
import com.dineshonjava.customerservice.domain.Account; 
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 
 
@Service 
public class AccountServiceImpl implements AccountService { 
    
   @Autowired 
   @LoadBalanced 
   RestTemplate restTemplate; 
    
   @HystrixCommand(fallbackMethod = "defaultAccount") 
   public List<Account> findByCutomer(Integer customer) { 
          
         //do stuff that might fail 
         return restTemplate.getForObject("http://ACCOUNT-              
SERVICE/account/customer/{customer}", List.class, customer); } public List<Account> defaultAccount() { /* something useful */; return new ArrayList<>(); } }

We have used an annotation, @HystrixCommand(fallbackMethod = "defaultAccount"), on top of the findByCutomer(Integer customer) method. And the fallbackMethod attribute denotes the defaultAccount() method for a fallback condition. The fallback method can have any access modifier. As you know, Netflix has produced a very powerful library for fault tolerance. Hystrix allows you to wrap the code in HystrixCommand objects after wrapping that code in a circuit-breaker.

Spring Cloud creates a proxy for those Spring Beans that are annotated with the @HystrixCommand annotation, and that proxy is connected to the Hystrix circuit-breaker. And that circuit-breaker monitors when to open and close the circuit, and also takes decisions in the case of a failure to perform an action. You can also use the commandProperties attribute with a list of @HystrixProperty annotations to configure @HystrixCommand.

It's important to remember that the Hystrix command and fallback should be placed in the same class and have the same method signature (optional parameter for failed execution exception).

As we have declared a method, defaultAccount, it will be used to process fallback logic in case of any errors. If you need to run the defaultAccount fallback method as separate Hystrix command, then you need to annotate it with the HystrixCommand annotation:

 @HystrixCommand(fallbackMethod = "defaultAccount") 
 public Account getAccountById(String id) { 
    return accountService.getAccountById(id); 
 } 
 
 @HystrixCommand 
 private Account defaultAccount(String id) { 
   return new Account(); 
 }

As you can see, we have marked a fallback method with @HystrixCommand, now this defaultAccount fallback method also has another fallback method:

@HystrixCommand(fallbackMethod = "defaultAccount") 
    public Account getAccountById(String id) { 
        return accountService.getAccountById(id); 
    } 
 
    @HystrixCommand(fallbackMethod = "defaultUserSecond") 
     private Account defaultAccount(String id) { 
        return new Account(); 
    } 
     
    @HystrixCommand 
    private Account defaultAccountSecond(String id) { 
        return new Account("1002", "2000"); 
    } 

We have declared second a defaultAccountSecond fallback method as a fallback method of the first defaultAccount fallback method.

The Hystrix library also allows you to pass the extra parameter in order to get an exception thrown by a command. Let's see the following example:

@HystrixCommand(fallbackMethod = "fallback1") 
public Account getAccountById(String id) { 
   throw new RuntimeException("getAccountById command raised error"); 
} 
 
@HystrixCommand(fallbackMethod = "fallback2") 
Account fallback1(String id, Throwable e) { 
   throw new RuntimeException("fallback1 raised error"); 
} 
 
@HystrixCommand(fallbackMethod = "fallback3") 
Account fallback2(String id) { 
   throw new RuntimeException("fallback2 raised error"); 
} 
 
@HystrixCommand(fallbackMethod = "staticFallback") 
Account fallback3(String id, Throwable e) { 
   throw new RuntimeException("fallback3 raised error"); 
} 
 
Account emptyObjectFallback(String id, Throwable e) { 
   return new Account(); 
}

This code has several fallback methods, with an extra parameter of the Throwable type and each fallback has its own fallback method with an extra Throwable parameter to propagate an exception at the command to the fallback method.

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

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