Since each unit runs in a different process and relies on a remote invocation, it is possible that a call failure or delay may occur due to network or dependent service problems that directly cause the caller's external service to be delayed, and if the caller's request is increasing at this time, In the end, a backlog of tasks is formed due to waiting for the failure of the relying party response, which eventually leads to the paralysis of its service.
In the micro-service architecture, there are a large number of service units, if a unit fails, it is easy to rely on the dependency of the spread of failure, resulting in the overall system paralysis, the architecture is more unstable than the traditional architecture, in order to solve such problems, generated a series of service protection mechanisms such as circuit breakers.
In the distributed architecture, the function of the circuit breaker mode is similar, when a service unit fails, through the fault monitoring of the circuit breaker, return an error response to the caller, rather than a long wait, so that the thread will not be caused by the call failure service for a long time occupied not released. Avoid the spread of failures in distributed systems.
To address these issues, Spring Cloud Hystrix implements a range of service protection functions such as circuit breakers, thread isolation, and the following shows the protection of services through circuit breakers, and the need to start the service registry that we created earlier, Hello-service Service and Consumer-helloservice Engineering, when the Hello-service service is closed before the circuit breaker is added, the following output occurs when the Consumer-helloservice accesses Hello-service:
Type Exception Report
Message Java.lang.IllegalStateException:No instances available for ORG. Drsoft. WEBSERVICE. Hellonameservice
Description the server encountered an unexpected condition, prevented it from fulfilling the request.
Exception
Javax.servlet.ServletException:java.lang.IllegalStateException:No instances available for ORG. Drsoft. WEBSERVICE. Hellonameservice
Org.glassfish.jersey.servlet.WebComponent.serviceImpl (webcomponent.java:489)
Org.glassfish.jersey.servlet.WebComponent.service (webcomponent.java:427)
Org.glassfish.jersey.servlet.ServletContainer.service (servletcontainer.java:388)
Org.glassfish.jersey.servlet.ServletContainer.service (servletcontainer.java:341)
。。。。。。
Let's add a circuit breaker to protect the service:
-
<DEPENDENCY>
<groupid>org.springframework.cloud </groupid>
<artifactid>spring-cloud-starter-hystrix </artifactid>
</dependency>
- use @EnableCircuitBreaker Annotations to turn on circuit breaker functionality in the main class consumerhelloserviceapplication of the Consumer-helloservice project:
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class Consumerhelloserviceapplication {
??
???????? Public staticvoidmain(string[] args) {
???????????????? Springapplication.run (Consumerhelloserviceapplication.class, args);
????????}
??
???????? @Bean
???????? @LoadBalanced
???????? public resttemplate resttemplate() {
?????????????? return newresttemplate();
????????}
}
- transform the service consumption mode, add @Service annotations in the service invocation class, add @HystrixCommand annotations in the method of the service invocation, and increase the parameter Fallbackmethod set the method name that the service invocation failed (requires the same parameters as the calling service method)
@HystrixCommand (Fallbackmethod = "Hellofallback")
???????????? public String Hello() {
???????????????? return resttemplate.getforentity ("Http://hello-service/hello/get", String.class). GetBody ();
????????????}
??
Public String hellofallback() {
???????????? return"error";
}
- The Hello-service service is turned off, and the "error" return occurs when the consumer-helloservice is accessed Hello-service
Note: Spring Cloud hystrix Service Fault Tolerant protection