Write at the beginning
In the Springcloud project, calls are made between services (RPC remote Procedure call-Remote Procedure calls), and when a service that is at the bottom of the calling link becomes unavailable, the request generates a heap that blocks the server thread and even causes the server to crash. A circuit breaker is a way to solve a service unavailability problem.
Body Start
This article is based on the third piece of code, the introduction of circuit breaker is based on the Ribbon type of circuit breaker
Users of new projects can check the following components when building a project
Users who have already built the project can add
<!--circuit Breaker plug-in <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
In addition, we need to do a bit of configuration
Application increase
@EnableHystrix
method to add failure annotations
@RequestMapping (value = "/invokeservice", method = requestmethod.get) = "Hierror") public String Invokeservice (@RequestParam (value = "Name") string name) { System.out.println ("Enter parameter name") : "+ name") ; = Resttemplate.getforobject ("http://CLOUD-SERVICE/getServiceNameAndPort?name=" + Name, String. Class); SYSTEM.OUT.PRINTLN (JSON); return json; }
Defining methods when calling a method fails
Public string Hierror (string name) { return "--------------Hi," + name + "microservices down--------------"; c5/>}
---------------------------------------------------configuration is complete Let's do two Tests-------------------------------------------------------
1 Start service center, S end, C end
Interface Request HTTP://LOCALHOST:8003/INVOKESERVICE?NAME=TIANMH return result Hello tianmh Serviceport 8001
2 Stop the S-end
Interface Request HTTP://LOCALHOST:8003/INVOKESERVICE?NAME=TIANMH return results--------------hi,tianmh microservices down--------------
Written in the last
This article introduces the circuit breaker, which guarantees the orderly operation of the caller in the event of an exception. In addition, Springcloud provides a graphical management interface for this function, which is presented in the next article.
Fourth Circuit breaker (Hystrix)--idea Springcloud All-in-one pro-test available