9

我正在使用 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<>的情况下)或得到一个回调(在响应式执行的情况下......并且 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");
            }
        };
    }
}

为什么我要返回 aEricComponent$1而不是 a Eric?顺便说一句,Eric只是一个带有 2 个字符串的简单类......它被省略了。

我想我必须显式执行,但这暗示了我,因为:1)使用 Future<> 执行它,queue() 方法不可用,因为文档声称和 2)使用 Observable<> 执行它确实没有'我得到的执行它的方法。

4

1 回答 1

6

你的应用程序类上有@EnableHystrix注释吗?

subscribe方法是异步的,您正在尝试在同步控制器方法中填充列表,因此那里可能存在问题。你能改变一下subscribetoBlockingObservable().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");
}

更新 #2 在此处查看我的代码https://github.com/spencergibb/communityanswers/tree/so26372319

更新 #3 当我注释掉该fallbackMethod属性时,它抱怨它找不到EricComponentAOP 的公共版本。我做了EricComponent public static,它奏效了。自己文件中的顶级类可以工作。上面链接的我的代码有效(假设 restTemplate 调用有效)并返回n OK.

于 2014-10-15T00:38:35.940 回答