Chapter 8 Detailed Explanation of Hystrix Circuit Breaker - Global Service Degradation of Service Degradation

        In Chapter 7, partial service degradation is used in both ProductController and OrderController, but it also leads to two problems. By observing two partial degradation cases, we can find:

  • Each business method corresponds to a degraded method, which will lead to code expansion
  • The business logic method is mixed with the downgrade method for handling service exceptions.

1. Global downgrade

  • Each business method corresponds to a degraded method, which will lead to code expansion

       To solve the first problem that each business method corresponds to a downgrade method, which will lead to code expansion, you can use @DefaultProperties(defaultFallback = "") for global service downgrade.

       Use  @DefaultProperties(defaultFallback = "") to jump to the unified processing result page for other ordinary ones. Some important core businesses are exclusive, and the general and exclusive ones are separated to avoid code expansion and reduce the amount of code reasonably.

       Taking the order service OrderController as a case, explain how to solve the problem of code expansion. All the bottom-up methods in OrderController are downgraded using a unified global bottom-up method. Use exclusives for individual methods such as the buy( )  method. The process is as follows:

1. Define the global downgrade method

2. Use the @HystrixCommand annotation to specify exclusive

      Use this annotation on the private method to specify that this method needs a downgrade method for timeout exceptions. Other methods use the annotation @HystrixCommand to use the global degradation method.

3. Use the @DefaultProperties annotation to specify global degradation

Global fallbacks are specified using the annotation @DefaultProperties      on the OrderController class of the order service .

The specific codes are all modified on the basis of Chapter 7, the codes are as follows:


import com.hwadee.springcloud.entity.Product;
import com.hwadee.springcloud.service.IOrderFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "globalHystrixHandler")
public class OrderController {
    @Autowired
    IOrderFeignService orderFeignService;

    @RequestMapping("/buy/{id}")
    // 指定专属降级服务
    @HystrixCommand(fallbackMethod = "buyTimeout", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    public Product buy(@PathVariable Long id) {
        System.out.println("进入OrderController的buy方法, orderFeignService 准备调用远端接口 findById");
        Product product = orderFeignService.findOrderById(id);
        return product;
    }

    @RequestMapping(value = "/delete/{id}")
    @HystrixCommand // 未指定专属则使用全局降级服务
    public Product deleteOrderById(@PathVariable Long id) {
        System.out.println("进入OrderController的deleteOrderById方法, orderFeignService 准备调用远端接口deleteOrderById");
        int i =10/0;
        Product product = orderFeignService.deleteOrderById(id);
        return product;
    }

    // 专属降级服务
    public Product buyTimeout(Long id) {
        Product product = new Product();
        product.setId(id);
        product.setName("当前订单服务访问/order/buy/1 超时 进行专属服务降级:" + id);
        return product;
    }

    // 全局降级服务
    public Product globalHystrixHandler() {
        Product product = new Product();
        product.setName("当前订单服务访问/order/delete/1 10/0发生异常 进行全局服务降级:");
        return product;
    }
}

 2. Test

        Visit respectively: http://localhost:9000/order/buy/1 and http://localhost:9000/order/delete/1 for global downgrade and exclusive downgrade. The result is as follows:

Chapter 7: Detailed Explanation of Hystrix Circuit Breaker - Partial Downgrade of Service Downgrade

Chapter 9: Detailed explanation of Hystrix circuit breaker + global decoupling of service degradation

Guess you like

Origin blog.csdn.net/qq_41946216/article/details/127362089