springcloud (Greenwich): Hystrix fault tolerance configuration

Overview

In a distributed framework, remote calls between services are often required. In this way, call failures or delays may occur due to network reasons or depending on the service itself. The occurrence of these conditions will directly cause delays in external interface services. When a party calls a large number of delayed interfaces or services, it will cause a backlog of tasks and thread resources that cannot be released. Eventually, it will spread through the call chain and eventually bring down the entire system.

In springcloud, Spring Cloud Hystrix fault-tolerant protection is provided for the above problems. It is also based on Netflix's open source framework Hystrix. It has powerful functions such as service degradation, service fuse, thread isolation, request caching, request merging, and service monitoring. .

  • Resource isolation: Including thread pool isolation and semaphore isolation, limiting the use of resources for invoking distributed services, and a problem with a called service will not affect other service calls.
  • Downgrade mechanism: Downgrade over time and insufficient resources (threads or semaphores). After the downgrade, you can cooperate with the downgrade interface to return the bottom data.
  • Fuse: When the failure rate reaches the threshold, the downgrade is automatically triggered (for example, the failure rate is high due to network failure/timeout), and the fast failure triggered by the fuse will recover quickly.
  • Cache: Provides request cache and request merge implementation.

mechanism

isolation

  1. Thread pool isolation mode: Use a thread pool to store current requests, the thread pool processes the requests, sets the task return processing timeout time, and the accumulated requests enter the thread pool queue first. In this way, you need to apply for a thread pool for each dependent service, which has a certain resource consumption. The advantage is that it can cope with sudden traffic (when the traffic peak comes, the data can be stored in the thread pool team for processing slowly)
  2. Semaphore isolation mode: Use an atomic counter (or semaphore) to record how many threads are currently running. The request is to first determine the value of the counter. If the maximum number of threads is exceeded, new requests of this type will be discarded. Execute counting operation request to counter +1, and request to return counter -1. This method strictly controls threads and returns immediately, and cannot cope with sudden traffic (when the traffic peak comes, the number of threads processed exceeds the number, other requests will be returned directly, and the dependent services will not continue to be requested)

Fuse

Under normal circumstances, the circuit breaker is in the closed state (Closed). If the call continues to fail or timeout, the circuit is opened and enters the fusing state (Open). All subsequent calls within a period of time will be rejected (Fail Fast) . After a period of time, protection The device will try to enter the half-open state (Half-Open), allowing a small number of requests to come in and try, if the call still fails, it will return to the fuse state, if the call is successful, it will return to the circuit closed state

breaker

  • When a certain threshold is met (the default is more than 20 requests within 10 seconds)
  • When the failure rate reaches a certain level (by default, more than 50% of requests fail within 10 seconds)
  • When the above threshold is reached, the circuit breaker will open
  • When enabled, all requests will not be forwarded
  • After a period of time (the default is 5 seconds), the circuit breaker is half open at this time, and one of the requests will be forwarded. If it succeeds, the circuit breaker will close, if it fails, it will continue to open.

Downgrade

When a large number of interface call exceptions occur in a certain service or interface, the entire service or interface will be fused and will no longer be called. This becomes a degradation

 

Example

Please prepare a simple springcloud (Greenwich) project in advance, a registry, a consumer, and a service provider. The consumer service is based on Feign remote calls.

Since Spring Cloud Hystrix is ​​the consumer side to achieve service degradation, service fuse and other functions, the following dependencies are introduced in the consumer service

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

Transform Feign call interface

package com.leolee.msf.feignInterface;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("database-web")
public interface TestClinet {

    @RequestMapping(value = "/test/hello", method = RequestMethod.GET)
    public String feignHello();

    @RequestMapping(value = "/test/value", method = RequestMethod.GET)
    public String feignValue();
}

The key points are as follows:

package com.leolee.msf.service;


import com.leolee.msf.feignInterface.TestClinet;
import com.leolee.msf.service.serviceInterface.TestService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName TestServiceImpl
 * @Description: TODO
 * @Author LeoLee
 * @Date 2020/8/18
 * @Version V1.0
 **/
@Service("loginService")
public class TestServiceImpl implements TestService {

    @Autowired
    private TestClinet testClinet;

    @HystrixCommand(fallbackMethod = "fallback")
    @Override
    public String getFeignValue() {
        return testClinet.feignValue();
    }

    public String fallback() {
        return "Hystrix";
    }
}

After injecting the Feign interface, add @HystrixCommand(fallbackMethod = "fallback") to the calling method

fallbackMethod points to the fallback() method. When a timeout occurs, it will actively abandon the remote call and call the local fallback() method instead.

Related configuration

ribbon:
#  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置规则 随机
#  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询
#  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置规则 重试
#  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重
#  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # 默认为;轮询,这里改为随机
#  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略
  ConnectTimeout: 5000 # 连接超时时间(ms)
  ReadTimeout: 5000 # 通信超时时间(ms)

feign:
  hystrix:
    enabled: true # 开启Feign的熔断功能
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 7000 # 设置hystrix的超时时间为6000ms

feign.hystrix.enabled=true enables feign fault tolerance

Hystrix default call timeout time is 6000ms, it can be adjusted according to actual situation

Guess you like

Origin blog.csdn.net/qq_25805331/article/details/108297545