1

我有两个要调用的api。它们都用 Hystrix Observable 包裹:

这是一个例子:

@HystrixCommand()
    public Observable<String> getAvailableFlightBookings() {
        return new ObservableResult<String>() {
            @Override
            public String invoke() {
                URI uri = registryService.getServiceUrl("flight-booking-service", "http://localhost:8081/flight-booking-service");
                String url = uri.toString() + "/flights/list";
                ResponseEntity<String> resultStr = restTemplate.getForEntity(url, String.class);
                return resultStr.getBody();
            }
        };
    }

我有以下代码,我正在努力完成:

public DeferredResult<FlightDetails> getAllFlightDetails() {
        //Calling previous defined functions
        Observable<String> availableFlightBookings=flightBookingIntegrationService.getAvailableFlightBookings();
        Observable<String> couponId=couponIntegrationService.getCoupon();


        Observable<String> details = Observable.zip(

    ..?


    }

我不确定如何调用以下 API:

flightBookingIntegrationService.getAvailableFlightBookings();
couponIntegrationService.getCoupon();

并使用 Observable.zip 填充最终结果对象 (FlightDetails)

航班详情:

public class FlightDetails {

    String couponId;
    String availableFlightList;

..
}

谢谢你,雷。

4

2 回答 2

2

我对 Hystrix 不熟悉,但是压缩两个 observable 应该与纯 RxJava 没有什么不同。

Observable.zip(availableFlightBookings, couponId, new Func2<String, String, FlightDetails>() {
    @Override
    public FlightDetails call(String availableFlights, String coupon) {
        return new FlightDetails(availableFlights, coupon);
    }
}).subscribe();
于 2015-07-13T17:06:15.090 回答
1

首先将您的 Hystrix 命令类设为 a @Component,因为您使用的是 Spring,然后Autowire将其放入您的控制器并调用

使用 lambdas,看起来像这样:

public DeferredResult<FlightDetails> getAllFlightDetails() {

    Observable<String> availableFlightBookings=flightBookingIntegrationService.getAvailableFlightBookings();
    Observable<String> couponId=couponIntegrationService.getCoupon();

    //Create a new DeferredResult
    DeferredResult<FlightDetails> result = new DeferredResult();

    Observable.zip(availableFlightBookings,couponId, (avaliable, coupon) -> {
     // do some logic here or just..
    return new FlightDetails(avaliable,coupon);
    }).subscribe(result::setResult,result::setErrorResult);
    return result;
}
于 2015-07-13T18:07:22.887 回答