【问题标题】:How do timeouts work in Hystrix with Observables?超时如何在带有 Observables 的 Hystrix 中工作?
【发布时间】:2017-02-05 05:24:26
【问题描述】:

我正在使用 Reactive Observables 开发支持 Hystrix 的 Spring Boot 应用程序,并试图弄清楚超时是如何工作的。我假设如果在执行过程中发生超时,Hystrix 会立即返回响应(回退或异常)。现在,鉴于下面的代码,情况似乎并非如此。相反,对 myService.getString() 的调用会阻塞 5 秒。当它最终返回时,会执行 throwable lambda。

是我的假设不正确还是下面有其他错误?

@SpringBootApplication
@EnableCircuitBreaker
public class App {
  public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run(App.class, args);
  }
}

@RestController
public class MyController {

  @Autowired
  private MyService myService;

  @RequestMapping("/")
  public DeferredResult index() {

    DeferredResult<String> result = new DeferredResult<>();
    Observable<String> res = myService.getString();

    res.subscribe(s -> {
                result.setResult("Got a " + s);
            },
            throwable -> {
                result.setErrorResult("Got a " + throwable.getMessage());
            });

    return result;
  }
}


@Service
public class MyService {

  @HystrixCommand() // Default timeout is 1 sec
  public Observable<String> getString() {
    return Observable.create(subscriber -> {
        if (!subscriber.isUnsubscribed()) {

            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            subscriber.onNext("regular response");
            subscriber.onCompleted();
        }
    });
  }
}

pom.xml: http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

    <groupId>dag</groupId>
    <artifactId>dag</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.5</version>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

感谢您的帮助!

【问题讨论】:

    标签: java spring-boot rx-java hystrix


    【解决方案1】:

    请说明您的 hystrix 命令的隔离是什么以及您使用的 hystrix 版本是什么?

    “超时现在适用于信号隔离命令以及 线程隔离的命令。在 1.4.x 之前,信号量隔离命令 无法超时。他们现在在另一个上注册了超时 (HystrixTimer) 线程,触发超时流。如果你使用 信号量隔离的命令,它们现在会看到超时”

    无论如何尝试:

    1. 使用自己的线程池切换到 THREAD 隔离。
    2. 睡眠周期较短。

    【讨论】:

    【解决方案2】:

    execution.isolation.strategy 设置为THREAD 解决了我的问题。

    @HystrixCommand(
      commandProperties = {
        @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD")
      }
    )
    

    文档指出HystrixCommand 默认使用THREAD,但javanica @HystrixCommand 有点不同,它根据带注释的方法的返回类型创建不同的类型。 HystrixCommandHystrixObservableCommand。而HystrixObservableCommandSEMAPHORE作为默认的隔离策略。

    更多详情请见https://github.com/Netflix/Hystrix/issues/1383

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 2017-10-10
      • 1970-01-01
      • 2019-06-11
      • 2015-10-01
      • 2018-12-21
      • 2023-03-22
      • 2020-06-14
      • 2014-04-10
      相关资源
      最近更新 更多