具有多个出站 HTTP 调用的流 [英] Flow with multiple outbound HTTP calls

查看:30
本文介绍了具有多个出站 HTTP 调用的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含订单项的购物车域模型.在外部,我们有一个 API 来结帐购物车.但在内部,我们有 3 个不同的 HTTP 服务:

We have a shopping cart domain model with line items. Externally, we have a single API to checkout the shopping cart. Internally though, we have 3 distinct HTTP services:

  1. 创建购物车
  2. 添加订单项 - 每个订单项一次 HTTP 调用
  3. 结帐

我们想在集成流程中表达这一点,如果任何步骤失败或超时,则中止.这是流程的一些伪代码:

We'd like to express this in an integration flow, aborting if any of the steps fail or timeout. Here is some pseudo code for the flow:

 @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).除了确认调用成功外,服务不会返回任何数据.

Adding the Http.outboundGateway calls isn't an issue. The question really is about preserving context (about the ShoppingCart after the HTTP method invocation). The services don't return any data beyond confirming that the call succeeded.

我理解的一个选项是创建一个自定义 bean,它进行 HTTP 调用并将其注入管道.我觉得这种方法不太习惯.

One option I understand is to create a custom bean which makes the HTTP calls and inject that into the pipeline. That approach doesn't feel very idiomatic to me.

谢谢!

推荐答案

您的用例完全适合 Content Enricher pattern,你有一些payload,调用一些外部服务(通过request-channel上的网关code>),等待回复,在原来的payload中添加一些内容.

You use-case fully fit to the Content Enricher pattern, with which you have some payload, call some external service (via gateway on the request-channel), wait for the reply and add some content to the original payload.

对于您的用例,您可能需要几个相应的 .enrich() 定义.

For your use-case you might need several consequential .enrich() definitions.

另见 Spring Integration Content Enricher 定义 了解更多信息.

Also see Spring Integration Content Enricher definition for more information.

编辑

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();
}

这篇关于具有多个出站 HTTP 调用的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