首页 > 编程语言 > 详细

SpringCloud降级熔断 Hystrix

时间:2020-06-21 12:39:02      阅读:137      评论:0      收藏:0      [点我收藏+]

1、分布式核心知识之熔断、降级讲解
简介:系统负载过高,突发流量或者网络等各种异常情况介绍,常用的解决方案

    1、熔断:
         保险丝,熔断服务,为了防止整个系统故障,包含子和下游服务

          下单服务 -》商品服务
                          -》用户服务 (出现异常-》熔断)

      2、降级:
             抛弃一些非核心的接口和数据

              旅行箱的例子:只带核心的物品,抛弃非核心的,等有条件的时候再去携带这些物品


       3、熔断和降级互相交集
        相同点:
        1)从可用性和可靠性触发,为了防止系统崩溃
         2)最终让用户体验到的是某些功能暂时不能用

        不同点
        1)服务熔断一般是下游服务故障导致的,而服务降级一般是从整体系统负荷考虑,由调用方控制

2、Netflix开源组件断路器Hystrix介绍
简介:介绍Hystrix基础知识和使用场景

  文档地址:
  https://github.com/Netflix/Hystrix
  https://github.com/Netflix/Hystrix/wiki

  1、什么是Hystrix?
  1)hystrix对应的中文名字是“豪猪”
  2)hystrix 英[h?st‘r?ks] 美[h?st‘r?ks]


  2、为什么要用?
  在一个分布式系统里,一个服务依赖多个服务,可能存在某个服务调用失败,
  比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,
  通过Hystrix就可以解决

  http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#_circuit_breaker_hystrix_clients

  3、提供了熔断、隔离、Fallback、cache、监控等功能

  4、熔断后怎么处理?
  出现错误之后可以 fallback 错误的处理信息
  兜底数据

 

3、Feign结合Hystrix断路器开发实战

  简介:讲解SpringCloud整合断路器的使用,用户服务异常情况

  1、加入依赖
  注意:网上新旧版本问题,所以要以官网为主,不然部分注解会丢失
  最新版本 2.0

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

  2、增加注解
  启动类里面增加注解
  @EnableCircuitBreaker

@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }

}

  3、API接口编码实战
  熔断-》降级

  1)最外层api使用,好比异常处理(网络异常,参数或者内部调用问题)
  api方法上增加 @HystrixCommand(fallbackMethod = "saveOrderFail")

  编写fallback方法实现,方法签名一定要和api方法签名一致(注意点!!!)

@RestController
@RequestMapping("api/v1/order")
public class OrderController {


    @Autowired
    private ProductOrderService productOrderService;

    @RequestMapping("save")
    @HystrixCommand(fallbackMethod = "saveOrderFail")
    public Object save(@RequestParam("user_id")int userId, @RequestParam("product_id") int productId){

        Map<String, Object> data = new HashMap<>();
        data.put("code", 0);
        data.put("data", productOrderService.save(userId, productId));
        return  data;
    }


    //注意,方法签名一定要要和api方法一致
    private Object saveOrderFail(int userId, int productId){

        Map<String, Object> msg = new HashMap<>();
        msg.put("code", -1);
        msg.put("msg", "抢购人数太多,您被挤出来了,稍等重试");
        return msg;
    }
}

  调用失败后会调saveOrderFail方法返回结果

       4、Feign结合Hystrix断路器(接口实现)

    简介:讲解SpringCloud整合断路器的使用,用户服务异常情况
    1、feign结合Hystrix

    1)yml开启feign支持hystrix (注意,一定要开启,旧版本默认支持,新版本默认关闭)
    feign:
      hystrix:
         enabled: true

        2)FeignClient(name="xxx", fallback=xxx.class ), class需要继承当前FeignClient的类

/**
 * 商品服务客户端
 */
@FeignClient(name = "product-service", fallback = ProductClientFallback.class)
public interface ProductClient {

    @GetMapping("/api/v1/product/find")
    String findById(@RequestParam(value = "id") int id);
}

/**
 * 针对商品服务,错降级处理
 */
@Component
public class ProductClientFallback implements ProductClient {

    @Override
    public String findById(int id) {

        System.out.println("feign 调用product-service findbyid 异常");
        return null;
    }
}

调用接口出错后会执行ProductClientFallback .findById, API 返回OrderController. saveOrderFail

  

SpringCloud降级熔断 Hystrix

原文:https://www.cnblogs.com/daxiong225/p/13172005.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!