zoukankan      html  css  js  c++  java
  • SpringCloud学习笔记(3)——Hystrix

    参考Spring Cloud官方文档第13、14、15章

    13. Circuit Breaker: Hystrix Clients

    Netflix提供了一个叫Hystrix的类库,它实现了断路器模式。在微服务架构中,通常一个微服务会调用多个其他的微服务。一个相对低层级的服务失败可能造成上层应用的级联失败,服务访问量越大失败率越高。当断路打开的时候,这个调用就被终止了。打开的断路可以阻止级联失败。

    13.1 How to Include Hystrix

    15.1 How to Include Hystrix Dashboard

    为了使用Hystrix仪表盘,需要在Spring Boot main class上用@EnableHystrixDashboard注解标注。

    上代码

    在原先的ribbon-demo基础上修改

    以上是在Ribbon中使用Hystrix,而Feign默认就支持Hystrix。下面看一下在Feign中如何使用Hystrix。

    17.4 Feign Hystrix Support

    如果Hystrix在classpath中,并且feign.hystrix.enabled=true的话,那么Feign将用断路器包装所有的方法。

    注意:在Spring Cloud Dalston之前的版本中,如果classpath中有Hystrix,那么Feign默认回为所有的方法加上断路器。这种默认的行为在Spring Cloud Dalston版本中被改变了,取而代之的是可选的。

    17.5 Feign Hystrix Fallbacks

    Hystrix支持回调,当断路器打开的时候回回调默认的代码。为了回调指定的方法,可以在@FeignClient中设置fallback属性,它的值是类的名字。例如:

     

     如果需要获得回调触发的原因,可以使用@FeignClient的fallbackFactory属性。

    上代码

    基于先前的feigen-demo工程改造

    其它的不变

    个人感觉还是用@EnableCircuitBreaker+@HystrixCommand的方式更方便一点儿

     至于断路器仪表盘还是跟前面一样配置

  • 相关阅读:
    Unique Binary Search Trees——LeetCode
    Binary Tree Inorder Traversal ——LeetCode
    Maximum Product Subarray——LeetCode
    Remove Linked List Elements——LeetCode
    Maximum Subarray——LeetCode
    Validate Binary Search Tree——LeetCode
    Swap Nodes in Pairs——LeetCode
    Find Minimum in Rotated Sorted Array——LeetCode
    Linked List Cycle——LeetCode
    VR AR MR
  • 原文地址:https://www.cnblogs.com/sunsing123/p/10275341.html
Copyright © 2011-2022 走看看