springcloud微服务(二十) - Hystrix全局服务降级DefaultProperties

springcloud微服务(二十) - Hystrix全局服务降级DefaultProperties,第1张

springcloud微服务(二十) - Hystrix全局服务降级DefaultProperties

一、存在的问题

在前面的章节中,我们可以看到

1.每个业务方法都需要一个fallback方法,代码会急剧的膨胀

2.业务方法和fallback混杂在一起

二、解决方案

全局服务降级@DefaultProperties(defaultFallback=" 全局异常处理回退方法")

除了个别重要核心业务有专属的fallback方法,其他普通的业务方法可以通过@DefaultProperties(defaultFallback=" ") 跳转到统一的fallback方法。通用的和独享的各自分开,避免了代码膨胀,合理减少了代码量。

三、全局服务降级的实现

3.1 类上加注解@DefaultProperties(defaultFallback="全局异常处理回退方法 ")

3.2 业务方法加注解@HystrixCommand()

3.3 全局异常处理回退方法

@RestController
@DefaultProperties(defaultFallback = "payment_global_fallback_method")
public class HystrixOrderController {

    @Autowired
    private HystrixPaymentService hystrixPaymentService;

    @GetMapping("/consumer/payment/info/ok/{id}")
    public String paymentInfo_ok(@PathVariable("id") Integer id)
    {
        return hystrixPaymentService.paymentInfo_ok(id);
    }

    //模拟业务流程长,耗时长
    @GetMapping("/consumer/payment/info/timeout/{id}")
    

    @HystrixCommand(commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")})
    public String paymentInfo_timeout(@PathVariable("id") Integer id)
    {
        return hystrixPaymentService.paymentInfo_timeout(id);
    }
    public String paymentInfo_timeoutFallback(Integer id)
    {
        return "消费端80,支付系统异常,请稍后再试";
    }

    public String payment_global_fallback_method()
    {
        return "全局的异常处理";
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5672543.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存