- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们的应用程序是通过使用 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,因此知道应用程序可用的所有配置),任何人都可以发表评论吗?
或者如果这根本不可能在我的应用上下文中得到验证?
任何输入都是有值(value)的。
最佳答案
您可以测试您的 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);
}
}
关于tdd - 试驾 Hystrix 断路器配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38781398/
断路器工作原理 1、 如果经过断路器的流量超过了一定的阈值,HystrixCommandProperties.circuitBreakerRequestVolumeThreshold(); 举个例
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 3 年前。 Improve this ques
我正在尝试设计一个断路器/开关,它可以在某些事件中关闭和打开。我使用 SVG 设计了断路器,并使用 css 动画和转换属性来为它的关闭设置动画。 使用 transform-origin: bottom
在 Hadoop YARN 集群中使用 Apache Spark 1.6.2。 一些(简单的)查询会消耗大量资源,我看到我们的开发人员针对 1To 文件运行 SELECT * FROM DB!因此,它
我知道没有办法杀死 iframe 断路器......但是,我想知道,有没有办法优雅地处理它。 到目前为止,我在退出之前设法检测到它,使用 .我想知道是否可以阻止它加载iframe或强制它在新窗口中打
我观察到 Hystrix 将所有来自命令的异常视为断路目的的失败。它包括从 command run() 方法抛出并由 Hystrix 本身创建的异常,例如HystrixTimeoutException
断路器(curcuit breaker)模式 在分布式环境下,特别是微服务结构的分布式系统中, 一个软件系统调用另外一个远程系统是非常普遍的。这种远程调用的被调用方可能是另外一个进程,或者是跨网路
有没有办法配置 Micronaut 断路器,使其不针对特定的 HTTP 状态代码打开?例如,如果我查找某个项目但未找到该项目,则服务器可能会返回 HTTP 状态 404。这可能是一个完全有效的响应,我
问题: 自从从 ES-5.4 升级到 ES-7.2 后,当我尝试从我的多线程 Java 应用程序(使用 elasticsearch-rest-high-level-client-7.2.0.jar j
在官方 https://spring.io/guides/gs/circuit-breaker/手册有 业务方法(阅读列表) 回退方法(可靠) @HystrixCommand(fallbackMeth
我想通过重试和 Hystrix 断路器实现 Apache Camel 路由。我的路线如下所示: ...... java.lang.Exception
想问一个关于两种技术的问题。 我们首先从一个必须调用其他第三方 rest API 的应用程序开始,因此,我们在 SpringBoot Webflux 项目中使用了 Webflux WebClient。
我是一名优秀的程序员,十分优秀!