超时如何在 Hystrix 中与 Observables 一起工作?
How do timeouts work in Hystrix with Observables?
我正在使用 Reactive Observables 开发支持 Hystrix 的 Spring Boot-app,并试图弄清楚超时是如何工作的。我假设如果在执行期间发生超时,Hystrix 会立即 return 响应(回退或异常)。现在,鉴于我下面的代码,情况似乎并非如此。相反,对 myService.getString()
的调用会阻塞 5 秒。当它最终 returns 时,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>
感谢您的帮助!
请说明您的 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"
无论如何尝试:
- 使用自己的线程池切换到 THREAD 隔离。
- 以更短的周期睡眠。
将 execution.isolation.strategy
设置为 THREAD
解决了我的问题。
@HystrixCommand(
commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD")
}
)
文档指出 HystrixCommand
默认使用 THREAD
但它与 javanica @HystrixCommand
有点不同,它根据注释方法的 return 类型创建不同的类型。 HystrixCommand
或 HystrixObservableCommand
。 HystrixObservableCommand
将 SEMAPHORE
作为默认隔离策略。
有关详细信息,请参阅 https://github.com/Netflix/Hystrix/issues/1383。
我正在使用 Reactive Observables 开发支持 Hystrix 的 Spring Boot-app,并试图弄清楚超时是如何工作的。我假设如果在执行期间发生超时,Hystrix 会立即 return 响应(回退或异常)。现在,鉴于我下面的代码,情况似乎并非如此。相反,对 myService.getString()
的调用会阻塞 5 秒。当它最终 returns 时,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>
感谢您的帮助!
请说明您的 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"
无论如何尝试:
- 使用自己的线程池切换到 THREAD 隔离。
- 以更短的周期睡眠。
将 execution.isolation.strategy
设置为 THREAD
解决了我的问题。
@HystrixCommand(
commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD")
}
)
文档指出 HystrixCommand
默认使用 THREAD
但它与 javanica @HystrixCommand
有点不同,它根据注释方法的 return 类型创建不同的类型。 HystrixCommand
或 HystrixObservableCommand
。 HystrixObservableCommand
将 SEMAPHORE
作为默认隔离策略。
有关详细信息,请参阅 https://github.com/Netflix/Hystrix/issues/1383。