美文网首页
Spring Cloud Netflix—断路器:Hystrix

Spring Cloud Netflix—断路器:Hystrix

作者: 万万558 | 来源:发表于2018-03-02 14:14 被阅读0次

     Netflix的创造了一个调用的库Hystrix实现了断路器图案。在微服务架构中,通常有多层服务调用。

    图1.微服务图

    较低级别的服务中的服务故障可能导致用户级联故障。当对特定服务的呼叫达到一定阈值时(Hystrix中的默认值为5秒内的20次故障),电路打开,不进行通话。在错误和开路的情况下,开发人员可以提供后备。

    图2. Hystrix回退防止级联故障

    开放式电路会停止级联故障,并允许不必要的或失败的服务时间来愈合。回退可以是另一个Hystrix保护的调用,静态数据或一个正常的空值。回退可能被链接,所以第一个回退使得一些其他业务电话又回到静态数据。

    如何加入Hystrix

    要在项目中包含Hystrix,请使用组org.springframework.cloud和artifact idspring-cloud-starter-hystrix的启动器。有关 使用当前的Spring Cloud发布列表设置构建系统的详细信息,请参阅Spring Cloud项目页面

    示例启动应用程序:

    @SpringBootApplication

    @EnableCircuitBreaker

    public class Application {

        public static void main(String[] args) {

            new SpringApplicationBuilder(Application.class).web(true).run(args);

        }

    }

    @Component

    public class StoreIntegration {

        @HystrixCommand(fallbackMethod = "defaultStores")

        public Object getStores(Map parameters) {

            //do stuff that might fail

        }

        public Object defaultStores(Map parameters) {

            return /* something useful */;

        }

    }

    @HystrixCommand由名为“javanica”的Netflix contrib库提供 。Spring Cloud在连接到Hystrix断路器的代理中使用该注释自动包装Spring bean。断路器计算何时打开和关闭电路,以及在发生故障时应该做什么。

    要配置@HystrixCommand,您可以使用commandProperties属性列出@HystrixProperty注释。请参阅 这里 了解更多详情。有关 可用属性的详细信息,请参阅Hystrix维基

    源码来源

    相关文章

      网友评论

          本文标题:Spring Cloud Netflix—断路器:Hystrix

          本文链接:https://www.haomeiwen.com/subject/rwdaxftx.html