Spring Cloud (C): Service fault-tolerant protection --Spring Cloud Hystrix

  In the micro-service architecture, the phenomenon usually occurs service is not available, assuming that A is the service provider, the caller B to A services, C, D B services for the caller, then when A service is not available, as the Over time it will lead to B service unavailable, service unavailable B may result in unusable C, D services, eventually leading to the entire system unusable, in order to solve this problem cascading failure in a distributed architecture a series circuit breakers and other protection services.

  Use Hystrix achieve breaker in the Spring Cloud, Spring Cloud Hystrix is ​​based on open source Netflix framework Hystrix implementation. To switch off the circuit breaker from the open state by the current service state of health (state of health service request = number of failures / total number of requests) and the set threshold value (default 20 faults within 10 seconds) determined by the comparison. When the breaker switch is turned off, allowing the request through the circuit breaker, if the current service state of health is higher than a set threshold, the switch will remain off; if the current service status is below a set threshold, then the switch is switched to an open state. When the breaker switch is turned on, request prohibited, if the fallback method provided, the process will enter the fallback, when the circuit breaker switch is turned on, over time, the circuit breaker will automatically enter the half open state and allows a request via If the call request is successful, the breaker will return to the oFF state, or remain open.

  This section be built on a project basis, the Project Address: https://github.com/francis785/springclouddemo.git  , to use Spring Cloud Hystrix, you can just do the following steps:

1. Create  springcloud-demo-user-hystrix  module,  pom.xml  document reads as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-demo-parent</artifactId>
        <groupId>com.fix</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0 . 0 </ modelVersion> 

    <artifactId> springcloud-Demo-the User-hystrix </ artifactId> 
    <the Dependencies> 
        <-! Finchley, Greenwich version of spring cloud, create a client project when the need to rely on the introduction of the -> 
        < dependency> 
            <the groupId> org.springframework.boot </ the groupId> 
            <the artifactId> Starter-Spring-Boot-Web </ the artifactId> 
        </ dependency> 
        <dependency> 
            <the groupId> org.springframework.cloud </ the groupId> 
            <the artifactId> -Cloud-Starter-the Spring Netflix-Eureka-Client </ artifactId> 
        </ dependency> 
        <-! hystrix breaker support -> 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!--hystrixCommandAspect 的初始化依赖于com.google.common.collect.ImmutableMap-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.0-jre</version>
        </dependency>
    </dependencies>
</project>

2. Write  application.yaml  :

Server: 
  Port: 8030 
eureka: 
  instance: 
    the prefer -ip-address: to true      # Ip whether the host 
  Client: 
    Service - url: 
      defaultzone: HTTP: // localhost: 8761 / eureka / # to specify the server address eureka 
the Spring: 
  the Application: 
    name: springcloud -demo-the User-hystrix

3. Write the main class  UserHystrixMain  , and to master classes with the  @EnableCircuitBreaker  NOTE Indicates open circuit breaker functions:

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class UserHystrixMain {
    public static void main(String[] args) {
        SpringApplication.run(UserHystrixMain.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate initRestTemplate() {
        return new RestTemplate();
    }
}

4. Preparation of  the UserController  , and  findOrderByUser ()  method of the above plus  @HystrixCommand  annotation to specify callback method:

@RestController
 public  class the UserController { 
    @Autowired 
    Private RestTemplate RestTemplate; 

    / * * 
     * in accordance with the order information query user id 
     * @param id user id 
     * @return user order information 
     * / 
    @GetMapping ( " / findOrderByUser / {id} " )
    @HystrixCommand (fallbackMethod = " fallBackMethod " )
     public String findOrderByUser (@PathVariable String ID) {
         int the orderId = 123 ;
         return  the this .restTemplate.getForObject ( "HTTP: // SPRINGCLOUD-DEMO-the ORDER / the Order / " . + orderId, String class ); 
    } 

    / * * 
     * Order Inquiry call after the failure to return the interface information 
     * @param the above mentioned id 
     * @return 
     * / 
    public String fallBackMethod (@PathVariable ID String) {
         return  " service is not available, the current query = ID " + ID; 
    } 
}

The functional verification

  In order to start the service registration center, as well as other modules, the Service has the following service registry instance:

  Access in the browser: HTTP: // localhost: 8030 / findOrderByUser / 1  can access:

  Then close  springcloud-demo-order  service, continue to visit http: // localhost: 8030 / findOrderByUser / 1 link, it will prompt the following information:

  Description Spring Cloud Hystrix take effect.

Guess you like

Origin www.cnblogs.com/fengweiweicoder/p/11075054.html