springcloud-Circuit Breaker (Hystrix)

Source: Internet
Author: User

In a microservices architecture, the service is split into services based on the business, and services and services can be called each other (RPC), which can be invoked with the rest Template + ribbon and feign in spring cloud. To ensure their high availability, a single service is typically deployed in a cluster. Due to network reasons or their own reasons, the service does not guarantee that 100% is available, if a single service problem, call the service will be a thread blocking, if there is a large number of requests, the servlet container thread resources will be exhausted, resulting in service paralysis. The dependence between service and service, the failure can spread, can cause catastrophic serious consequence to the whole micro-service system, this is the avalanche effect of service fault.
In order to solve this problem, a circuit breaker model is proposed.

Circuit Breaker Introduction

Netflix opens up the Hystrix component, implements the circuit breaker mode, and springcloud the component. In a microservices architecture, it is very common for a request to invoke multiple services, such as

Lower-level services can cause cascading failures if they fail. When a call to an attribute's service is not available to reach a threshold value (Hystric is 5 seconds 20 this) the circuit breaker will be opened.

When the circuit breaker is open, it can be used to avoid cascading failures, and the fallback method may return a fixed value directly
Using a circuit breaker in the Ribbon
Adding Spring-cloud-starter-hystrix's starting dependency in the Pom file

<dependency><groupId>org.springframework.cloud</groupId><artifactId> Spring-cloud-starter-hystrix</artifactid></dependency>

Add annotations on the startup class of the program @enablehystrix enable the circuit breaker, as follows

@EnableHystrix @enablediscoveryclient@springbootapplicationpublic class Serviceribbonapplication {public static void Main (string[] args) {Springapplication.run (serviceribbonapplication.class, args);} @Bean @loadbalancedresttemplate resttemplate () {return new resttemplate ();}}

Transform the HelloService class to add @hystrixcommand annotations on the Hiservice method. This note creates a fuse function for the method and specifies the Fallbackmethod fuse method, which directly returns a string with the code as follows

@Servicepublic class HelloService {@AutowiredRestTemplate resttemplate; @HystrixCommand (Fallbackmethod = "Hierror") public string Hiservice (string name) {return Resttemplate.getforobject ("http://eureka-client/hello?name=" + Name, String.class);} public string Hierror (string name) {return "Hi," + name + ", sorry Error";}}

Test

Break off eureka-client and test again

Indicates that when the Eureka-client project is unavailable, Service-ribbon calls the API interface of Eureka-client, performs a quick failure, returns a set of strings directly instead of waiting for the corresponding timeout, which controls the container's thread blocking well.

Using circuit breakers in feign

Feign is a self-contained circuit breaker, and in the D version of Spring Cloud, he does not open it by default. Needs to be configured in the configuration file to open it, under the code in the configuration file
Feign:
Hystrix:
Enabled:true
Just add the fallback specified class to the annotations of the Feignclient Schedualservicehi interface:

@FeignClient (value = "Eureka-client", fallback = schedualservicehihystric.class) public interface Schedualservicehi {@ GetMapping ("/hello") 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

@Componentpublic class Schedualservicehihystric implements Schedualservicehi {@Overridepublic String Sayhifromclientone (String name) {return "sorry" + Name;}}

Test under normal state

Stop eureka-client and retry as follows

Proving that the circuit breaker is working.

Hystrix Dashboard (Circuit breaker: hystic instrument panel)

Based on Service-ribbon transformation, the transformation of feign is the same
The start-up dependence of spring-cloud-starter-hystrix-dashboard introduced in Pom

<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 @eablehystrixdashboard annotations to the startup class to turn on Hystrixdashbord

@EnableHystrixDashboard @enablehystrix@enablediscoveryclient@springbootapplicationpublic Class serviceribbonapplication {public static void main (string[] args) {Springapplication.run ( Serviceribbonapplication.class, args);} @Bean @loadbalancedresttemplate resttemplate () {return new resttemplate ();}}

Start as follows
Http://localhost:8764/hystrix

Click Monitor Stream, at which point the request Http://localhost:8764/hi?name=test

springcloud-Circuit Breaker (Hystrix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.