"Microservices in Action" Chapter 8 Hystrix of Spring Cloud

Series Article Directory

Chapter 8 Hystrix of Spring Cloud
Chapter 7 GateWay of Spring Cloud
Chapter 5 Ribbon of Spring Cloud Netflix
Chapter 4 Eureka of Spring Cloud Netflix
Chapter 3 Introduction to Spring Cloud
insert image description here


foreword

多个微服务之间调用的时候,假如微服务A调用微服务B和微服务C,微服务B和微服务C又调用其他的微服务,这就是所谓的"扇出"。
如果扇出的链路上某个微服务的调用响应的时间过长或者不可用,对微服A的调用就会占用越来越多的系统资源,进而引起系统崩溃,即"雪崩效应"。
这时就需要一个组件(hytrix),来保证微服务出现故障时,不会导致整个系统出现雪崩效应,以提高分布式系统弹性。

1. hytrix concept

  • Hystrix is ​​an open source library for dealing with delay and fault tolerance of distributed systems. It can ensure that when a service fails, it will not cause an avalanche effect on the entire system, so as to improve the resilience of distributed systems;
  • As a "circuit breaker", when a service fails, it can be monitored by the short circuit and a response result that can be processed is returned to ensure that the service calling thread will not be occupied for a long time and avoid the spread of faults

2. The role of Hystrix

2.1. Service downgrade

insert image description here

2.2. Service circuit breaker

insert image description here

3. Case

3.1. Service Provider Downgrade

3.1.1. Modify pom.xml configuration dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

3.1.2. Add downgrade annotation code to the microservice method

//超过3秒钟降级到userInfoListFallBack
@HystrixCommand(fallbackMethod = "userInfoListFallBack",commandProperties = {
    
    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds"
        ,value = "3000")})
  • The parameter fallbackMethod attribute is used to specify the fallback method.
  • The parameter execution.isolation.thread.timeoutInMilliseconds is used to set the peak value of its own call timeout time, and it can run normally within the peak value, otherwise the downgrade method will be executed

3.1.3. Add annotations to the startup class

@EnableHystrix

3.2. Consumer downgrade

3.2.1. Modify pom.xml configuration dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

3.2.2. Modify application.yml configuration

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000

3.2.3. Add code on the consumer method

@HystrixCommand(fallbackMethod = "fallbackMethod")

3.2.4. Add annotations to the consumer startup class

@EnableHystrix

3.3. Global downgrade

@RestController
@RequestMapping("/user")
@DefaultProperties(defaultFallback = "globalFallback")
@Slf4j
public class UserConsumer {
    
    
	@GetMapping("/userInfoList")
	@HystrixCommand
	public List<UserInfo> userInfoList(){
    
    
	}
}

3.4. Decoupling and downgrading

Regardless of the downgrade method specified by the business method or the global downgrade method, they must be in the same class as the business method to take effect, and the coupling between business logic and downgrade logic is extremely high.

3.4.1. Modify application.yml

feign:
#  hystrix:
#    enabled: true
  circuitbreaker:
    enabled: true

3.4.2. Modify the method

@FeignClient(value = "USER-SERVICE",fallback = UserServiceFallBack.class)
public class UserServiceFallBack implements UserService{
    
    

3.5. Service circuit breaker

The fuse mechanism is a microservice link protection mechanism that emerged in response to the avalanche effect.
There are three fuse states involved in the fuse mechanism:

  • Closed state (Closed): When the service access is normal, the circuit breaker is in the closed state, and the service caller can call the service normally.
  • Fuse open state (Open): By default, when the interface call error rate reaches a threshold (for example, 50%) within a fixed time, the fuse will enter the fuse open state. After entering the fuse state, subsequent calls to the service will be cut off, and the fuse will execute the local fallback (FallBack) method.
  • Half-Open: After the fuse has been turned on for a period of time, the fuse will enter the half-open state. In the semi-broken state, the circuit breaker will try to restore the service caller's call to the service, allow some requests to call the service, and monitor its call success rate. If the success rate meets expectations, it means that the service has returned to normal, and the fuse enters the closed state; if the success rate is still low, it re-enters the fuse open state.
    insert image description here

3.5.1, Hystrix implements the steps of service fusing

  • When the call error rate of the service reaches or exceeds the rate specified by Hystix (the default is 50%), the fuse enters the fuse-open state.
  • After the fuse enters the fuse-on state, Hystrix will start a sleep time window. During this time window, the degradation logic of the service will temporarily act as the main business logic, while the original main business logic is unavailable.
  • When there is a request to call the service again, the degradation logic will be directly invoked to quickly return a failure response to avoid system avalanche.
  • When the sleep time window expires, Hystrix will enter a semi-fuse state, allowing some requests to call the original main business logic of the service, and monitor its call success rate.
  • If the call success rate meets expectations, it means that the service has returned to normal, Hystrix enters the circuit breaker off state, and the original main business logic of the service is restored; otherwise, Hystrix re-enters the circuit breaker on state, restarts the sleep time window, and continues to repeat steps 2 to 5.
parameter describe
metrics.rollingStats.timeInMilliseconds Statistical time window.
circuitBreaker.sleepWindowInMilliseconds Sleep time window, after the fuse is on for a period of time, the fuse will automatically enter the half-blown state, this period is called the sleep window period.
circuitBreaker.requestVolumeThreshold Total request threshold.

Within the statistical time window, the total number of requests must reach a certain order of magnitude before Hystrix may open the fuse and enter the fuse-on transition state, and this request order of magnitude is the threshold of the total number of requests. The default threshold for the total number of Hystrix requests is 20, which means that within the statistical time window, if the number of service calls is less than 20, even if all requests are wrongly called, the fuse will not be opened.
circuitBreaker.errorThresholdPercentage Error percentage threshold.

When the total number of requests exceeds the threshold of the total number of requests within the statistical time window, and the request call error rate exceeds a certain ratio, the fuse will be opened and enter the fuse-on transition state, and this ratio is the error percentage threshold. If the error percentage threshold is set to 50, it means that the error percentage is 50%. If the service has 30 calls, 15 of which have errors, that is, the error percentage exceeds 50%, and the fuse will be opened at this time.

3.5.2. Examples

@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
    
    
        @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
        @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "10000")//时间窗口10秒
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "30000"), // 时间窗口期
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),// 失败率达到多少后跳闸
})
public interface UserService {
    
    
    public String paymentCircuitBreaker(@PathVariable("id") Integer id);
}
@Service
public class UserServiceImpl implements UserService {
    
    
    @Override
    @HystrixCommand(fallbackMethod = "paymentCircuitBreakerFallback",commandProperties = {
    
    
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
            @HystrixProperty(name = HystrixPropertiesManager.METRICS_ROLLING_STATS_TIME_IN_MILLISECONDS,value = "10000"),
            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD,value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "30000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),// 失败率达到多少后跳闸
    })
    public String paymentCircuitBreaker(Integer id) {
    
    
        if(id < 0)        {
    
    
            throw new RuntimeException("******id 不能负数");
        }
        String serialNumber = UUID.randomUUID().toString();
        return Thread.currentThread().getName()+"\t"+"调用成功,流水号: " + serialNumber;
    }
    
    public String paymentCircuitBreakerFallback(@PathVariable("id") Integer id) {
    
    
        return "id 不能负数,请稍后再试,/(ㄒoㄒ)/~~   id: " +id;
    }
}

Guess you like

Origin blog.csdn.net/s445320/article/details/130690100