[Microservice Notes 10] Hystrix of microservice components implements service degradation and service fusing

This article mainly introduces Hystrix of microservice components to implement service degradation and service fusing.

Table of contents

1. Service downgrade

1.1. What is service downgrade?

1.2. Realize service downgrade

(1) Introduce dependencies

(2) Write Service layer code

(3) Write Controller layer code

(4) Run the test

(5) fallbackMethod attribute

2. Service fuse

2.1. What is a service circuit breaker?

2.2. Realize service fusing

(1) Introduce dependencies

(2) Write Service layer code

(3) Write Controller layer code

(4) Run the test


1. Service downgrade

1.1. What is service downgrade?

Service degradation: occurs on the client side. When a microservice has a large number of requests and cannot handle them, a certain strategy can be adopted at this time to directly interrupt those unimportant requests and return to the pre-defined The fallback method (can be called the bottom-up method), so that more resources can be provided for those core business interfaces.

Service downgrade is the easiest way to implement it, so under what circumstances will service downgrade start? ? ?

  • The first case: When an exception other than [HystrixBadRequestException] occurs, the service downgrade will be triggered.
  • The second case: when the interface method call times out, service degradation is triggered.
  • The third case: when the fuse is turned on.
  • The fourth case: when the thread pool, queue, and semaphore have reached their maximum value.

1.2. Realize service downgrade

(1) Introduce dependencies

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

(2) Write Service layer code

  • On the method that needs to enable service downgrade in the service layer, use the [@HystrixCommand] annotation, and use the [fallbackMethod] attribute to specify the fallback method after downgrade.
package com.gitcode.hystrix.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;

/**
 * @version 1.0.0
 * @Date: 2023/4/10 21:47
 * @Author ZhuYouBin
 * @Description 服务降级
 */
@Service
public class HystrixFallbackService {

    /**
     * 标记当前方法开启服务降级
     */
    @HystrixCommand(fallbackMethod = "reductionGradeFallback")
    public String reductionGrade(String id) {
        if (id.equals("1001")) {
            throw new RuntimeException("模拟业务异常");
        }
        System.out.println("模拟调用其他微服务接口......");
        return "success.";
    }

    /**
     * fallback 方法,必须和 @HystrixCommand 注解标记的方法具备相同的【参数列表】、【返回值类型】
     */
    private String reductionGradeFallback(String id) {
        System.out.println("服务降级,请稍后重试!");
        return "服务降级,请稍后重试!";
    }
}

(3) Write Controller layer code

package com.gitcode.hystrix.controller;

import com.gitcode.hystrix.service.HystrixFallbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0.0
 * @Date: 2023/4/10 21:46
 * @Author ZhuYouBin
 * @Description 服务降级
 */
@RestController
@RequestMapping("/hystrix/fallback")
public class HystrixFallbackController {

    @Autowired
    private HystrixFallbackService hystrixFallbackService;

    @GetMapping("/reduce")
    public String reductionGrade(String id) {
        return hystrixFallbackService.reductionGrade(id);
    }

}

(4) Run the test

