当前位置: 首页 > 知识库问答 >
问题:

Java中断路器的Hystrix配置

龙宣
2023-03-14
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;
    }
}

共有1个答案

尹乐邦
2023-03-14

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

请注意,属性名称字符串的HystrixCommandKey部分实际上是使用setter的.andCommandKey()方法设置的断路器的名称。因此,如果将命令键设置为“mycommand”,超时的属性键实际上是“hystrix.command.mycommand.execution.isolation.thread.timeoutinmilliseconds”

 类似资料:
  • 或者如果在我的应用程序上下文中根本不可能验证这一点? 任何输入都是有价值的。

  • 应用程序可以使用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()方法中直接

  • 我正在尝试春云和春靴。它使用了Netflix的OSS应用程序,其中有Ribbon和Hystrix。 Ribbon是一个负载均衡器,带有一些功能,其中一个是断路器。

  • Hystrix的主要优点之一是它收集关于每个HystrixCommand的一套指标。Hystrix仪表板以有效的方式显示每个断路器的运行状况。 图3. Hystrix仪表板

  • Netflix的创造了一个调用的库Hystrix实现了断路器图案。在微服务架构中,通常有多层服务调用。 图1.微服务图 较低级别的服务中的服务故障可能导致用户级联故障。当对特定服务的呼叫达到一定阈值时(Hystrix中的默认值为5秒内的20次故障),电路打开,不进行通话。在错误和开路的情况下,开发人员可以提供后备。 图2. Hystrix回退防止级联故障 开放式电路会停止级联故障,并允许不必要的或