Java Hystrix拒绝完全未来返回类型
我正在尝试更正一个Java Hystrix拒绝完全未来返回类型,java,completable-future,hystrix,Java,Completable Future,Hystrix,我正在尝试更正一个@Hystrix方法,该方法返回一个CompltetableFuture 我在某个地方读到,我可能会尝试更改Fallback方法的返回类型,这样它就不会返回未来,而是返回泛型值。所以我试了一下: public String myMethodFallback(Throwable t){ return ""; } 得到了这个错误: org.springframework.web.util.NestedServletException: Request pr
@Hystrix
方法,该方法返回一个CompltetableFuture
我在某个地方读到,我可能会尝试更改Fallback方法的返回类型,这样它就不会返回未来,而是返回泛型值。所以我试了一下:
public String myMethodFallback(Throwable t){
return "";
}
得到了这个错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: Incompatible return types.
...
Hint: fallback cannot return Future if the fallback isn't command when the command is async.
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: com.netflix.hystrix.HystrixCommand$4 cannot be cast to java.util.concurrent.CompletableFuture
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: com.netflix.hystrix.contrib.javanica.utils.FutureDecorator cannot be cast to java.util.concurrent.CompletableFuture
我试着将@HystrixCommand
添加到回退方法中
@HystrixCommand
public CompletableFuture<String> myMethodFallback(Throwable t){
return CompletableFuture.completedFuture("");
}
未来
下面是一种使用SpringWebreactive和HystrixCommand的替代方法
@Autowired
HystrixImpl hystrixImpl;
public Mono<String> getResponse() {
return HystrixCommands.from(hystrixImpl.myMethod())
.fallback(throwable -> {
return hystrixImpl.myMethodFallback(throwable);
})
.commandName("MyCommand")
.toFlux()
.single();
}
@Autowired
希斯特里克西姆普;希斯特里克西姆普;
公共响应(){
返回HystrixCommands.from(hystrixImpl.myMethod())
.后备(可丢弃->{
返回hystrixImpl.myMethodFallback(可丢弃);
})
.commandName(“MyCommand”)
.toFlux()
.single();
}
HystrixImpl.Java
@Service
public class HystrixImpl {
public Mono<String> myMethod() {
return Mono.just("From main method");
}
public Mono<String> myMethodFallback(Throwable t) {
return Mono.just("From fallback method");
}
}
@服务
公共级HystrixImpl{
公共方法(){
返回Mono.just(“从主方法”);
}
公共方法回退(可丢弃的t){
返回Mono.just(“从回退方法”);
}
}
我们只需要使用getResponse().block()来获取Mono的字符串值。这些是一些有趣的没有帮助的错误消息。
@Service
public class HystrixImpl {
public Mono<String> myMethod() {
return Mono.just("From main method");
}
public Mono<String> myMethodFallback(Throwable t) {
return Mono.just("From fallback method");
}
}