测试 Hystrix 断路器配置

2023-12-10

我们的应用程序是通过使用 Hystrix 实现断路器模式以抗脆弱的方式编写的。

整个应用程序是使用测试驱动实践创建的,但陷入了我们需要通过在方法上配置相同策略来实现断路器策略的阶段。

以下是我们使用的示例配置 -

@HystrixCommand(commandProperties = {
        @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "8"),
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "25"),
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")},
        fallbackMethod = "retrieveMapFallback")

如果有可用的功能或有机会在我的集成测试中测试它(它加载整个 WebApplicationContext,因此了解应用程序可用的所有配置),任何人都可以发表评论吗?

或者如果这根本不可能在我的应用程序上下文中进行验证?

任何投入都将是有价值的。


您可以测试您的Hystrix断路器配置。

例如,看看这个示例应用程序Spring Boot 1.4:

    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.stereotype.Component;

    @EnableCircuitBreaker
    @SpringBootApplication
    public class HystrixDemo {

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

        @Component
        static class MyService {

            static final String COMMAND_KEY = "MyCommandKey";

            private final Outbound outbound;

            MyService(Outbound outbound) {
                this.outbound = outbound;
            }

            @HystrixCommand(
                    commandKey = COMMAND_KEY,
                    commandProperties = {
                        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2016")
                    })
            void process() {
                outbound.call();
            }
        }

        interface Outbound {
            void call();
        }
    }

您的配置测试可能如下所示:

    import com.netflix.hystrix.HystrixCommandKey;
    import com.netflix.hystrix.HystrixCommandMetrics;
    import com.netflix.hystrix.HystrixCommandProperties;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.test.context.junit4.SpringRunner;

    import static org.junit.Assert.assertTrue;

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class MyServiceCircuitBreakerConfigurationTests {

        @Autowired
        private HystrixDemo.MyService myService;

        @MockBean
        private HystrixDemo.Outbound outbound;

        @Before
        public void setup() {
            warmUpCircuitBreaker();
        }

        @Test
        public void shouldHaveCustomTimeout() {
            assertTrue(getCircuitBreakerCommandProperties().executionTimeoutInMilliseconds().get() == 2016);
        }

        private void warmUpCircuitBreaker() {
            myService.process();
        }

        public static HystrixCommandProperties getCircuitBreakerCommandProperties() {
            return HystrixCommandMetrics.getInstance(getCommandKey()).getProperties();
        }

        private static HystrixCommandKey getCommandKey() {
            return HystrixCommandKey.Factory.asKey(HystrixDemo.MyService.COMMAND_KEY);
        }
    }
    

另外,如果你想测试断路器,可以看看这个测试:

    import com.netflix.config.ConfigurationManager;
    import com.netflix.hystrix.Hystrix;
    import com.netflix.hystrix.HystrixCircuitBreaker;
    import com.netflix.hystrix.HystrixCommandKey;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.test.context.junit4.SpringRunner;

    import static org.junit.Assert.assertFalse;
    import static org.junit.Assert.assertTrue;
    import static org.junit.Assert.fail;
    import static org.mockito.BDDMockito.willThrow;

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class MyServiceCircuitBreakerTests {

        @Autowired
        private HystrixDemo.MyService myService;

        @MockBean
        private HystrixDemo.Outbound outbound;

        @Before
        public void setup() {
            resetHystrix();
            warmUpCircuitBreaker();
            openCircuitBreakerAfterOneFailingRequest();
        }

        @Test
        public void shouldTripCircuit() throws InterruptedException {
            willThrow(new RuntimeException()).given(outbound).call();

            HystrixCircuitBreaker circuitBreaker = getCircuitBreaker();

            // demonstrates circuit is actually closed
            assertFalse(circuitBreaker.isOpen());
            assertTrue(circuitBreaker.allowRequest());

            try {
                myService.process();
                fail("unexpected");
            } catch (RuntimeException exception) {
                waitUntilCircuitBreakerOpens();
                assertTrue(circuitBreaker.isOpen());
                assertFalse(circuitBreaker.allowRequest());
            }
        }

        private void waitUntilCircuitBreakerOpens() throws InterruptedException {
            /* one second is almost sufficient
               borrowed from https://github.com/Netflix/Hystrix/blob/v1.5.5/hystrix-core/src/test/java/com/netflix/hystrix/HystrixCircuitBreakerTest.java#L140
             */
            Thread.sleep(1000);
        }

        private void resetHystrix() {
            Hystrix.reset();
        }

        private void warmUpCircuitBreaker() {
            myService.process();
        }

        public static HystrixCircuitBreaker getCircuitBreaker() {
            return HystrixCircuitBreaker.Factory.getInstance(getCommandKey());
        }

        private static HystrixCommandKey getCommandKey() {
            return HystrixCommandKey.Factory.asKey(HystrixDemo.MyService.COMMAND_KEY);
        }

        private void openCircuitBreakerAfterOneFailingRequest() {
            ConfigurationManager.getConfigInstance().setProperty("hystrix.command." + HystrixDemo.MyService.COMMAND_KEY + ".circuitBreaker.requestVolumeThreshold", 1);
        }
    }   
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测试 Hystrix 断路器配置 的相关文章

