@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")
或者如果在我的应用程序上下文中根本不可能验证这一点?
任何输入都是有价值的。
您可以测试Hystrix
断路器配置。
例如,使用Spring Boot1.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);
}
}
应用程序可以使用Spring Cloud Netflix项目提供的Hystrix断路器将这个启动器包含在项目pom.xml:spring-cloud-starter-hystrix中。Hystrix不依赖于Netflix Discovery Client。@EnableHystrix注释应放置在配置类(通常是主类)上。那么方法可以用@HystrixCommand注释来被断路器保护。有关详细信息,请
我在我的spring boot应用程序中使用Hystrix实现断路器,我的代码如下所示: 我看到每次失败时都会调用fallback()。但3次故障后电路不开。在3次失败之后,我原以为它会直接调用并跳过。但这并没有发生。有人能告诉我我在这里做错了什么吗? 谢谢,B Jagan 下面是实际代码。我用这个玩得更远了。当我在RegistrationHystrix.RegisterSeller()方法中直接
Hystrix的主要优点之一是它收集关于每个HystrixCommand的一套指标。Hystrix仪表板以有效的方式显示每个断路器的运行状况。 图3. Hystrix仪表板
Netflix的创造了一个调用的库Hystrix实现了断路器图案。在微服务架构中,通常有多层服务调用。 图1.微服务图 较低级别的服务中的服务故障可能导致用户级联故障。当对特定服务的呼叫达到一定阈值时(Hystrix中的默认值为5秒内的20次故障),电路打开,不进行通话。在错误和开路的情况下,开发人员可以提供后备。 图2. Hystrix回退防止级联故障 开放式电路会停止级联故障,并允许不必要的或
我正在尝试春云和春靴。它使用了Netflix的OSS应用程序,其中有Ribbon和Hystrix。 Ribbon是一个负载均衡器,带有一些功能,其中一个是断路器。