【发布时间】:2016-07-22 22:11:03
【问题描述】:
这是shopping cart sample 的延续,我们有一个外部 API 允许从购物车中结帐。回顾一下,我们有一个流程,我们创建一个空购物,添加订单项,最后结帐。上述所有操作都是通过对外部服务的 HTTP 调用进行的扩充。我们想同时添加订单项(作为添加订单项的一部分)调用。我们当前的配置如下所示:
@Bean
public IntegrationFlow fullCheckoutFlow() {
return f -> f.channel("inputChannel")
.transform(fromJson(ShoppingCart.class))
.enrich(e -> e.requestChannel(SHOPPING_CART_CHANNEL))
.split(ShoppingCart.class, ShoppingCart::getLineItems)
.enrich(e -> e.requestChannel(ADD_LINE_ITEM_CHANNEL))
.aggregate(aggregator -> aggregator
.outputProcessor(g -> g.getMessages()
.stream()
.map(m -> (LineItem) m.getPayload())
.map(LineItem::getName)
.collect(joining(", "))))
.enrich(e -> e.requestChannel(CHECKOUT_CHANNEL))
.<String>handle((p, h) -> Message.called("We have " + p + " line items!!"));
}
@Bean
public IntegrationFlow addLineItem(Executor executor) {
return f -> f.channel(MessageChannels.executor(ADD_LINE_ITEM_CHANNEL, executor).get())
.handle(outboundGateway("http://localhost:8080/api/add-line-item", restTemplate())
.httpMethod(POST)
.expectedResponseType(String.class));
}
@Bean
public Executor executor(Tracer tracer, TraceKeys traceKeys, SpanNamer spanNamer) {
return new TraceableExecutorService(newFixedThreadPool(10), tracer, traceKeys, spanNamer);
}
为了并行添加订单项,我们使用了一个执行器通道。但是,当在 zipkin 中看到它们时,它们似乎仍然是按顺序处理的:
我们做错了什么?整个项目的源码在github,供参考。
谢谢!
【问题讨论】: