gpt4 book ai didi

java - 超时如何在 Hystrix 中与 Observables 一起工作?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:27 25 4
gpt4 key购买 nike

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

感谢您的帮助!

最佳答案

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

"Timeouts now apply to semaphore-isolated commands as well as thread-isolated commands. Before 1.4.x, semaphore-isolated commands could not timeout. They now have a timeout registered on another (HystrixTimer) thread, which triggers the timeout flow. If you use semaphore-isolated commands, they will now see timeouts"

无论如何尝试:

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

关于java - 超时如何在 Hystrix 中与 Observables 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39727031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com