【问题标题】:Best practice of multiple steps flow多步骤流程的最佳实践
【发布时间】:2016-12-31 16:26:51
【问题描述】:

我有一项服务可以将 POST 发送到“/”。此服务将消息推送到处理消息的 RABBITMQ 队列。我想在完成后返回指示,然后调用另一个执行另一个处理的函数。 场景:

(http)->server1->RABBITMQ QUEUE->RABBITMQ CONSUMER->返回server1->调用函数A

服务器是NODE JS

谢谢:)

【问题讨论】:

    标签: node.js queue rabbitmq


    【解决方案1】:

    Promise 链在您的情况下可能很有用。它会一一执行所有异步步骤,如果其中任何一个步骤失败,则会捕获错误。

    const doSomethingStepByStep = () => {
      return Promise.resolve()
    .then(() => {
      // call server, or whatever - here just an example that promise is resolved
      return Promise.resolve()
      })
    .then(() => {
      // etc, call another server
      })
    .catch(ex => {
        // something went wrong in either in the steps above
      })
    }
    
    // use it
    doSomethingStepByStep()
      .then((res) => {
    // do something with the result
    })
    

    【讨论】:

      【解决方案2】:

      查看消息http://www.enterpriseintegrationpatterns.com/patterns/messaging/ProcessManager.html的“进程管理器”模式

      以及“saga”(https://docs.particular.net/nservicebus/sagas/ 和 https://lostechies.com/jimmybogard/2013/05/14/saga-patterns-wrap-up/)或“工作流管理器”模式,它们基本上是相同的,但名称不同。

      它的要点是你有一段核心代码可以理解整个流程的流程。它处理在正确的时间发送正确的消息、接收响应、处理该响应以确定接下来会发生什么,并进行下一次调用,以便流程中的下一步可以发生。

      我有一篇博客文章涵盖了这个核心思想,从 Node 中的简单 JS 对象转移到带有这些概念的 RabbitMQ https://derickbailey.com/2015/08/10/managing-workflow-in-long-running-javascript-processes/

      【讨论】:

        猜你喜欢
        • 2011-07-27
        • 1970-01-01
        • 2011-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多