试驾 Hystrix 断路器配置 [英] Test drive Hystrix Circuit Breaker configuration

查看:52
本文介绍了试驾 Hystrix 断路器配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Our application is written in anit-fragile manner by implementing circuit breaker pattern using Hystrix.

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

The whole of the application is created using test driven practice but is stuck at the point where we need to implement the circuit breaker strategy by configuring the same on the methods.

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

Below is the sample configuration used by us -

@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,因此知道应用程序可用的所有配置),任何人都可以发表评论吗?

Can anyone comment, if there is an available feature or opportunity to test drive it in my Integration Test (which loads the whole of WebApplicationContext, and hence is aware of all configurations available with the application)?

或者如果这根本不可能在我的应用上下文中得到验证?

Or if this is not at all possible to be verified in my app context?

任何输入都是有价值的.

Any inputs will be of value.

推荐答案

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

You can test your Hystrix circuit breaker configuration.

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

For instance, take a look at this example application with 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();
    }
}

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

Your configuration tests may look like this one:

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);
    }
}

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

In addition, if you want to test circuit breaker you can take a look at this test:

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);
    }
}   

这篇关于试驾 Hystrix 断路器配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