Hystrix

  • Hystrix is Netflix’s open source high availability framework, which perfectly solves the problem of high availability services in distributed system architecture
    • The circuit breaker
    • Service degradation
    • Service fusing
    • Service Isolation mechanism
    • Service avalanche effect
  • Hystrix is self-protective

Service Protection concept

  • In the distributed system with high availability of microservices, the following problems may occur: call timeout between services and call failure between services

Service avalanche effect

  • By default, Tomcat only a thread pool to deal with the client sends the request, so that in the case of high concurrency client all requests are stacked in the same service interface, Tomcat will produce all the thread pool to process the service interface, can lead to other service interface can’t access, so in other interface access will produce a delay and waiting
  • Severe service avalanche may cause a cascading avalanche effect, which may cause all microservice interfaces to be inaccessible and the entire service to break down
Tomcat has a pool of threads that handle each request sent by the clientCopy the code
  • Hystrix-based mechanisms for addressing service avalanche effects:
    • Service degradation:
    • Service fuse:
    • Service isolation:

Service degradation

  • In the case of high concurrency, to prevent the user from waiting, the service calls the fallBack method and returns a friendly prompt directly to the client without processing the request, in order to improve the user experience
When Tomcat does not have threads to process client requests, the interface should not rotate uniformly, making the user wait if the service calls other interfaces timeout (default 1 second), by default, the business logic can be executed, if the service does not respond directly to the service degradation methodCopy the code

Service fusing

  • In the case of high concurrency, set a service threshold. When the traffic exceeds the threshold, the protection function is automatically enabled and a friendly message is sent to the client in service degradation mode
  • Circuit breakers and service downgrades work together
  • The purpose of a service circuit breaker is to protect the service

Service isolation

  • There are two types of service isolation: thread pool isolation and semaphore isolation
  • Thread pool isolation:
    • Each service interface has its own separate thread pool, each with complementary impacts
    • Because thread pool CPU usage is very high, thread pool isolation is not used for all service interfaces, only for core critical interfaces

Hystrix environment setup

  • Import Hystrix dependencies: spring-cloud-starter-netflix-Hystrix
  • Turn on the Hystrix circuit breaker in the configuration file in the Service Consumer project
feign.hystrix.enabled=true
Copy the code
  • You can set the Hystrix service timeout period in the configuration file to prevent delayed service response and service degradation
hystrix.command.default.execution.isolation.thread. timeoutInMilliseconds=10000
Copy the code
  • Enable Fegin’s Hystrix function by annotating @enableFeignClient on the main class
  • Use the Hystrix framework to annotate @HystrixCommand on service implementation methods
By default, service degradation, service meltdown, and Service Isolation is enabled at @hystrixCommand. The thread pool isolation mode is enabled at @hystrixCommand HystrixCommand(fallback=" service degradation prompt method name "), where fallback is used for service degradationCopy the code

Fallback interface

  • Hystrix uses the fallback class to handle service degradation
Create a fallback class. 2. Load the class into the container with the @Component annotation. 3. When calling the fallback class, add the fallback argument @feignClient (fallback= fallback.class) to the @feignClient annotation.Copy the code