zl程序教程

您现在的位置是:首页 >  其他

当前栏目

SpringCloud学习(十三):Hystrix的服务降级实现

2023-03-14 22:40:46 时间

1、订单侧使用fallback


       

在cloud-provider-payment8001中完成


(1)为paymentInfo_TimeOut写一个降级的服务

               

当我们当前的方法不能正常运行时,需要有一个方法来为他兜底


public String paymentInfo_TimeOutHandler(Integer id) {
        return "线程池:  "+Thread.currentThread().getName()+"  8001系统繁忙或者运行报错,请稍后再试,id:  "+id+"	"+"o(╥﹏╥)o";
    }


(2)开启和激活服务降级

        

为paymentInfo_TimeOut加上@HystrixCommand注解,并在主启动类上添加@EnableCircuitBreaker注解


@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")
    })
    public String paymentInfo_TimeOut(Integer id) {
        int timeNumber = 5;
//        int age = 10/0;
        try {
            TimeUnit.SECONDS.sleep(timeNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池:  "+Thread.currentThread().getName()+" id:  "+id+"	"+"O(∩_∩)O哈哈~"+"  耗时(秒): "+timeNumber;
    }
    public String paymentInfo_TimeOutHandler(Integer id) {
        return "线程池:  "+Thread.currentThread().getName()+"  8001系统繁忙或者运行报错,请稍后再试,id:  "+id+"	"+"o(╥﹏╥)o";
    }


bdd5b7b3b3e346a692ae45faab16d331.png


(3)运行测试

               

1.测试超时

       

在浏览器地址栏输入

       

http://localhost:8001/payment/hystrix/timeout/1

016af9c2e3e54394a6a8ac7a7c1d77cf.png


2.测试异常

       

将代码中睡眠5秒的代码注释掉,将模拟异常的 int age = 10/0; 注释打开,重启测试

fe097b6ee87f4bd4a69ef07f389de68f.png


2、服务侧使用fallback



这里涉及到一个重要的常识:Hystrix不仅能放在消费侧,也能放在服务侧。


(1)yml


1. feign:
2.   hystrix:
3.     enabled: true


2)在主启动类上加上@EnableHystrix注解


(3)逻辑代码

        

与上面订单侧一致


(4)运行测试

       

启动7001、8001、80,在浏览器地址栏输入

       

http://localhost:90/consumer/payment/hystrix/timeout/1

ef3ceaaa185c44c2a9adaafad9028e4e.png


3、解决代码膨胀



使用DefaultProperties

        假如每个方法都需要一个兜底的降级方法,那一百个方法就需要一百个兜底,而这些兜底方法的代码逻辑往往大同小异,导致代码膨胀,于是我们可以写一个全局通用的降级方法。


(1)在类上加上注解@DefaultProperties


@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")


(2)暂时把fallbackMethod里的内容注释

       

因为就近原则,fallbackMethod里的内容会干扰我们的测试,但是不要注释掉@HystrixCommand了。

e807d20ff8d84a4189a06b172c1c4a6c.png


@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {
 
    @Autowired
    private PaymentHystrixService paymentHystrixService;
 
    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_OK(id);
        log.info("===========result:" + result);
        return result;
    }
 
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand
//            (fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
//    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
//        int i = 10/0;
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("===========result:" + result);
        return result;
    }
    public String paymentInfo_TimeOutHandler(Integer id) {
        return "线程池:  "+Thread.currentThread().getName()+"  8001系统繁忙或者运行报错,请稍后再试,id:  "+id+"	"+"o(╥﹏╥)o";
    }
 
    // 全局fallback方法
    public String payment_Global_FallbackMethod()
    {
        return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
    }
}


(3)运行测试


6dd0f61206bc4c2a86401c29f813b041.png


4、实现解耦



        现在我们将fallback方法写在controller层上,那么有其他服务又要为每个服务配置fallback,并且与业务逻辑代码混在一起,耦合度高;并且在客户端去调用服务端时,若遇上服务端宕机,就不能实现服务降级。于是我们可以在80服务中单独写一个类继承service接口,来处理服务降级。


 1、新建PaymentFallbackService类,实现80服务中的PaymentHystrixService接口


@Component
public class PaymentFallbackService implements PaymentHystrixService{
    @Override
    public String paymentInfo_OK(Integer id) {
        return "----------PaymentHystrixService`s fallback-----------------";
    }
 
    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "----------PaymentHystrixService`s fallback-----------------";
    }
}


2、 在service接口上标注

74713f1d836d49a081332d58c31db1b7.png


3、运行测试

        

将之前配置的全局fallback方法暂时注释掉,重新运行。


195637b048c0432481351b1c66bda1a1.png