SpringCloud Hystrix protection mechanism

Introduction to Hystrix protection mechanism

Hystrix is an open source delay and fault-tolerant library of Netflix, used to isolate access to remote services and third-party libraries to prevent cascading failures.

Avalanche problem

In microservices, the calling relationship between services is wrong and complicated. A request may be implemented by calling multiple interface services, which will form a particularly complex calling link.
For example: A request needs to call four A/P/H/I services, and these four services need to call other services.
Insert picture description hereIf an exception occurs in the microservice I, the request is blocked, and the user will not get a response, then this thread of tomcat will not be released.
Insert picture description hereSo more and more user requests, more and more threads are blocked. The number of concurrency and threads supported by the server is limited, and requests are blocked all the time, which will cause the server resources to be exhausted, resulting in all other services being unavailable. Form an avalanche effect .

Insert picture description here

Hystrix has two methods to solve the avalanche effect:

  • Thread isolation
  • Service fusing

Thread isolation, service degradation

Schematic diagramHystrix allocates a thread pool for each dependent service call. If the thread pool is full, the call will be rejected immediately. By default, queuing is not used, and the acceleration fails, and the time is determined.
User requests will no longer directly access the service, but will access the service through idle threads in the thread pool . If the thread pool is full or the request times out , it will be downgraded.

什么是降级处理?
	优先保证核心服务,而非核心服务不可用或者弱可用。

Advantage: When
users request a fault, they will not be blocked or see the system crash. At least one execution result can be seen.
Circumstances that cause Hystrix service degradation:

  • Request timed out
  • Thread pool is full

Introduce dependencies

<dependency>
	 <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
@SpringBootApplication
@EnableCircutBreaker//开启熔断
public class TestApplcation{
    
    
	public static void main(String[] args){
    
    
		SpringApplication.run(TestApplication.class,args);
	}
}

or

@SpringCloudApplication
public class TestApplication{
    
    
	public static void main(String[] args){
    
    
		SpringApplication.run(TestApplication.class,args);
	}
}

@SpringCloudApplication combines three annotations often introduced by
microservices , 1.@EnableCircutBreaker turns on fuse
2.@SpringBootApplication
3.@EnableDiscoveryClient turns on load balancing

Write downgrade logic

@RestController("test")
public class TestController{
    
    
	@Autowried
	private TestTemplate testTemplate;
	
	@HystrixCommand(fallbackMethod="queryTestByIdFallBack")
	public String queryTestById(@Param("id")Long id){
    
    
		
	}
	public String queryTestByIdFallBack(Long id){
    
    
		return "请求繁忙";
	}

}

Note: The
fusing and degrading logic method must be consistent with the return value of the normal logic method.
@HystrixCommand(fallbackMethod="queryTestByIdFallBack") is used to declare a downgrade logic method.

@RestController("test")
@DefaultProperties(defaultFallback="queryTestByIdFallBack") //声明一个全局降级逻辑方法
public class TestController{
    
    
	@HystrixCommand
	public String queryTestById(@Param("id")Long id){
    
    
		return "";
	}
	public String queryTestByIdFallBack(Long id){
    
    
		return "请求繁忙";
	}
}
  • @DefaultProperties(defaultFallback="queryTestByIdFallBack"): indicates a unified downgrade method on the class.
  • @HystrixCommand: Use this annotation directly on the method and use the default editing method.
  • defaultFallback: The default downgrade method, without any parameters, to match more methods, but the return value must be consistent

Set timeout

Hystrix default timeout time is 1S, we can hystrix.command.default.execution.isolation.thread.timeoutInMillisecondsset the timeout time of Hystrix.

hystrix:
	command:
		default:
			execution:
				isolation:
					thread:
						timeoutInMilliseconds:6000 #设置超时时间为6000ms
			

Service fusing

Service fusing principle

The 3 states of the fusing state machine:

  • Close: Closed state, all requests are accessed normally.
  • Open: Open state, all requests will be downgraded. Hystix counts requests. Hystix counts the number of requests. When the percentage of failed requests reaches the threshold within a certain period of time, the fuse is triggered and the circuit breaker is fully opened. The default failure rate threshold is 50%, and the number of requests is at least 20 times.
  • Half Open: Half-open state. The open state is not permanent. It will enter the sleep time after being opened (the default is 5S). Then the circuit breaker will automatically enter the half-open state. At this time, some requests will be released. If these requests are healthy, the circuit breaker will be completely closed, otherwise the circuit breaker will remain open, and the sleep timer will be performed again.

Modify the parameters of the fusing strategy:

circuitBreaker:
	requestVolumeThreshold:10
	sleepWindowInMilliseconds:1000
	errorThresholdPercentage:50

1. requestVolumeThreshold: the minimum number of requests to trigger the fuse: default 20
2.errorThresholdPercentage: the minimum proportion of failed requests that trigger the fuse: default 50%
3. sleepWindowInMilliseconds: sleep duration, default 5000ms

Guess you like

Origin blog.csdn.net/weixin_42789301/article/details/105093898