• Hystrix 部署


    参考

    Java版本实现: github地址

    PHP版本实现: github地址

    实现

    Hystrix支持两种方式实现熔断降级功能:

    • 编程式

    • 声明式

    编程式

    在编程式实现中,基于Hystrix丰富的API,通过编码的方式实现断路器的设置。API要求用户将不同的外部交互封装成命令模式对象。在调用过程中,形成一层代理,Hystrix框架对命令对象执行周期接管,并根据窗口内获取到的状态报告决定断路器状态。

    • 添加核心依赖 hystrix-core 到pom.xml文件

    1. <dependency>
    2. <groupId>com.netflix.hystrix</groupId>
    3. <artifactId>hystrix-core</artifactId>
    4. <version>1.5.18</version>
    5. </dependency>
    • 包裹需要熔断处理的代码逻辑到Command类中

    1. static class CommandHello extends HystrixCommand<String> {
    2. private final String name;
    3. public CommandHello(String name) {
    4. super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
    5. .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(3000)));
    6. this.name = name;
    7. }
    8. @Override
    9. protected String runTest() {
    10. return "Hello " + name + "!";
    11. }
    12. // Default fallback method
    13. public String getFallback() {
    14. return "Fallback of Hello was triggered";
    15. }
    16. }
    • 初始化Command对象,并执行 execute() 方法。

    声明式

    顾名思义,通过配置声明的方式以达到以上方式同样的效果,这种方式对业务侵入小,无需编码自动植入代码功能,业务接受程度高。此方案需要引入依赖,并在main class中使用注释 EnableHystrix 激活该功能。

    • 添加核心依赖 hystrix-javanica 到pom.xml文件

    1. <dependency>
    2. <groupId>com.netflix.hystrix</groupId>
    3. <artifactId>hystrix-javanica</artifactId>
    4. <version>x.y.z</version>
    5. </dependency>
    • 使用@HystrixCommand修饰被监控方法,并提供兜底逻辑:

     
    
    • 在环境中引入 HystrixCommandAspect 注入切面,拦截所有带有HystrixCommand注释的方法,并生成其代理对象。业务调用无需调整,我

    spring-cloud-netflix-hystrix 依赖于 hystrix-javanica ,使能Hystrix开箱即用。自动配置代码见  

    1. @HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = {
    2. @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")})
    3. public String hello() throws InterruptedException {
    4. System.out.println("Hello");
    5. Thread.sleep(3000);
    6. return "Welcome Hystrix";
    7. }
    8. private String fallback_hello() {
    9. return "Request fails. It takes long time to response";
    10. }

    Metrics桥接

    在Hystrix设计支持以plugin的方式桥接metrics到外部系统,该插件要求继承 com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher

    样例地址 

    桥接后的metrics可统一导出到actuator端点,随后被Prometheus拉取到。

  • 相关阅读:
    RIP协议;OSPF协议;BGP协议
    同态加密:以CKKS为例的Bootstrapping操作介绍(不定期更新)
    网络代理技术:保护隐私与增强网络安全
    SpringBoot集成Mybatis——基于SpringBoot和Vue的后台管理系统项目系列博客(五)
    Java操作Redis以及Redis线程池的用法
    Alphago Zero的原理及实现:Mastering the game of Go without human knowledge
    【Unity开发总结】C# 闭包陷阱
    刷题笔记20——各种顺序的二叉树构造
    Win10更新补丁错误代码0x80070643解决方法
    Redis数据类型
  • 原文地址:https://blog.csdn.net/qq_27229113/article/details/125405301