美文网首页
6.Hystrix-多级降级方案

6.Hystrix-多级降级方案

作者: 溅十三 | 来源:发表于2020-06-06 15:46 被阅读0次
image.png

1.Fallback中修改error方法,不再使用静默方法,再次抛出异常

  • RuntimeException fallback2 fallback3
  • @HystrixCommand 需要保持协同,即 fallback2 fallback3的传参要一致
package com.imooc.springcloud;

import com.imooc.springcloud.hystrix.RequestCacheService;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    @Autowired
    private MyService myService;
    @Autowired
    private RequestCacheService requestCacheService;

    @GetMapping("/fallback")
    public String fallback(){
        return myService.error();
    }

    @GetMapping("/timeout")
    public String timeout(Integer timeout){
        return myService.retry(timeout);
    }

    @GetMapping("/cache")
    public Friend cache(String  name){
        @Cleanup HystrixRequestContext context =
                HystrixRequestContext.initializeContext();

        Friend friend = requestCacheService.requestCache(name);
        friend = requestCacheService.requestCache(name);
        return friend;
    }

}

2.Hystrix超时的另外一种方式(方法级别的配置)

  • timeout2
  • HystrixCommand fallbackMethod @HystrixProperty
  • 注释掉hystrix.command.MyService#retry(int).execution.isolation.thread.timeoutInMilliseconds=3000
package com.imooc.springcloud;

import com.imooc.springcloud.hystrix.RequestCacheService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import lombok.Cleanup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    @Autowired
    private MyService myService;
    @Autowired
    private RequestCacheService requestCacheService;

    @GetMapping("/fallback")
    public String fallback(){
        return myService.error();
    }

    @GetMapping("/timeout")
    public String timeout(Integer timeout){
        return myService.retry(timeout);
    }

    @GetMapping("/timeout2")
    @HystrixCommand(
            fallbackMethod = "timeoutFallback",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="3000")
            }
    )
    public String timeout2(Integer timeout){
        return myService.retry(timeout);
    }

    public String timeoutFallback(Integer timeout){

    }

    @GetMapping("/cache")
    public Friend cache(String  name){
        @Cleanup HystrixRequestContext context =
                HystrixRequestContext.initializeContext();

        Friend friend = requestCacheService.requestCache(name);
        friend = requestCacheService.requestCache(name);
        return friend;
    }

}

相关文章

网友评论

      本文标题:6.Hystrix-多级降级方案

      本文链接:https://www.haomeiwen.com/subject/hhnxtktx.html