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

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

1、订单侧使用fallback


     

在cloud-provider-payment8001中完成


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

             

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


public String paymentInfo_TimeOutHandler(Integer id) {
        return "线程池:  "+Thread.currentThread().getName()+"  8001系统繁忙或者运行报错,请稍后再试,id:  "+id+"\t"+"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+"\t"+"O(∩_∩)O哈哈~"+"  耗时(秒): "+timeNumber;
    }
    public String paymentInfo_TimeOutHandler(Integer id) {
        return "线程池:  "+Thread.currentThread().getName()+"  8001系统繁忙或者运行报错,请稍后再试,id:  "+id+"\t"+"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+"\t"+"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


相关文章
|
16小时前
服务熔断器-Hystrix
服务熔断器-Hystrix
23 2
|
16小时前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
201 0
|
16小时前
|
Java Nacos 微服务
全面学习SpringCloud框架指南
V 哥建议:如果你正在学习SpringCloud,你可以把这篇文章作为学习指南,如果你已经学过了,可以查漏补缺,因为 V 哥这篇文章全面介绍了SpringCloud的方方面面,当然你还需要结合官方文档的细节和源码部分去学习,成功拿下SpringCloud不是问题。加油兄弟!
|
16小时前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
16小时前
|
监控 Java 微服务
第八章 Spring Cloud 之 Hystrix
第八章 Spring Cloud 之 Hystrix
14 0
|
16小时前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
16小时前
|
Dubbo Java 应用服务中间件
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
|
16小时前
Springcloud-ribbon和hystrix配置
Springcloud-ribbon和hystrix配置
10 0
|
16小时前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
254 0
|
16小时前
|
Java API Nacos
第十二章 Spring Cloud Alibaba Sentinel
第十二章 Spring Cloud Alibaba Sentinel
28 0