一 命令执行流程
二 断路器开启
-
整个链路达到一定的阀值,默认情况下,10秒内产生超过20次请求,则符合第一个条件。
-
满足第一个条件的情况下,如果请求的错误百分比大于阀值,则会打开断路器,默认为50%。
三 开启断路器
1 代码
package org.crazyit.cloud.open;
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixCommand.Setter;
public class OpenMain {
public static void main(String[] args) {
// 10秒内大于10次请求,满足第一个条件,目的,流量到达一定的值,才可能开启断路器
ConfigurationManager
.getConfigInstance()
.setProperty(
"hystrix.command.default.circuitBreaker.requestVolumeThreshold",
10);
for(int i = 0; i < 15; i++) {
ErrorCommand c = new ErrorCommand();
c.execute();
// 输出断路器状态
System.out.println(c.isCircuitBreakerOpen());
}
}
static class ErrorCommand extends HystrixCommand<String> {
public ErrorCommand() {
super(Setter.withGroupKey(
HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(500)));
}
@Override
protected String run() throws Exception {
Thread.sleep(800);
return "";
}
@Override
protected String getFallback() {
return "fallback";
}
}
}
2 测试结果——第11次才打开断路器
false
false
false
false
false
false
false
false
false
false
true
true
true
true
true
四 关闭断路器
1 代码
package org.crazyit.cloud.open;
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandMetrics.HealthCounts;
import com.netflix.hystrix.HystrixCommandProperties;
public class CloseMain {
public static void main(String[] args) throws Exception {
// 10秒内大于3次请求,满足第一个条件
ConfigurationManager
.getConfigInstance()
.setProperty(
"hystrix.command.default.circuitBreaker.requestVolumeThreshold",
3);
boolean isTimeout = true;
for(int i = 0; i < 10; i++) {
TestCommand c = new TestCommand(isTimeout);
c.execute();
//健康信息
HealthCounts hc = c.getMetrics().getHealthCounts();
System.out.println("断路器状态:" + c.isCircuitBreakerOpen() + ", 请求数量:" + hc.getTotalRequests());
if(c.isCircuitBreakerOpen()) {
isTimeout = false;
System.out.println("============ 断路器打开了,等待休眠期(默认5秒)结束");
Thread.sleep(6000);
}
}
}
static class TestCommand extends HystrixCommand<String> {
//控制命令执行时间
private boolean isTimeout;
//设置命令默认超时时间为500Ms
public TestCommand(boolean isTimeout) {
super(Setter.withGroupKey(
HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(500)));
this.isTimeout = isTimeout;
}
@Override
protected String run() throws Exception {
//超时执行
if(isTimeout) {
Thread.sleep(800);
} else {
Thread.sleep(200);
}
return "";
}
@Override
protected String getFallback() {
return "fallback";
}
}
}
2 测试结果
断路器状态:false, 请求数量:0
断路器状态:false, 请求数量:1
断路器状态:false, 请求数量:2
断路器状态:true, 请求数量:3
============ 断路器打开了,等待休眠期(默认5秒)结束
断路器状态:false, 请求数量:0
断路器状态:false, 请求数量:0
断路器状态:false, 请求数量:0
断路器状态:false, 请求数量:3
断路器状态:false, 请求数量:3
断路器状态:false, 请求数量:5
3 说明
休眠期默认:5秒
然后尝试执行一次,如果成功,将断路器关闭,如果还是失败,断路器还是处于打开状态。