关于Hystrix进行服务降级的问题

服务端存在服务,假定处理时长为2000ms;客户端进行调用,容忍时长为3000ms,否则进行服务降级调用fallback处理。但是如果只是这样进行调用,那么客户端仍然会进行服务降级。测试代码如下:

服务处理假定为2000ms

@HystrixCommand(
            fallbackMethod = "fallBackPayment",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
            })
    public String paymentInfo_TimeOut(Integer id)
    {
        long speedTime = 2000;
        try { TimeUnit.MILLISECONDS.sleep(speedTime); } catch (InterruptedException e) { e.printStackTrace(); }
        return "线程池:  "+Thread.currentThread().getName()+" id:  "+id+"\t"+"  耗时(毫秒):"+speedTime;
    }

    private String fallBackPayment(Integer id){
        return "当前线程"+Thread.currentThread().getName()+" id: "+id
                + "\t" + "8001处理失败,调用fallBackMethod =.= ";
    }

客户端调用容忍时长为3000ms 

@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(
            fallbackMethod = "fallbackClientMethod",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
            })
    public String paymentInfo_Timeout(@PathVariable("id") Integer id){
        return paymentHystrixService.paymentInfo_TimeOut(id);
    }
    private String fallbackClientMethod(@PathVariable("id") Integer id){
        return "客户端请求超时或出现异常,请稍后重试";
    }

按照设想,此时应该返回正确的调用结果,但是客户端却进行了服务降级。这是因为,Hystrix的命令默认超时时长为1s,即使客户端的调用能够容忍2s的处理时长,但由于Hystrix命令的默认超时时间为1秒,因此在超过1秒后,Hystrix会认为服务调用超时,并触发服务降级逻辑,导致调用fallbackClientMethod方法。

处理方法:需要在配置文件中修改Hystrix命令的默认时长,比如设置命令超时时长为5000ms,即可解决上述问题。

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000

以上结论根据结果测试分析而来,由于并没有分析源码,所以可能存在问题。此处只是作为个人观点,如有问题,还望指正。=.=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值