Start the project, and when the browser accesses [ http://127.0.0.1:9898/hystrix/fallback/reduce?id=1001 ], an exception will be thrown at this time, triggering the service downgrade function; modify the id to other values, it can be normal transfer.

(5) fallbackMethod attribute

The fallbackMethod attribute is used to specify the alternative method to call after the service is downgraded or broken. In this method, we can return some simulated data according to the specific business scenario. For example: when the interface is degraded or broken, alternative data can be queried from the cache or the database and returned to the client. The method specified by the fallbackMethod attribute must satisfy the following rules:

  • 1. The method must be in the same class as the method marked with the @HystrixCommand annotation.
  • 2. The [parameter list] of the method must be the same as the method parameter list marked by the @HystrixCommand annotation.
  • 3. The [return value type] of the method must be the same as the return value of the method marked by the @HystrixCommand annotation.

2. Service fuse

2.1. What is a service circuit breaker?

Service fusing: Occurs on the server side. When a microservice fails and the service is unavailable, the caller service finds that the service is unavailable. At this time, the fusing mechanism will be activated to block all requests to the server. Return the pre-defined fallback method.

Service fusing is like a fuse in a circuit. When it is found that the downstream system service is unavailable, hystrix will turn on the fuse at this time, block all interfaces that call the downstream system, and directly return to the fallback method. If no fallback method is set, the default return strategy will be executed.

The fuse mechanism, hystrix provides a fuse, and its working principle is roughly as follows:

  • When calling the downstream service interface is normal, the fuse mechanism will not be enabled.
  • When the number of calls to the downstream service interface exceeds 20, and the interface call failure rate exceeds 50%, then the fuse will be turned on at this time.
  • Once the fuse is turned on, the microservice will not continue to call the downstream system interface, but will directly return to the bottom-up method specified by fallback (by default, the default method will be executed).
  • But the fuse cannot always be in the blown state, it must be turned on again, so hystrix will try to call the downstream system every [5 seconds] by default to determine whether the downstream system service is available.
  • When hystrix finds that the downstream system service is available again, it will close the fuse at this time.

Both service degradation and service fusing can use a pre-prepared fallback method, which is equivalent to a backup solution, but this solution is not the correct business result, but in this way, system availability can be improved. Why can performance be improved? Woolen cloth? You can think of it this way, because every time a request arrives at the server, the fallback result is returned directly, without the need to initiate an HTTP request to call another microservice, so this reduces the number of requests.

2.2. Realize service fusing

(1) Introduce dependencies

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

(2) Write Service layer code

package com.gitcode.hystrix.service;

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.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @version 1.0.0
 * @Date: 2023/4/10 21:47
 * @Author ZhuYouBin
 * @Description 服务熔断
 */
@Service
public class HystrixCircuitBreakerService {

    /**
     * 标记当前方法开启服务降级
     */
    @HystrixCommand(
            commandProperties = {
                    // 启用熔断器
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value = "true"),
                    // 设置请求线程数量,默认是20个时候,发生熔断,这设置成5个
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "5"),
                    // 请求线程失败的比率,默认是大于50%时候发生熔断
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "50"),
                    // 熔断机制重试策略的时间间隔,默认是5秒重试一次
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value = "5"),
            },
            fallbackMethod = "circuitBreakFallback"
    )
    public String circuitBreaker(String id) {
        if (id.equals("1001")) {
            throw new RuntimeException("模拟业务异常");
        }
        System.out.println("模拟调用其他微服务接口......当前时间:" + LocalDateTime.now());
        return "success.";
    }

    /**
     * fallback 方法,必须和 @HystrixCommand 注解标记的方法具备相同的【参数列表】、【返回值类型】
     */
    private String circuitBreakFallback(String id) {
        System.out.println("服务熔断降级,请稍后重试!");
        return "服务熔断降级,请稍后重试!";
    }
}

(3) Write Controller layer code

package com.gitcode.hystrix.controller;

import com.gitcode.hystrix.service.HystrixCircuitBreakerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0.0
 * @Date: 2023/4/10 21:46
 * @Author ZhuYouBin
 * @Description 服务熔断
 */
@RestController
@RequestMapping("/hystrix/fallback")
public class HystrixCircuitBreakerController {

    @Autowired
    private HystrixCircuitBreakerService service;

    @GetMapping("/breaker")
    public String circuitBreaker(String id) {
        return service.circuitBreaker(id);
    }

}

(4) Run the test

Start the project, visit [ http://127.0.0.1:9898/hystrix/fallback/breaker?id=1001 ] in the browser , visit a few more times, this will cause hystrix to open the fuse, and then access the request whose id is not 1001 to view the console output.

So far, the service downgrade and service fusing functions in Hystrix have been introduced.

In summary, this article is over, mainly introducing Hystrix of microservice components to implement service degradation and service fusing.

Guess you like

Origin blog.csdn.net/qq_39826207/article/details/130038452