In the microservice architecture, services are divided into one service after another according to the business. Services can call each other (RPC). In Spring Cloud, services can be called by RestTemplate+Ribbon and Feign. To ensure high availability, individual services are typically deployed in clusters. Due to network reasons or its own reasons, the service cannot guarantee 100% availability. If a single service has problems, the invocation of this service will be blocked. At this time, if a large number of requests flood in, the thread resources of the Servlet container will be consumed and the service will break down. The “avalanche” effect of service failures is the spread of service-to-service dependencies, which can have catastrophic consequences for the entire microservice system.

In order to solve this problem, the industry put forward the breaker model.

I. 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-source the Hystrix component to implement the circuit breaker model, and SpringCloud integrates this component. In a microservice architecture, it is very common for a single request to invoke multiple services, as shown below:

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

The fallback method can directly return a fixed value to avoid interlocking failures when the circuit is open.

Second, preparation

This article is based on the project of the previous article. Firstly, it starts the project of the previous article, namely, the Eureka-Server project. Start the service-HI project with port 8762.

3. Use circuit breakers in the ribbon

Add the spring-cloud-starter-hystrix dependency to the pox. XML file:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>Copy the code

Start Hystrix with @enablehystrix annotation in the ServiceRibbonApplication startup class:

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }
 
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        returnnew RestTemplate(); }}Copy the code

Modify the HelloService class to annotate the hiService method with @hystrixCommand. This annotation creates a fuse function for the method and specifies the fallbackMethod fuse method, which returns a string of “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!"; }}Copy the code

Launch: Service-Ribbon Project, when we visit http://localhost:8764/hi? Name =forezp, browser displays:

hi forezp,i am from port:8762Copy the code

Close the service-hi project at this time, when we visit http://localhost:8764/hi again? Name =forezp, the browser will display:

hi ,forezp,orry,error!Copy the code

This means that when the service-hi project is unavailable, the service-ribbon calls the service-Hi API interface, and the execution fails quickly. Instead of waiting for the response to time out, the service-hi API interface returns a string. This controls the thread blocking of the container.

The architecture code is as follows:



Spring Cloud large enterprise distributed micro service Cloud to build B2B2C e-commerce platform source code please add penguin beg: 10387746626