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-hystrix start-up dependency:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId> Spring-cloud-starter-hystrix</artifactid></dependency>
In the program's startup class Serviceribbonapplication add @enablehystrix annotations to open Hystrix:
@SpringBootApplication @enablediscoveryclient@enablehystrix Public class serviceribbonapplication { publicstaticvoid main (string[] args) { Springapplication.run (serviceribbonapplication. class , args); } @Bean @LoadBalanced resttemplate resttemplate () { returnnew Resttemplate (); } }
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; = "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.
The schema code is as follows:
Source source technical support for complete projects 2147775633
Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (quad) circuit breaker (Hystrix)