Rub*_*sMN 9 java hystrix spring-cloud
我正在使用spring-cloud-starter(即具有所有微服务功能的spring boot).当我在使用javanica @HystrixCommand注释的组件中创建hystrix方法时,请按照javanica github网站(https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica)上的说明进行操作该方法运行async,无论我是使用'Future <>'还是Reactive执行'Observable <>',都没有运行/执行,
java.lang.ClassCastException: springbootdemo.EricComponent$1 cannot be cast to springbootdemo.Eric
每当我尝试拉取结果(在Future <>的情况下)或得到回调(在Reactive Execution的情况下......并且println不会触发,所以它确实没有运行).
public class Application { ...
}
@RestController
@RequestMapping(value = "/makebunchofcalls/{num}")
class EricController { ..
@RequestMapping(method={RequestMethod.POST})
ArrayList<Eric> doCalls(@PathVariable Integer num) throws IOException {
ArrayList<Eric> ale = new ArrayList<Eric>(num);
for (int i =0; i<num; i++) {
rx.Observable<Eric> oe = this.ericComponent.doRestTemplateCallAsync(i);
oe.subscribe(new Action1<Eric>() {
@Override
public void call(Eric e) { // AT RUNTIME, ClassCastException
ale.add(e);
}
});
}
return ale;
}
@Component
class EricComponent { ...
// async version =========== using reactive execution via rx library from netflix ==============
@HystrixCommand(fallbackMethod = "defaultRestTemplateCallAsync", commandKey = "dogeAsync")
public rx.Observable<Eric> doRestTemplateCallAsync(int callNum) {
return new ObservableResult<Eric>() {
@Override
public Eric invoke() { // NEVER CALLED
try {
ResponseEntity<String> result = restTemplate.getForEntity("http://doges/doges/24232/photos", String.class); // actually make a call
System.out.println("*************** call successfull: " + new Integer(callNum).toString() + " *************");
} catch (Exception ex) {
System.out.println("=============== call " + new Integer(callNum).toString() + " not successfull: " + ex.getMessage() + " =============");
}
return new Eric(new Integer(callNum).toString(), "ok");
}
};
}
public rx.Observable<Eric> defaultRestTemplateCallAsync(int callNum) {
return new ObservableResult<Eric>() {
@Override
public Eric invoke() {
System.out.println("!!!!!!!!!!!!! call bombed " + new Integer(callNum).toString() + "!!!!!!!!!!!!!");
return new Eric(new Integer(callNum).toString(), "bomb");
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我要回来EricComponent$1
而不是Eric
?顺便说一句,Eric
只是一个带有2个字符串的简单类...它被省略了.
我认为我必须明确执行,但这包括我,因为:1)使用Future <>使用queue()方法不可用,因为文档声称和2)使用Observable <>执行它真的没有'我得到的一种执行方式.
@EnableHystrix
您的应用程序类上有注释吗?
该subscribe
方法是异步的,您正在尝试使用同步控制器方法填充列表,因此那里可能存在问题。您可以将更subscribe
改为toBlockingObservable().forEach()
,看看是否有帮助?
更新#1
我能够复制。您的默认方法不应返回Observable<Eric>
,而应返回Eric
。
public Eric defaultRestTemplateCallAsync(final int callNum) {
System.out.println("!!!!!!!!!!!!! call bombed " + new Integer(callNum) + "!!!!!!!!!!!!!");
return new Eric(new Integer(callNum).toString(), "bomb");
}
Run Code Online (Sandbox Code Playgroud)
更新#2 在这里查看我的代码https://github.com/spencergibb/communityanswers/tree/so26372319
更新#3
当我注释掉该fallbackMethod
属性时,它抱怨找不到EricComponent
AOP 的公共版本。我做了EricComponent
public static
,它奏效了。自己文件中的顶级类将起作用。上面链接的我的代码有效(假设restTemplate调用有效)并返回n OK
。