随机推荐

  • 如何阻止 Excel 工作簿在自动化打开时闪烁?

    我将 GetObject 与工作簿路径结合使用来创建新实例或获取现有 Excel 实例 如果它正在抓取现有的用户创建的实例 则应用程序窗口是可见的 如果相关工作簿路径关闭 它将打开并隐藏 但不会在屏幕上闪烁之前 Application Sc
  • 在shell脚本中将xml文件转换为csv?

    我正在尝试将 xml 文件转换为 csv 文件 我有一个像这样的输入 xml 文件
  • 通过 VMWare 测试 iOS 设备

    我正在尝试将我的 iPhone 应用程序上传到我的 iPhone 我在 VMWare 虚拟机上运行 Xcode 和 OS X 尽管 iTunes 在插入 iPhone 时可以识别它 这意味着虚拟机和 iPhone 之间的 USB 连接工作正
  • 无法使用 jackson 反序列化器反序列化 java.awt.color

    public class TestJacksonColor public static void main String args throws IOException ObjectMapper objectMapper new Objec
  • 在 TensorFlow 中使用多个 CPU 核心

    我广泛研究了有关 TensorFlow 的其他答案 但我似乎无法让它在我的 CPU 上使用多个内核 根据 htop 的说法 以下程序仅使用单个 CPU 核心 import tensorflow as tf n cpus 20 sess tf
  • 使用编号规则定义 Firestore 文档字段值

    我想将用户信息及其 id 而不是文档 id 存储在文档中 我想生成像 XYZ0001 这样的 id 特定格式 只要有新用户添加 最后 4 位数字就会自动增加 例如 XYZ0001 XYZ0002 之类的 我已经尝试过这个 return fi
  • 更改 Android SeekBar 以在主要进度之上绘制次要进度?

    我想更改 Android 搜索栏的行为 以便辅助 Android 搜索栏实际上绘制在主要 Android 搜索栏的顶部 从此处列出的 ProgressBar 文档中 http developer android com reference
  • 如何在 JAXB 中设置非命名空间 nil 和数据类型属性

    我将 JAXB 与 Scala 结合使用 我的编组代码如下所示 def marshalToXml String val context JAXBContext newInstance this getClass val writer new
  • 在 Dynamic Linq 查询中嵌套 OrderBy 是如何工作的?

    我正在使用 Dynamic Linq 今天我想构建一个稍微复杂的嵌套查询 Composition Where ExpirationDate gt DateTime UtcNow Date ExpirationDate Year 9999 O
  • For 循环不中断 (Python)

    我正在用 Python 编写一个简单的 For 循环 有没有办法在不使用 break 命令的情况下打破循环 我认为通过设置 count 10 将满足退出条件并且循环将停止 但事实似乎并非如此 注意 部分挑战是使用 FOR 循环 而不是 WH
  • 收到错误“无法添加已存在的实体”。将表的值插入数据库时​​无需过程

    我使用 Linq to sql 将记录列表插入到数据库表中 如下所示 my DataContext Class using VTMMedicalDBDataContext objVTMMedicalDBDataContext new VTM
  • Python IOError:Errno 13 权限被拒绝

    好吧 我完全困惑了 我整晚都在研究这个问题 但无法让它发挥作用 我有权查看该文件 我想做的就是阅读该死的东西 每次我尝试都会得到 Traceback most recent call last File
  • Xcode 6 对按钮图像的约束

    我正在创建一个应用程序 其仪表板包含 11 个图像 1 是背景 1 是分隔按钮的线 9 是带有图像的按钮 我尝试了几次自动约束分配 但我也尝试了手动 但我不知道如何正确设置约束 我也遵循了很多教程 如果您观看下图 您会发现按钮 图像尤其是
  • CouchDB + 凭据 + 原始通配符的 CORS 问题

    我正在尝试在 flow ch 上从 Cloudant 迁移到 Jelastic 在本地测试 离子服务 时 我收到 pouchdb 的 CORS 错误 除了我缺少的 CouchDB 之外 Jelastic 中还有其他配置吗 XMLHttpRe
  • numpy 获取许多样本而不按行替换

    我有一个非常大的清单 想象一下它看起来像这样 test llama cow horse fish sheep goat cat dog 我想多次从这个列表中抽样 我希望每个样品都被采集而不需要更换 在这种情况下我想避免 for 循环 我在
  • 使用sql计算之前的时间

    假设我有下表test它有列id time post这是它拥有的数据的样本 id time post 1 2018 06 17 16 12 30 post1 2 2018 06 17 16 13 09 post2 3 2017 06 15 1
  • 使用 gradle build 运行 tomcat jasper 任务 (jspc)

    我试图在 gradle 中使用 jspc 编译我们的 jsp 文件 但出现异常 这是相关的 gradle 部分 tomcatHome is defined in gradle properties ant tomcatHome tomcat
  • Facebook 无法在应用程序 swift 3 中打开页面

    我的网站上有一个指向我的 Facebook 的链接 该链接可在手机浏览器中打开 如果安装了应用程序 我希望它默认在 Facebook 应用程序中打开 我试图输入这段代码 但它不起作用 始终且仅通过 Safari 打开 Facebook 页面
  • 使用 SMTPLib Python 时获取未经授权的发件人地址

    我编写了一个非常简单的 Python 脚本来自动发送电子邮件 这是它的代码 import smtplib From email protected To email protected with smtplib SMTP smtp gmx
  • 测试 Hystrix 断路器配置

    我们的应用程序是通过使用 Hystrix 实现断路器模式以抗脆弱的方式编写的 整个应用程序是使用测试驱动实践创建的 但陷入了我们需要通过在方法上配置相同策略来实现断路器策略的阶段 以下是我们使用的示例配置 HystrixCommand co