SringCloud | Part 4: Circuit Breaker (Hystrix)

In the microservice architecture, it is divided into services one by one according to the business, and the services can be called (RPC) with each other. In Spring Cloud, RestTemplate+Ribbon and Feign can be used to call. In order to ensure its high availability, a single service is usually deployed in a cluster. Due to network reasons or its own reasons, services cannot be guaranteed to be 100% available. If there is a problem with a single service, thread blocking will occur when calling this service. At this time, if a large number of requests flood in, the thread resources of the servlet container will be consumed. , resulting in service paralysis. Dependency between services, failures will propagate and cause catastrophic serious consequences to the entire microservice system, which is the "avalanche" effect of service failures.

To solve this problem, the industry proposes the circuit breaker model.

1. Introduction to circuit breaker
Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.

Netflix has open sourced the Hystrix component and implemented the circuit breaker pattern. components are integrated. In a microservice architecture, it is very common for a request to call multiple services.

Lower-level services can cause cascading failures if they fail. When the unavailability of calls to a particular service reaches a threshold (Hystric is 20 times in 5 seconds) the circuit breaker will be opened.



After the circuit breaker is opened, cascading failures can be avoided, and the fallback method can directly return a fixed value.

2. Preparations
This article is based on the project of the previous article, first start the project of the previous article, start the eureka-server project; start the service-hi project, its port is 8762.

3. Use circuit breakers in the ribbon to
transform the code of the serice-ribbon project. First, add the starting dependencies of spring-cloud-starter-hystrix to the pox.xml file:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>


Add the @EnableHystrix annotation to the startup class ServiceRibbonApplication of the program to enable Hystrix:
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}


Modify the HelloService class and add the @HystrixCommand annotation to the hiService method. This annotation creates the function of a circuit breaker for this method, and specifies the fallbackMethod circuit breaker method. The circuit breaker method directly returns a string, the string is "hi,"+name+",sorry,error!", the code is as follows:
@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }
}


Start: service-ribbon project , when we visit http://localhost:8764/hi?name=forezp, the browser displays:

hi forezp, i am from port:8762

At this time, close the service-hi project , when we visit http ://localhost:8764/hi?name=forezp, the browser will display:

hi ,forezp,orry,error!

This means that when the service-hi project is unavailable, when the service-ribbon calls the service-hi API interface , which will fail fast and return a set of strings directly instead of waiting for the response to timeout, which controls the thread blocking of the container well.

Fourth, the use of circuit breakers in
Feign Feign has its own circuit breaker . In the D version of Spring Cloud, it is not turned on by default. You need to configure and open it in the configuration file, and add the following code to the configuration file:

feign.hystrix.enabled=true

To transform based on the service-feign project, you only need to add the specified class of fallback to the annotation of the ScheduleServiceHi interface of FeignClient:
@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}


ScheduleServiceHiHystric needs to implement the ScheduleServiceHi interface and inject it into the Ioc container. The code is as follows:
@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }
}


Start the four servcie-feign projects, open http://localhost:8765/hi?name=forezp in the browser , note that the service-hi project is not started at this time, and the webpage displays:

sorry forezp

open the service-hi project, visit again, the browser Shows:

hi forezp,i am from port:8762

This proves that the circuit breaker worked.

5. Hystrix Dashboard (circuit breaker: Hystrix dashboard) is

based on service-ribbon transformation, and Feign's transformation is the same as this.

It is preferred to introduce the starter dependencies of spring-cloud-starter-hystrix-dashboard in pom.xml:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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


Add the @EnableHystrixDashboard annotation to the main program startup class to enable hystrixDashboard:
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}


Open the browser: visit http://localhost:8764/hystrix,



click monitor stream, enter the next interface, visit: http://localhost:8764/hi?name=forezp

At this time, the monitoring interface will appear:



from: http: //blog.csdn.net/forezp/article/details/69934399




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326265695&siteId=291194637
Recommended