栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录

1、订单侧使用fallback

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

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

         (3)运行测试

2、服务侧使用fallback

        (1)yml

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

        (3)逻辑代码

        (4)运行测试

3、解决代码膨胀

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

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

        (3)运行测试

4、实现解耦

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

        2、 在service接口上标注

        3、运行测试


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";
    }

 

         (3)运行测试

                1.测试超时

        在浏览器地址栏输入

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

               2.测试异常

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

2、服务侧使用fallback

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

        (1)yml
feign:
  hystrix:
    enabled: true

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

        (3)逻辑代码

        与上面订单侧一致

        (4)运行测试

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

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

3、解决代码膨胀

使用DefaultProperties

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

 

        (1)在类上加上注解@DefaultProperties
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")

 

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

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

@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)运行测试

 

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接口上标注

        3、运行测试

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

  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490106.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号