Amateur Grass Springcloud Tutorial | Fourth: Circuit breaker (Hystrix) (Finchley version)

Source: Internet
Author: User

In a microservices architecture, the service is split into services according to the business, and the service and service can call each other (RPC), which can be invoked with Resttemplate+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 will spread, will have disastrous serious consequence to the whole micro-service system, this is the "avalanche" effect of service failure.

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

First, Circuit breaker introduction

Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it was common to the multiple layers of service calls.

. --Excerpt from official website

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 a particular service is not available to reach a threshold (Hystric is 5 seconds 20 times) The circuit breaker will be opened.

The fallback method can return a fixed value directly after the open circuit is opened to avoid cascading failures.

Ii. preparatory work

This article is based on the project of the previous article, first to start the project of the previous article, start the Eureka-server project, start the Service-hi project, its port is 8762.

Iii. use of circuit breakers in the Ribbon

Transform the code of the Serice-ribbon project, first in the Pox.xml file to add the Spring-cloud-starter-netflix-hystrix start-up dependency:

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

In the program's startup class Serviceribbonapplication add @enablehystrix annotations to open Hystrix:

1 @SpringBootApplication2 @EnableEurekaClient3 @EnableDiscoveryClient4 @EnableHystrix5  Public classserviceribbonapplication {6  7      Public Static voidMain (string[] args) {8Springapplication.run (serviceribbonapplication.class, args);9     }Ten   One @Bean A @LoadBalanced - resttemplate resttemplate () { -         return Newresttemplate (); the     } -   -}

Transform the HelloService class and add @hystrixcommand annotations to the Hiservice method. The note creates a fuse function for the method and specifies the Fallbackmethod fuse method, which directly returns a string with the string "Hi," "+name+", sorry,error! ", and 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

At this point the Service-hi project is closed, and when we revisit Http://localhost:8764/hi?name=forezp, the browser will show:

Hi, forezp,orry,error!

This means that when the Service-hi project is unavailable, Service-ribbon calls the API interface of Service-hi, performs a quick failure, returns a set of strings directly, rather than waiting for a response timeout, which controls the container's thread blocking well.

Iv. use of circuit breakers in feign

The feign comes with a circuit breaker, which is not opened by default after the D version of Spring Cloud. It needs to be configured in the configuration file to open it, under the code in the configuration file:

Feign.hystrix.enabled=true

Based on the Service-feign project, simply add the fallback specified class to the annotations of the Feignclient Schedualservicehi interface:

1 @FeignClient (value = "Service-hi", fallback = schedualservicehihystric.  Class)2publicinterface  Schedualservicehi {3     @ Requestmapping (value = "/hi", method = requestmethod.get)4     String Sayhifromclientone (@ Requestparam (value = "Name") String name); 5 }

The schedualservicehihystric needs to implement the Schedualservicehi interface and inject it into the IOC container with the following code:

1 @Component 2  Public class Implements Schedualservicehi {3    @Override4public       String Sayhifromclientone (String name) {5         return "Sorry" +name; 6     }7 }

Start four Servcie-feign project, browser Open HTTP://LOCALHOST:8765/HI?NAME=FOREZP, note at this time Service-hi project does not start, Web page display:

Sorry Forezp

Open Service-hi Project, visit again, browser display:

Hi forezp,i am from port:8762

This proves that the circuit breaker is playing a role.

This article source code download:
Https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter4

Vi. references

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

Thank you for your attention! Add QQ1 Group: 135430763,QQ2 Group: 454796847,QQ3 Group: 187424846. QQ Group into the group password: xttblog, want to add a group of friends, you can search: Xmtxtt, note: "Xttblog", add Assistant pull you into the group. Note errors do not agree with a friend application. Thanks again for your attention! Follow up with wonderful content will be sent to you the first time! Please send the original article to [email protected] email. Business cooperation can add assistants to communicate!

Amateur Grass Springcloud Tutorial | Fourth: Circuit breaker (Hystrix) (Finchley version)

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.