Note: The concept part of the previous article excerpt from the Blog Garden Pure Smile
Fuse Avalanche effect
In a microservices architecture, there are often multiple service layer calls, and the failure of the underlying service can lead to cascading failures that could result in an entire system being unavailable, a phenomenon known as service avalanche effect. The service avalanche effect is a process in which the service consumer is not available and will not be usable due to the unavailability of the service provider.
If: A serves as a service provider, B is a service consumer, and C and D are service consumers of B. A unavailability causes the unavailability of B and will not be available as a snowball to C and D, the avalanche effect is formed.
Fusing Device (Circuitbreaker)
The fuse principle is simple, like a power overload protector. It can achieve rapid failure, and if it detects many similar errors over time, it forces its subsequent calls to fail quickly and no longer accesses the remote server, preventing the application from continually trying to perform operations that might fail, allowing the application to continue without waiting for remediation errors. Or waste CPU time to wait for a long time-out to occur. The fuse can also enable the application to diagnose if the error has been corrected, and if it has been corrected, the application will try to invoke the operation again.
The fuse mode is like a proxy for operations that are prone to errors. This agent can record the number of times a recent call has occurred, and then decide to continue with the allow operation, or return an error immediately.
The logic for switching the fuse switches to each other is as follows:
A fuse is the last line of defense for high-availability protection services.
Hystrix characteristics
1. Circuit breaker mechanism
Circuit Breaker It's good to understand that when the Hystrix command requests a backend service failure quantity exceeding a certain percentage (default 50%), the circuit breaker switches to the open State (open). At this point, all requests fail directly and are not sent to the backend service. The circuit breaker remains in the open state for some time (default 5 seconds), automatically switches to the semi-open state (Half-open). This will determine the return of the next request, if the request succeeds, the circuit breaker is switched back to the closed circuit (CLOSED), or re-switch to open mode (open). Hystrix Circuit breaker is like a fuse in our home circuit, once the backend service is unavailable, the circuit breaker will directly cut off the request chain, avoid sending a large number of invalid requests affecting the system throughput, and the circuit breaker has the ability to self-detect and recover.
2.Fallback
Fallback is equivalent to a downgrade operation. For the query operation, we can implement a fallback method that can use the value returned by the fallback method when the backend service is requested to have an exception. The return value of the fallback method is typically the default value for the setting or from the cache.
3. Resource Isolation
In Hystrix, resource isolation is achieved primarily through the thread pool. Usually when used, we divide multiple thread pools according to the invocation of the remote service. For example, call the command of the product service into the a thread pool and call the command of the account service into the B-thread pool. The main advantage of this is that the operating environment is isolated. This will not affect other services of the system, even if the code that invokes the service has a bug, or if it is otherwise exhausted by its own online pool. But the cost is that maintaining multiple thread pools can bring additional performance overhead to the system. You can use Hystrix's signal mode (semaphores) to isolate resources if it is not a problem for client code that has stringent performance requirements and is confident that you are invoking the service.
ribbon--Hystrix
Because the fuse only works at this end of the service invocation, we only need to change the Smicroservice-consumer-movie-ribbon-with-hystrix project-related code as per the example code in the previous article.
Pom.xml file
<?xml version= "1.0"?>
<project xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" Xmlns= "http://maven.apache.org/POM/4.0.0"
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" >
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-consumer-movie-ribbon-with-hystrix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>
</project>
Configuration file Application.yml
Spring
Application:
Name:microservice-consumer-movie-ribbon-with-hystrix
Server
port:8010
Eureka
Client
Healthcheck:
Enabled:true
Serviceurl:
Defaultzone:http://user:[email Protected]:8761/eureka
Instance
Prefer-ip-address:true
hystrix.command.default.execution.isolation.thread.timeoutinmilliseconds:5000
=================================================================================
Start class
Package Com.itmuch.cloud;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
Import org.springframework.cloud.client.loadbalancer.LoadBalanced;
Import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.web.client.RestTemplate;
/**
* @author z
*
*/
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker//Circuit Breaker Annotations
public class Consumermovieribbonapplication {
@Bean
@LoadBalanced
Public Resttemplate resttemplate () {
return new Resttemplate ();
}
public static void Main (string[] args) {
Springapplication.run (Consumermovieribbonapplication.class, args);
}
}
==============================================================================
Controller: Annotate @HystrixCommand on called method (Fallbackmethod = "Findbyidfallback")
Package Com.itmuch.cloud.controller;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.GetMapping;
Import org.springframework.web.bind.annotation.PathVariable;
Import Org.springframework.web.bind.annotation.RestController;
Import Org.springframework.web.client.RestTemplate;
Import Com.itmuch.cloud.entity.User;
Import Com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class Moviecontroller {
@Autowired
Private Resttemplate resttemplate;
@GetMapping ("/movie/{id}")
@HystrixCommand (Fallbackmethod = "Findbyidfallback")
Public User FindByID (@PathVariable Long ID) {
Return This.restTemplate.getForObject ("http://microservice-provider-user/simple/" + ID, user.class);
}
/**
* Method of this method sends back value type to be consistent with FindByID return value type, parameter must be consistent
* @param ID
* @return
*/
Public User Findbyidfallback (Long ID) {
User user = new user ();
User.setid (0L);
return user;
}
}
Just like this, a circuit breaker is complete. At this point we start the registry with the Smicroservice-consumer-movie-ribbon-with-hystrix and user micro Services
At this point we stop the user micro-service, access LOCALHOST:8081/MOVIE/1 at this time the circuit breaker will take effect and enter the corresponding fallback method.
Summarize:
Add Hystrix (fuse) on ribbon
1: Need to join Hystrix dependency Package
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2: Start class plus hystrix annotations
@EnableCircuitBreaker//Circuit breaker annotations
3: Add annotations to the method being called
@GetMapping ("/movie/{id}")
@HystrixCommand (Fallbackmethod = "Findbyidfallback")
Public User FindByID (@PathVariable Long ID) {
Return This.restTemplate.getForObject ("http://microservice-provider-user/simple/" + ID, user.class);
}
and the parameter in the Findbyidfallback method and the return value type are consistent with the calling method
4: Questions about starting a direct entry Findbyidfallback method
Because the Hystrix default call time-out is 1 seconds, we need to set the timeout in the configuration file at this time
hystrix.command.default.execution.isolation.thread.timeoutinmilliseconds:5000
Springcloud Four: Fuse Ribbon--hystrix