Spring integration 具有多个出站HTTP调用的流
我们有一个购物车领域模型与行项目。在外部,我们有一个单一的API来检查购物车。但在内部,我们有3种不同的HTTP服务:Spring integration 具有多个出站HTTP调用的流,spring-integration,Spring Integration,我们有一个购物车领域模型与行项目。在外部,我们有一个单一的API来检查购物车。但在内部,我们有3种不同的HTTP服务: 创建购物车 添加行项目-每个行项目一个HTTP调用 结帐 我们希望在集成流中表达这一点,如果任何步骤失败或超时,都将中止。下面是流的一些伪代码: @Bean public IntegrationFlow cartFlow() { return IntegrationFlows.from(channel()) .transform(fromJso
@Bean
public IntegrationFlow cartFlow() {
return IntegrationFlows.from(channel())
.transform(fromJson(ShoppingCart.class))
.handle(Http.outboundGateway(.....) // How do we proceed here to create a shopping cart?
.split(ShoppingCart.class, ShoppingCart::getLineItems)
.handle(Http.outboundGateway(.....) // And here to add each line item?
.aggregate()
.handle(Http.outboundGateway(.....) // And here to checkout
.get();
}
添加Http.outboundGateway调用不是问题。问题实际上是关于保存上下文(关于HTTP方法调用后的ShoppingCart)。除了确认呼叫成功之外,服务不会返回任何数据
我理解的一个选项是创建一个定制bean,用于进行HTTP调用并将其注入管道。那种方法我觉得不太习惯
谢谢 您的用例完全适合
内容充实器
,使用它您有一些有效负载
,调用一些外部服务(通过请求频道上的网关
),等待回复并将一些内容添加到原始有效负载
对于您的用例,您可能需要几个相应的.enrich()
定义
有关更多信息,请参见Spring集成内容充实器
编辑
ContentEnricher
示例:
@Bean
public IntegrationFlow enricherFlow() {
return IntegrationFlows.from("enricherInput", true)
.enrich(e -> e.requestChannel("enrichChannel")
.requestPayloadExpression("payload")
.shouldClonePayload(false)
.propertyExpression("name", "payload['name']")
.propertyFunction("date", m -> new Date())
.headerExpression("foo", "payload['name']")
)
.get();
}
谢谢你的建议。你能给我举个例子吗?感谢您的帮助,因为我开始使用spring集成。请在我的答案中找到一个示例。我不太确定我是否理解这个示例。正如您在解释答案时提到的,需要致电外部服务。因此,我希望能够将uri、http方法、响应类型等配置为扩展的一部分。我很可能错过了这个外观,有一个e.requestChannel(“enrichChannel”)
。您应该在该通道上配置一个新的IntegrationFlow
,并执行您的HTTP请求。有关更多信息,请阅读参考手册。感谢耐心等待。我试图避免创建新的流。更具体地说,我试图做一些类似于camel所允许的事情:(参见“使用动态uri”一节)。