[英]Hystrix async methods within javanica not running inside spring-boot java application
我正在使用spring-cloud-starter(即具有所有微服務功能的spring boot)。 當我在使用javanica @HystrixCommand注釋的組件中創建hystrix方法時,請按照javanica github網站( https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica )上的說明進行操作該方法異步運行,無論我使用它們的“ Future <>”還是響應式執行“ 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");
}
};
}
}
為什么我要找回EricComponent$1
而不是Eric
? 順便說一句, Eric
只是一個帶有2個字符串的簡單類……它被忽略了。
我想我必須必須顯式執行,但這暗示了我,因為:1)由於文檔要求,不能使用Future <>來執行queue()方法,並且2)確實可以使用Observable <>來實現。這是我得到的一種執行方法。
您的應用程序類上是否有@EnableHystrix
批注?
subscribe
方法是異步的,您正在嘗試使用同步控制器方法填充列表,因此那里可能存在問題。 您可以更改對toBlockingObservable().forEach()
的subscribe
,看看是否有幫助?
更新#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");
}
更新#2在這里查看我的代碼https://github.com/spencergibb/communityanswers/tree/so26372319
更新#3當我注釋掉fallbackMethod
屬性時,它抱怨找不到EricComponent
於AOP的EricComponent
的公共版本。 我將EricComponent
public static
,它起作用了。 自己文件中的頂級類將起作用。 上面鏈接的我的代碼有效(假設restTemplate調用有效)並返回n OK
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.