Eureka (Version F) Course Four Circuit Breakers (Hystrix)

Posted by jek on Thu, 22 Aug 2019 11:26:09 +0200

Links to the original text: https://www.fangzhipeng.com/springcloud/2018/08/04/sc-f4-hystrix.html

In the micro-service architecture, services are separated into services according to their business, which can be invoked between services (RPC), and in Spring Cloud, which can be invoked by RestTemplate+Ribbon and Feign. To ensure high availability, individual services are usually deployed in clusters. Because of network reasons or its own reasons, services can not guarantee 100% availability. If a single service has problems, there will be thread blockage when calling this service. At this time, if there are a large number of requests flooding in, the thread resources of the Servlet container will be consumed, resulting in service paralysis. Dependence between services and services, failure will spread, will cause catastrophic consequences for the entire micro-service system, which is the "avalanche" effect of service failure.

In order to solve this problem, the circuit breaker model is proposed.

I. Brief Introduction of 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 opens source Hystrix components and implements circuit breaker mode, which Spring Cloud integrates. In a microservice architecture, it is very common for a request to invoke multiple services, as shown below:

If the lower level service fails, it will lead to cascading failure. When the call to a particular service is unavailable to a threshold (Hystric is 5 seconds 20 times), the circuit breaker will be opened.

When open, cascading failures can be avoided, and fallback method can directly return a fixed value.

II. Preparations

Based on the project of the previous article, this article starts the project of the previous article, starts the eureka-server project, and starts the service-hi project, whose port is 8762.

3. Using Circuit Breakers in ribbon

To modify the code of serice-ribbon project, the start dependency of spring-cloud-starter-netflix-hystrix is first added to the pox.xml file:

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

Open Hystrix with the @Enable Hystrix annotation in the startup class ServiceRibbon Application of the program:

@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();
    }

}

Modify the HelloService class and add the @HystrixCommand annotation to the hiService method. The annotation creates the fuse function for this method and specifies the fallbackMethod fuse method. The fuse method directly returns a string with the following codes: hi, "+name+", sorry,error!".

@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 shows:

hi forezp,i am from port:8762

Close the service-hi project at this time. 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 not available, service-ribbon calls the API interface of service-hi, it will execute a fast failure and return a set of strings directly instead of waiting for response timeout, which is a good control of the container's thread blocking.

IV. Use of Circuit Breakers in Feign

Feign is a self-contained circuit breaker, which does not open by default after version D Spring Cloud. You need to configure it in the configuration file to open it and add the following code in the configuration file:

feign.hystrix.enabled=true

Based on the service-feign project, only the specified classes of fallback are added to the annotations of the Schedule Service Hi interface of the FeignClient:

@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

SchedualService HiHystric needs to implement the SchedualService Hi 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, and the browser opens http://localhost:8765/hi?name=forezp. Note that the service-hi project is not started at this time, and the web page shows:

sorry forezp

Open the service-hi project and visit again. The browser shows:

hi forezp,i am from port:8762

This proves that the circuit breaker works.

Topics: Spring network xml