Other usage and usage of spring cloud 2020.0.4 series service degradation

Keywords: Java Distribution Spring Cloud Microservices

catalogue

1. General

2. Other usage of hystrix service degradation

3. Hytrix realizes fusing

4. Overview

5. official account

1. General

As the old saying goes: only by controlling your emotions can you control your life. Impulse is the devil, calm is the most important.

To get back to business, I talked about how to add a degradation strategy to the entire Feign interface class when Feign is called.

Today, let's talk about other uses of Hystrix on service degradation and how to use Hystrix to implement the circuit breaker mechanism.

Without much gossip, go straight to the code.

2. Other usage of hystrix service degradation

2.1 configure degradation policy for local Service business method

Write the degradation method in the Service implementation class and use it on the Service method  @ The HystrixCommand annotation specifies the demotion method.

    @HystrixCommand(fallbackMethod = "businessFunctionFallback")
    public String businessFunction() {
        throw new RuntimeException("Simulation anomaly");
    }

    public String businessFunctionFallback() {
        return "businessFunction Demotion";
    }

2.2 configuration of multi-level degradation

If the degradation method also has the risk of throwing exceptions, multi-level degradation is not prevented.

    @HystrixCommand(fallbackMethod = "exception2")
    public String exception() {
        System.out.println("implement Service exception");
        throw new RuntimeException("Simulation anomaly");
    }

    @HystrixCommand(fallbackMethod = "exception3")
    public String exception2() {
        System.out.println("implement Service exception2");
        throw new RuntimeException("Simulation anomaly");
    }

    public String exception3() {
        System.out.println("implement Service exception3");
        return "fail";
    }

2.3 configuring the degradation method with annotations

The service timeout can be configured using annotations, and multiple attributes can be configured, separated by commas

    @HystrixCommand(commandKey = "myTimeout", fallbackMethod = "timeoutFallback",
            commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
    })
    public String timeout(Integer timeoutSecond) {
        System.out.println("call Service timeout");
        return eurekaClientService.timeout(timeoutSecond);
    }

  public String timeoutFallback(Integer timeoutSecond) {
        System.out.println("timeoutFallback: " + timeoutSecond);
        return "timeoutFallback: " + timeoutSecond;
    }

2.4 use commandKey to configure a degradation method in yml file

commandKey yes  @ A property of the HystrixCommand annotation  

hystrix:
  command:
    myTimeout:   # Set specific methods according to commandKey
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 4000     # Timeout configuration

3. Hytrix realizes fusing

3.1 fuse overview

Fusing is usually used in conjunction with degradation. Within the specified time window, when the requested failure rate reaches the set threshold, fusing will be turned on and the degradation method will be called directly instead of calling the real logic. Recovery will be attempted after a period of time, but if it continues to fail, fusing will continue.  

3.2 fuse configuration

hystrix:
  command:
    default:
      fallback:
        enabled: true     # Turn on service degradation
      execution:
        timeout:
          enabled: true   # Global timeout configuration
        isolation:
          thread:
            timeoutInMilliseconds: 1000    # Timeout configuration
            interruptOnTimeout: true       # Terminate thread after timeout
            interruptOnFutureCancel: true  # Terminate thread after cancellation

      circuitBreaker:
        enabled: true                      # Whether the fuse is turned on
        requestVolumeThreshold: 5          # In the time window, the number of requests can be determined only when the number of requests reaches
        errorThresholdPercentage: 50       # Failure percentage
        sleepWindowInMilliseconds: 15000   # After entering the fusing state, it will enter the half open state after how many ms, and will try to send a request. If it fails, it will continue to fuse, and if it succeeds, the service will be provided normally
      metrics:
        rollingStats:
          timeInMilliseconds: 20000        # Configure the time interval of the time window, in ms

4. Overview

I talked today   Other usage and related knowledge of service degradation. I hope it can be helpful to everyone's work.

Welcome to like, comment, forward and pay attention:)

Pay attention to those who follow the wind to talk about Java and update Java dry goods every day.

5. official account

Fans talk about Java. Welcome to pay attention

Posted by Ryodox on Thu, 28 Oct 2021 02:37:34 -0700