我正在使用Reactive Observables开发支持Hystrix的Spring Boot-app并试图弄清楚超时的工作原理。我假设如果在执行期间发生超时,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>
感谢您的帮助!
答案 0 :(得分:1)
请说明您的hystrix命令的隔离是什么以及您使用的是什么hystrix版本?
“超时现在适用于信号量隔离的命令以及 线程隔离的命令。在1.4.x之前,信号量隔离命令 无法超时。他们现在在另一个上注册了超时 (HystrixTimer)线程,它触发超时流。如果你使用 信号量隔离的命令,他们现在会看到超时“
无论如何都试着:
答案 1 :(得分:0)
将function doGet() {
var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
var fileName = 'Ipsum.txt';
return ContentService.createTextOutput(text).downloadAsFile(fileName);
}
设置为execution.isolation.strategy
解决了我的问题。
THREAD
文档声明@HystrixCommand(
commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD")
}
)
默认使用HystrixCommand
,但javanica THREAD
根据注释方法的返回类型创建不同的类型有点不同。 @HystrixCommand
或HystrixCommand
。并且HystrixObservableCommand
将HystrixObservableCommand
作为默认隔离策略。