【问题标题】:Overriding Hystrix command properties specified in .properties file覆盖 .properties 文件中指定的 Hystrix 命令属性
【发布时间】:2023-03-24 16:50:02
【问题描述】:

我需要覆盖我的 application.properties 文件中指定的命令超时属性。这是我尝试过的

    @Test
    public void testTokenQueryTimeout() throws Exception
    {
      String propertyToSet ="hystrix.command.quickbaseTokenQueryCommand.execution.isolation.thread.timeoutInMilliseconds";
      String prop="";
      try {
        prop = ConfigurationManager.getConfigInstance().getProperty(
            propertyToSet).toString();
        logger.info("\n\n\noriginal quickbaseTokenQueryCommand timeout ="+prop);

        System.setProperty(
            propertyToSet,"10");

        prop = ConfigurationManager.getConfigInstance().getProperty(
            propertyToSet).toString();
        logger.info("\n\n\nupdated quickbaseTokenQueryCommand timeout ="+prop);

        String response = accountValidation.isValidToken(token);
        logger.info(response);
        Assert.assertFalse(true);
      }
      catch (AccountValidationServiceException e)
      {
        Assert.assertTrue(Constants.ERRCODE_TOKEN_QUERY_TIMED_OUT.equals(e.getErrorCode()));
      }
      finally {
        ConfigurationManager.getConfigInstance().clearProperty(propertyToSet);
        System.clearProperty(propertyToSet);
        if(!GeneralUtil.isObjectEmpty(System.getProperty(
            propertyToSet)))prop = System.getProperty(
            propertyToSet);

        logger.info("Updated testTokenQueryTimeout timeout ="+prop);
      }
    }

注意,System.setProperty(propertyToSet,"10")。使用这种方法,这个测试用例通过了,即属性被更改并且命令超时,但是另一个测试用例由于这个命令超时而失败,尽管我正在从系统中清除属性。

我还尝试使用 ConfigurationManager.getConfigInstance().setProperty( propertyToSet).toString(),"10");但是在那种情况下,这个属性的改变是没有效果的,命令也不会超时。

这里有什么我遗漏的吗?

请帮忙。

【问题讨论】:

    标签: hystrix


    【解决方案1】:

    尝试使用 ConcurrentCompositeConfiguration 类

    application.properties

    hystrix.command.HelloWorldCommand.execution.isolation.thread.timeoutInMilliseconds=200
    

    命令

    public class HelloWorldCommand extends HystrixCommand<String> {
    
      public HelloWorldCommand() {
        super(HystrixCommandGroupKey.Factory.asKey("HelloWorldGroup"));
      }
    
      @Override
      protected String run() throws Exception {
        TimeUnit.MILLISECONDS.sleep(1100);
        return "Hello";
      }
    }
    

    测试

    public class HelloWorldCommandTest {
    
      @Test
      public void commandConfigTest() {
        String propertyKey = "hystrix.command.HelloWorldCommand.execution.isolation.thread.timeoutInMilliseconds";
        ConcurrentCompositeConfiguration config = (ConcurrentCompositeConfiguration) ConfigurationManager.getConfigInstance();
    
        Integer originalTimeout = (Integer) config.getProperty(propertyKey);
        config.setOverrideProperty(propertyKey, 1200);
        String result = new HelloWorldCommand().execute();
        assertThat(result, is("Hello"));
    
        config.setOverrideProperty(propertyKey, originalTimeout);
        Integer timeoutValue = (Integer) config.getProperty(propertyKey);
        assertThat(timeoutValue, is(originalTimeout));
      }
    }
    

    【讨论】:

    • 感谢您的回复!我会试试看。清理步骤到底应该是什么?我正在做 System.clearProperty(propertyToSet); conf.clearProperty(propertyToSet); Hystrix.reset(); HystrixPlugins.reset();但我仍然看到随机命令超时,即使我的 .properties 文件中的超时时间非常高(100000)。我不确定它从哪里选择默认值。我检查了加载的系统属性和配置属性,它们似乎是正确的。
    • 是否要将超时值设置回原始值?如果是这样,只需将原始超时值存储在一个变量中,并在准备好时将其设置回来。我已经更新了答案来证明这一点
    • 整数 originalTimeout = (Integer) config.getProperty(propertyKey);将其取出并将其设置为整数是否重要?我将其设置为字符串。
    • 我试过 originalValue = (Integer)conf.getProperty(propertyToSet);我收到 ClassCastException。 java.lang.String 不能转换为 java.lang.Integer 但是,在我的属性中,我正在设置命令属性,就像您在示例中所做的那样。 hystrix.command.testcommand.execution.isolation.thread.timeoutInMilliseconds=50000
    • 没关系。我不在电脑前查看
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 2013-03-06
    • 2013-08-16
    相关资源
    最近更新 更多