How to do it...

Implement recovery transactions by following these steps:

  1. Create a new Maven project ch10-hystrix and add the core starter POM dependencies of Spring Boot 2.0 such as the webflux, embedded Tomcat server and the actuator. To import Hystrix modules, add first the Spring Cloud Finchley dependency plugin to pom.xml.
  2. Then, add the Spring Cloud Netflix Hystrix dependencies to pom.xml:
<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-hystrix</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-hystrix-dashboard 
</artifactId> 
</dependency> 
Inside the core org.packt.microservice.hystrix package, create the bootstrap class that enables Hystrix circuit breaker feature using @EnableCircuitBreaker and @EnableHystrix: 
@SpringBootApplication 
@EnableCircuitBreaker 
@EnableHystrix 
public class ConsumeHystrixBootApplication 
extends  SpringBootServletInitializer  { 
   
  @Override 
    protected SpringApplicationBuilder configure( 
SpringApplicationBuilder application) { 
        return application.sources( 
ConsumeHystrixBootApplication.class); 
    } 
 
    public static void main(String[] args) throws Exception { 
        SpringApplication.run(ConsumeHystrixBootApplication.class, 
      args); 
    } 
} 
  1. Inside the core org.packt.microservice.hystrix package, now create the bootstrap class that enables Hystrix circuit breaker feature using @EnableCircuitBreaker and @EnableHystrix:
@SpringBootApplication 
@EnableCircuitBreaker 
@EnableHystrix 
public class ConsumeHystrixBootApplication  
extends  SpringBootServletInitializer  { 
    
   @Override 
    protected SpringApplicationBuilder configure( 
SpringApplicationBuilder application) { 
        return application.sources( 
ConsumeHystrixBootApplication.class); 
    } 
 
    public static void main(String[] args)  
throws Exception { 
        SpringApplication.run( 
ConsumeHystrixBootApplication.class, args); 
    } 
} 
  1. Inside src/main/resources, create the application.properties file that contains the basic Tomcat server deployment properties, just like in the previous recipes.
  2. Afterwards, create a package, org.packt.microservice.hystrix.config, which contains a webflux configuration class with the injected RestTemplate and AsyncRestTemplate:
@Configuration 
@EnableWebFlux 
public class WebfluxConfig { 
   
  @Bean 
    public RestTemplate restTemplate() { 
        return new RestTemplate(); 
    } 
   
  @Bean 
  public AsyncRestTemplate asyncRestTemplate(){ 
    AsyncRestTemplate art = new AsyncRestTemplate(); 
    return art; 
  } 
} 
  1. Copy and add to a new package, org.packt.microservice.hystrix.model.data, all the needed entity models for JSON encoding/decoding.
  2. Now, the essential part of this application is in all the @Service found in org.packt.microservice.hystrix.service; this recipe is after creating recovery transactions called fallback methods for each endpoint service. Each fallback method is triggered every moment the service method it is assigned to reaches its tolerance level and starts emitting exceptions and errors. The following is a DeptHystrixService that consists of circuit-aware executions of RESTful services from the DEPARTMENT microservice:
@Service 
public class DeptHystrixService { 
   
      @HystrixCommand(fallbackMethod = "defaultSelectDept") 
      public  Mono<Department> getMonoDept(Integer id) { 
          return WebClient.create().method(HttpMethod.GET) 
                 .uri("http://localhost:8090/ 
ch10-dept/selectReactDept/" + id) 
                 .contentType( 
     MediaType.APPLICATION_OCTET_STREAM) 
.retrieve().bodyToMono(Department.class); 
      } 
   
      private Mono<Department> defaultSelectDept(Integer id) { 
        Mono<Department> blankDept = 
Mono.justOrEmpty(new Department()); 
        return blankDept ; 
      } 
} 
Not all exceptions and errors will lead to the execution of the fallbackMethod. Since Hystrix supports synchronous, asynchronous and reactive endpoint calls, the fault tolerance level depends on the type of endpoint executions. Moreover, fallbackMethod is triggered by unsuccessful execution and not explicitly called.
  1. Create also EmpHystrixService for Employee microservice based on the service methods of its previous recipes.
  1. Create also LoginHystrixService for Login microservice based from the service methods of its previous recipe.
  2. To apply all these circuit-aware service methods, create a typical Controller, as shown below:
@Controller 
public class HystrixClientController { 
   
    @Autowired 
    private DeptHystrixService deptHystrixService; 
     
    @Autowired 
    private EmpHystrixService empHystrixService; 
     
    @Autowired 
    private LoginHystrixService logniHystrixService; 
      
    @RequestMapping("/hystrixUsers") 
    @ResponseBody 
    public List<UserDetails> hystrixGetUsers(){ 
      return logniHystrixService.getLoginUsers(); 
    } 
         
    @RequestMapping("/hystrixGetEmp/{id}") 
    @ResponseBody 
    public Employee hystrixSelectEmp( 
@PathVariable("id") Integer id){ 
      return empHystrixService.getAsyncEmp(id); 
    } 
         
    @RequestMapping("/hystrixGetDept/{id}") 
    @ResponseBody 
    public Mono<Department> hystrixSelectDept( 
@PathVariable("id") Integer id){ 
      return deptHystrixService.getMonoDept(id); 
    } 
} 
  1. Save all files. Run clean spring-boot:run -U and then open a browser to execute all HystrixClientController transactions.
  2. Hystrix has a built-in dashboard that helps experts in monitoring the closed-open circuit status of each service. To enable this dashboard, just add the following starter POM in the pom.xml:
<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-hystrix-dashboard 
</artifactId> 
</dependency>
Be sure to include a working Spring Boot actuator module in the application.
  1. Shut down the application using the curl command. Start again and access the Hystrix dashboard at http://localhost:8790/ch10-hystrix/hystrix.stream.
  1. Insert http://localhost:8790/ch10-hystrix/hystrix.stream on the Dashboard and click Monitor Stream button to examine all the circuits.
..................Content has been hidden....................

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