繁体   English   中英

Spring集成流程与常规服务和适配器

[英]Spring integration flow vs regular service and adapter

我有一些带有SOAP服务的旧代码。 现在,我正在为可能调用一个或多个SOAP操作的某些对象构建Rest API。 我一直在研究Spring Integration。 来自文档

除了将细粒度的组件连接在一起之外,Spring Integration还提供了多种通道适配器和网关来与外部系统进行通信。

以上声明听起来很诱人。 我正在编写Rest微服务控制器,Validation服务,REST请求到SOAP请求映射器和SOAP客户端。 在某些情况下,当有多个调用时,我不得不编写更多的代码,并且在很多情况下确实编写了代码。

高层的Spring Integration看起来像是面向异步消息的框架。 我的问题是该呼叫或多或少需要是同步呼叫,而性能至关重要。 有没有人使用Spring integration解决此问题,您能否分享您的经验。

作为对Artem 答案的补充,值得注意的是,如果您要使用Spring Integration DSL之一 (Java,Groovy或Scala),则默认情况下,Spring Integration将选择(同步) DirectChannel您的端点整合流程。 这意味着只要您的端点保持同步并且您依赖它们之间的默认通道,整个集成流程也将保持同步。

例如(在Java DSL中 ):

  @Bean
  public IntegrationFlow syncFlow() {
    return IntegrationFlows
        .from(/* get a REST message from microservice */)
        // here the DirectChannel is used by default 
        .filter(/* validate (and filter out) incorrect messages */)
        // here the DirectChannel is used by default too
        .transform(/* map REST to SOAP */)
        // guess what would be here?
        .handle(/* send a message with SOAP client */)
        .get();
  }

这绝对不意味着您将永远与同步流捆绑在一起。 您可以在任何步骤进行异步或并行处理。 例如,如果决定并行发送SOAP消息,那么您要做的就是在SOAP客户端调用之前指定适当的通道:

  @Bean
  public IntegrationFlow syncFlow() {
        // ... the same as above ...
        .transform(/* map REST to SOAP */)
        .channel(c -> c.executor(Executors.newCachedThreadPool()))  // see (1)
        .handle(/* send a message with SOAP client */)
        .get();
  }

(1)从现在开始,由于使用了ExecutorChannel,将并行处理下游流。

请注意,消息端点还可能根据其逻辑异步运行。

我已经使用Spring Integration在我的家庭和工作项目中构建同步集成流程,并且事实证明这是一个非常强大而灵活的解决方案。

Spring Integration中的头等公民之一是MessageChannel抽象。 最简单,同步且因此直接的方法调用是DirectChannel

不知道是什么让您认为Spring Integration中的所有内容都是异步的。 实际上,除非您告诉自己是异步的,否则它总是直接的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM