我正在使用無功觀測功能研究啓用Hystrix的Spring Boot-app,並試圖找出超時如何工作。我假設如果在執行期間發生超時,Hystrix會立即返回響應(回退或異常)。現在,這似乎不是我的代碼如下所示。而是撥打myService.getString()
阻止5秒。當它最終返回時,可執行的lambda被執行。超時如何在Hystrix中使用Observable?
我的假設是錯誤的還是下面的其他錯誤?
@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>
感謝您的幫助!
感謝您的幫助。默認是THREAD隔離級別,所以我使用這個。 – DagR
好的...默認似乎是SEMAPHORE,而不是THREAD,如文檔所述:https://github.com/Netflix/Hystrix/wiki/Configuration#execution – DagR