Introduction to Hystrix, a component of Spring Cloud

 Service Avalanche Introduction

If the service provider is unavailable, the service caller will also be unavailable, and so on, all microservices in the entire link will be unavailable.

If service provider A fails for some reason, service caller service B cannot successfully call the interface provided by service B depending on service A. Over time, more and more requests depending on service A will cause Tomcat resource consumption of service B. As a result, the thread of service B is blocked , and service B also fails. Then if service C depends on service B, service C fails because service B also fails. By analogy, all microservices in the entire link are unavailable.

① Understand:

② solve

Shorten the timeout period (not desirable)

advantage:

  • Simple

shortcoming:

  • Some service overtimes are pre-booked

  • In some services, there may be a large amount of code or connection to DB, etc. If the connection time is not enough for the execution time of the code, then a normal business cannot be executed.

Set interceptor

overview

It can be resolved in microservices 服务雪崩, called 熔断器or 断路器. Can prevent distributed projects from happening 联动故障(one service goes down, other services cannot run normally).

拦截器A similar scheme is set up in Hystrix . If the service that needs to be called is down, then the machine will not be called and used directly 备选方案.

use

① rent-car-service

configuration file

server:
  port: 8080
spring:
  application:
    name: rent-car-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    hostname: localhost
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
    lease-renewal-interval-in-seconds: 5

API:

@RestController
public class CarController {

    @GetMapping("rent")
    public String rent(){
        return "租车成功";
    }
}

② conusmer-service

hystrix-dependency

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

configuration file

server:
  port: 8081
spring:
  application:
    name: consumer-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    hostname: localhost
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
    lease-renewal-interval-in-seconds: 5

OpenFeign interface

@FeignClient(value = "rent-car-service",fallback = ConsumerRentCarFeignHystrix.class) // 指定发生服务雪崩时执行哪个类
public interface ConsumerRentCarFeign {

    @GetMapping("rent")
    String rent();
}

Hystrix implementation class

@Component // 添加到IOC容器中
public class ConsumerRentCarFeignHystrix implements ConsumerRentCarFeign {

    /**
     * 备用方案
     * 当调用的服务挂掉后执行本方法
     * @return
     */
    @Override
    public String rent() {
        return "网络异常 稍后重试";
    }
}

API

@RestController
public class ConsumerController {

    @Autowired
    private ConsumerRentCarFeign consumerRentCarFeign;

    @GetMapping("consumerToRent")
    public String consumerToRent(){
        System.out.println("有人来租车");
        return consumerRentCarFeign.rent();
    }
}

Enable Hystrix

feign:
  hystrix:
    enabled: true

circuit breaker thinking

Common configuration

hystrix: #hystrix 的全局控制
  command:
    default: #default 是全局控制,也可以换成单个方法控制,把 default 换成方法名即可
      fallback:
        isolation:
          semaphore:
            maxConcurrentRequests: 1000 #信号量隔离级别最大并发数
      circuitBreaker:
        enabled: true #开启断路器
        requestVolumeThreshold: 3 #失败次数(阀值)
        sleepWindowInMilliseconds: 20000 #窗口时间
        errorThresholdPercentage: 60 #失败率
      execution:
        isolation:
          Strategy: thread #隔离方式 thread 线程隔离集合和 SEMAPHORE 信号量隔离
        thread:
          timeoutInMilliseconds: 3000 #调用超时时长

 Related Interview Questions

What is Hystrix?

Anti-avalanche tool, with functions such as service degradation, service fuse, dependency isolation, monitoring (Hstrix Dashboard) and so on

What is a service circuit breaker? What is the function of the fuse?

First of all, let's talk about what is fan-out and avalanche effect: when calling between multiple microservices, assuming that microservices

Service A calls microservice B and microservice C, and microservice B and microservice C call other services.

It is the so-called fan-out. If the call response time of a microservice on the fan-out link is too long, or it is not

Available, then the microservice caller will block the thread, occupy more and more system resources, and then crash.

In the same way, the caller that affects the caller will collapse step by step, which is the so-called avalanche effect.

Then the service fusing mechanism is a microservice link protection mechanism to deal with the avalanche effect, and the fan-out link

If a microservice is unavailable, or the response time is too long, the service will be downgraded, and the microservice will be blown.

The call of the node microservice quickly returns the wrong response information, when it is detected that the node microservice calls the response

When normal, restore the link. In the SpringCloud framework, the fuse mechanism is implemented through Hystrix.

Hystrix will create many small thread pools, such as order service request inventory service is a thread pool,

The request storage service is a thread pool, and the request credit service is a thread pool. Threads in each thread pool

It is only used to request that service. When a certain thread pool reaches the threshold, the service fuse will be started, and the service will be degraded.

class. If other requests continue to visit, the default value of fallback will be returned directly.

What is service downgrade?

When the service is broken, do a blown process before returning, such as storing the request information in the data

The database is used for later data recovery. I think this kind of post-fuse processing refers to service degradation. priority core

Services, non-core services are unavailable or weakly available.

Service downgrade case: Double Eleven: "Ouch, I'm overwhelmed..." or app spike: "Network desertion

It's gone, please try again later...》

Implement the downgrade logic in fallbackMethod (fallback function).

Circuit Breaking and Degradation: Fail Fast After Calling a Service Fails

  • Fuse: It is to prevent the abnormal non-diffusion and ensure the stability of the system

  • Downgrade: Write the remedial logic for call failure, and then stop the service directly, so that these interfaces cannot be called normally, but it will not report an error directly, but the service level will drop

  • Wrap all external systems (or dependencies) through HystrixCommand or HystrixObservableCommand, and the entire wrapper object runs in a single thread (this is a typical command mode).

  • Timeout requests should exceed your defined threshold

  • maintain a small thread pool (or semaphore) for each dependency; if it becomes full, then the dependency's requests will be rejected immediately instead of being queued

  • Counts successes, failures (exceptions thrown by clients), timeouts and thread rejections

  • Opening a circuit breaker stops all requests to a feature service for a period of time, and manually or automatically closes the circuit breaker if the service's error percentage passes a threshold.

  • When the request is rejected, the connection times out or the circuit breaker is opened, the fallback logic is executed directly.

  • Monitor metrics and configuration changes in near real time.

How does Hystrix avoid the avalanche effect?

First of all, to avoid the formation of the avalanche effect requires a powerful fault-tolerant mechanism. Hystrix is ​​a tool library  that implements the timeout mechanism and circuit breaker mode.
Hystrix mainly implements delay and fault tolerance through the following points:
Package request:  use HystrixCOmmand to package the call logic for dependencies , each command is executed in an independent thread. The "command mode" tripping mechanism of the design mode is used
:  when the error rate of the service exceeds a certain threshold, Hystrix can automatically or manually trip and stop requesting the service for a period of
resource isolation:  Hystrix maintains a small thread pool or semaphore for each dependency. If the thread is full, the request sent to the dependency is rejected immediately instead of waiting in line, thus speeding up the failure determination. Monitoring: Hystrix can be nearly real
-  time Monitor running indicators and configuration changes, such as success, failure, timeout and rejected requests, etc.
Rollback mechanism:  When the request fails, times out, is rejected, or when the circuit breaker is opened, the rollback logic is executed. Rollback The logic is provided by itself, including returning a default value
for self-healing:  after the circuit breaker is opened for a period of time, it will automatically enter the "half-open state", try to request services, and the request will pass the recovery request. If the request fails, the circuit breaker will continue to open

Guess you like

Origin blog.csdn.net/weixin_45934981/article/details/130151817