为什么消费侧和服务侧同时加上HystrixCommand会有多次请求

消费端和服务端同时加服务降级fallback,出现多次请求

在练习中遇到的问题:使用Hystrix对消费侧,服务侧同时加入fallback降级。
1.对消费侧降级要求是响应时长超过5s,则降级到另一个接口
2.对服务侧降级要求是响应时长超过5s,则降级到另一个接口
3.服务侧被降级接口被调用时方法内等待3s,之后返回信息。
4.通过请求消费者(被降级)接口,消费者接口通过OpenFeign服务调用服务方(被降级)接口。
5.按照逻辑5s内消费者会返回服务方被降级接口的返回,但实操情况是,日志中先打印服务方降级后接口的返回,之后打印两次服务方被降级接口返回。

1.消费侧代码

Controller

@GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")  //3秒钟以内就是正常的业务逻辑
    })
    public String paymentInfo_Timeout(@PathVariable("id") Integer id){
        log.info("-----------------------");
        String result = feignService.paymentInfo_TimeOut(id);
        log.info("************result: "+result);
        return result;
    }

    public String paymentInfo_TimeoutHandler(@PathVariable("id")  Integer id){
        log.info("--------------------------------------------------");
        String result = feignService.paymentInfo_TimeoutHandler(id);
        return result ;
    }

Service

 @GetMapping("/payment/hystrix/timeout/{id}")
    String paymentInfo_TimeOut(@PathVariable("id") Integer id);

    @GetMapping("/payment/hystrix/timeouthandler1/{id}")
    String paymentInfo_TimeoutHandler(@PathVariable("id") Integer id);

2.服务方代码

Controller

@GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_Timeout(@PathVariable("id") Integer id){

        log.info("8001-----------------------------------"+id);
        String result = paymentService.paymentInfo_Timeout(id);
        log.info("************result: "+result);
        return result;
    }

    @GetMapping("/payment/hystrix/timeouthandler1/{id}")
    public String paymentInfo_TimeoutHandler1(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_TimeoutHandler1(id);
        log.info("************result: "+result);
        return result;
    }

Service

@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler1" ,commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")  //3秒钟以内就是正常的业务逻辑
    })
    public String paymentInfo_Timeout(Integer id){
        System.out.println("---------------------------------------------8001");
        long time = 4000;
//        int age = 10/0;
        try{
            TimeUnit.MILLISECONDS.sleep(time);
//            Thread.sleep(time);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        return "线程池: " + Thread.currentThread().getName() + "paymentInfo_TimeOut:耗时"+time+"ms,id: O(∩_∩)O" + id;
    }

    public String paymentInfo_TimeoutHandler1(Integer id){

        return "线程池: " + Thread.currentThread().getName() + "系统繁忙, ┭┮﹏┭┮" ;
    }

3.浏览器调用消费者接口

在这里插入图片描述

4.日志输出

2020-07-01 21:00:52.205  INFO 4076 --- [io-8001-exec-10] c.f.s.controller.PaymentController       : 8001-----------------------------------2
---------------------------------------------8001
2020-07-01 21:00:53.211  INFO 4076 --- [nio-8001-exec-8] c.f.s.controller.PaymentController       : 8001-----------------------------------2
2020-07-01 21:00:53.212  INFO 4076 --- [nio-8001-exec-3] c.f.s.controller.PaymentController       : ************result: 线程池: http-nio-8001-exec-3系统繁忙, ┭┮﹏┭┮
---------------------------------------------8001
2020-07-01 21:00:56.210  INFO 4076 --- [io-8001-exec-10] c.f.s.controller.PaymentController       : ************result: 线程池: hystrix-PaymentService-10paymentInfo_TimeOut:耗时4000ms,id: O(∩_∩)O2
2020-07-01 21:00:57.215  INFO 4076 --- [nio-8001-exec-8] c.f.s.controller.PaymentController       : ************result: 线程池: hystrix-PaymentService-9paymentInfo_TimeOut:耗时4000ms,id: O(∩_∩)O2
2020-07-01 21:04:06.529  INFO 4076 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2020-07-01 21:09:06.530  INFO 4076 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

真的很诧异为什么是这样结果,如描述问题用词有错还请包涵,感谢感谢!!!!!!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值