(Turn) The simplest SpringCloud tutorial in history | Part 4: Circuit Breaker (Hystrix) (Finchley Version)

In the microservice architecture, the services are divided into individual services according to the business. Services and services can call each other (RPC). In Spring Cloud, you can use RestTemplate+Ribbon and Feign to call. In order to ensure its high availability, a single service is usually deployed in clusters. Due to network reasons or its own reasons, the service is not guaranteed to be 100% available. If a single service has a problem, the thread will be blocked when calling this service. If a large number of requests flow in at this time, the thread resources of the Servlet container will be consumed. , Resulting in service paralysis. The dependency between service and service, failure will propagate, and will cause catastrophic and serious consequences to the entire microservice system. This is the "avalanche" effect of service failure.

In order to solve this problem, the industry has proposed a circuit breaker model.

1. Brief introduction of 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.

. ----From the official website

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

å¨è¿éæå ¥ å¾çæè¿ °

If the lower-level services fail, it will cause cascading failures. 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. Preparation

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.

Three, use the circuit breaker in the ribbon

To modify the code of the serice-ribbon project, first add the start-up dependency of spring-cloud-starter-netflix-hystrix in the pox.xml file:

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

Add the @EnableHystrix annotation to the startup class ServiceRibbonApplication of the program to enable Hystrix:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {

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

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

Transform the HelloService class and add the @HystrixCommand annotation to the hiService method. This annotation creates the function of a fuse for this method, and specifies the fallbackMethod fuse method, the fuse 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

Now 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 service-ribbon calls the service-hi API interface, it will fail quickly and directly return a set of strings instead of waiting for a response timeout, which controls the container well. The thread is blocked.

Fourth, the use of circuit breakers in Feign

Feign comes with its own circuit breaker. After 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, 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 SchedualServiceHi 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);
}

SchedualServiceHiHystric needs to implement the SchedualServiceHi 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 project, open the browser http://localhost:8765/hi?name=forezp, note that the service-hi project is not started at this time, the web page displays:

sorry forezp

Open the service-hi project, visit again, the browser displays:

hi forezp,i am from port:8762

This proves that the circuit breaker has worked.


Download the source code of this article:

https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter4

Read more

Summary of the simplest SpringCloud tutorials in history

SpringBoot tutorial summary

Summary of Java Interview Questions Series

Reference

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

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

 

 

Guess you like

Origin blog.csdn.net/u014225733/article/details/100538453