The simplest SpringCloud tutorial in history | 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 breakers

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.

. ——From the official website

Netflix open sourced the Hystrix component to implement the circuit breaker pattern, and SpringCloud integrates this component. In the microservice architecture, it is very common for a request to call multiple services, as shown in the following figure:

HystrixGraph.png

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.

HystrixFallback.png

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 ribbon

To transform the code of the serice-ribbon project, first add the starter 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 the 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:

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 point, close the service-hi project. When we visit http://localhost:8764/hi?name=forezp again , 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, it will fail fast and return a set of strings directly instead of waiting for the response to time out, which controls the container well. thread is blocked.

Fourth, the use of circuit breakers in Feign

Feign has its own circuit breaker, and in the D version of Spring Cloud, it is not turned on by default. You need to configure it in the configuration file to open it, 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, and the browser displays:

>

hi forezp,i am from port:8762

This proves that the circuit breaker works.

5. Hystrix Dashboard (Circuit Breaker: Hystrix Dashboard)

Based on the service-ribbon transformation, 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 , the interface is as follows:

Paste_Image.png

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

The monitoring interface will appear:

Paste_Image.png

Download the source code of this article: 
https://github.com/forezp/SpringCloudLearning/tree/master/chapter4

6. References

circuit_breaker_hystrix

feign-hystrix

hystrix_dashboard

Excellent article recommendation:

Guess you like

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