Sum*_*mar 5 java netflix circuit-breaker hystrix
我正在写一个应用程序,我想实现断路器模式.这是我写的Hystrix Command类:
public class HystrixCommandNextGen extends HystrixCommand<ScriptContext> {
private ScriptContext scriptctx;
private ScriptFactory scriptFactory;
private ScriptContext responseContext = null;
private Logger logger = LoggerFactory.getLogger(HystrixCommandNextGen.class);
public HystrixCommandNextGen(ScriptContext scriptctx, ScriptFactory scriptFactory) {
super(
Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Thread_Pool"))
.andCommandKey(HystrixCommandKey.Factory.asKey(scriptctx.getExecutionData(ExecutionParam.SCRIPTNAME)))
);
this.scriptctx = scriptctx;
this.scriptFactory = scriptFactory;
HystrixCommandProperties.Setter().withCircuitBreakerEnabled(true);
HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(150);
}
@Override
protected ScriptContext run() throws Exception {
scriptFactory.execute(scriptctx);
return scriptctx;
}
@Override
protected ScriptContext getFallback() {
logger.error("FI is not responding: Error occurred in the execution of " + getClass().getSimpleName());
return scriptctx;
}
}
Run Code Online (Sandbox Code Playgroud)
我无法理解如何配置线程数,断路器的阈值时间和要处理的请求数.
Hystrix使用Archaius进行配置管理.Archaius库允许在运行时动态重新加载属性值.有关如何配置Archaius的文档,请访问:https://github.com/Netflix/archaius/wiki/Users-Guide
如果要在代码中配置Hystrix,可以使用Archaius ConfigurationManager类,如下所示:
ConfigurationManager.getConfigInstance().setProperty(
"hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds",
500);
Run Code Online (Sandbox Code Playgroud)
请注意,属性名称字符串的HystrixCommandKey部分实际上是使用Setter的.andCommandKey()方法设置的断路器的名称.因此,如果将命令键设置为"MyCommand",则超时的属性键实际上是"hystrix.command.MyCommand.execution.isolation.thread.timeoutInMilliseconds"
完整的配置列表和方法可以在这里找到: https: //github.com/Netflix/Hystrix/wiki/Configuration
对于您的具体问题:
配置编号 线程使用 ' hystrix.threadpool.HystrixThreadPoolKey.coreSize '
断路器使用'hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds'的阈值时间
在尝试设置之前,最好通读配置 wiki 以了解每个属性的结构和用法。
归档时间: |
|
查看次数: |
13494 次 |
最近记录: |