第八章 Hystrix断路器的详解-服务降级之全局服务降级

        第七章中在 ProductController 和 OrderController 中都使用了局部服务降级,但同时也导致两个问题,  通过观察两个局部降级的案例,可以发现:

  • 每个业务方法都对应一个降级方法,会导致代码膨胀
  • 业务逻辑方法和处理服务异常降级方法混在一起。

一、全局降级

  • 每个业务方法都对应一个降级方法,会导致代码膨胀

       为解决第一个问题即每个业务方法都对应一个降级方法,会导致代码膨胀,可以使用@DefaultProperties(defaultFallback = "") 进行全局服务降级。

       使用 @DefaultProperties(defaultFallback = "") 将其它普通的统一跳转到统一处理结果页面,个别重要核心业务有专属,通用的和独享的各自分开,避免了代码膨胀,合理减少了代码量。

       以 订单服务 OrderController 为案例,讲解解决代码膨胀膨胀问题。将 OrderController 中所有的兜底方法都使用统一的一个全局兜底方法进行降级。对个别的方法例如 buy( ) 方法使用专属。过程如下:

1、定义全局降级方法

2、使用 @HystrixCommand 注解 指定专属

      在专属方法上使用此注解指定此方法超时异常需要有降级兜底方法。其他方法使用注解@HystrixCommand 则使用全局的降级方法。

3、使用 @DefaultProperties 注解 指定全局降级

     在订单服务的 OrderController  类上使用注解 @DefaultProperties 指定全局降级。

具体代码均在第七章基础上进行修改,代码如下:


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;
    }
}

 二、进行测试

        分别访问:http://localhost:9000/order/buy/1 和 http://localhost:9000/order/delete/1 全局降级和专属降级。结果如下:

第七章:Hystrix断路器的详解-服务降级之局部降级

第九章:Hystrix断路器详解+服务降级之全局解耦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值