如何在Heroku上使用Node.js与Web和Worker dynos进行通信? [英] How to communicate Web and Worker dynos with Node.js on Heroku?

查看:184
本文介绍了如何在Heroku上使用Node.js与Web和Worker dynos进行通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Web Dynos 可以处理HTTP请求



Web Dynos 可以处理它们工人Dynos 可以处理作业。



但我不知道如何制作 Web Dynos 工人Dynos 例如,我希望通过 Web Dynos

接收HTTP请求。
能够相互沟通。


,发送给工人Dynos



,处理作业并将结果发送到 Web Dynos



,在Web上显示结果。



Node.js中有这种可能吗? (使用RabbitMQ或Kue等)?



我无法在 Web Dynos 中实施所有代码并缩放 Web Dynos 仅限于

背景作业和排队表明,您的网络动态链接将需要通过中间机制(通常是一个队列)与您的工作人员dynos进行通信。



要完成听起来像你希望遵循的一般方法:


  • Web dyno收到Web请求

  • Web dyno将作业添加到队列中

  • 工作人员dyno从队列中接收作业

  • 工作人员dyno执行作业,向共享组件写入增量进度

  • 浏览器端轮询请求来自web的作业状态dyno

    • Web dyno查询共享组件以获取后台作业的进度并将状态发送回浏览器
    • $

    • 工作人员dyno完成作业的执行并将其标记为共享组件中的完整数据
    • 浏览器端轮询请求状态dyno

      • Web dyno查询共享组件以获取后台作业的进度,并将完成状态发送回浏览器

      / li>


    就实际的实现而言,我并不太熟悉Node.js中最好的库,但是粘贴这个过程的组件一起在Heroku上以加载项提供。



    队列:AMQP是一个支持良好的队列协议和 CloudAMQP 插件可以充当您的网络和工作人员dynos之间的消息队列。



    沙红色状态:您可以使用其中一个 Postgres 加载项来共享正在处理的作业状态或更高性能的内容如 Memcache Redis 。因此,总而言之,您必须使用中间附加组件在Heroku上的dynos之间进行通信。虽然这种方法需要更多的工程,但结果是一个正确解耦和可扩展的架构。


    Web Dynos can handle HTTP Requests

    and while Web Dynos handles them Worker Dynos can handle jobs from it.

    But I don't know how to make Web Dynos and Worker Dynos to communicate each other.

    For example, I want to receive a HTTP request by Web Dynos

    , send it to Worker Dynos

    , process the job and send back result to Web Dynos

    , show results on Web.

    Is this possible in Node.js? (With RabbitMQ or Kue or etc)?

    I could not find an example in Heroku Documentation

    Or Should I implement all codes in Web Dynos and scaling Web Dynos only?

    解决方案

    As the high-level article on background jobs and queuing suggests, your web dynos will need to communicate with your worker dynos via an intermediate mechanism (often a queue).

    To accomplish what it sounds like you're hoping to do follow this general approach:

    • Web request is received by the web dyno
    • Web dyno adds a job to the queue
    • Worker dyno receives job off the queue
    • Worker dyno executes job, writing incremental progress to a shared component
    • Browser-side polling requests status of job from the web dyno
      • Web dyno queries shared component for progress of background job and sends state back to browser
    • Worker dyno completes execution of the job and marks it as complete in shared component
    • Browser-side polling requests status of job from the web dyno
      • Web dyno queries shared component for progress of background job and sends completed state back to browser

    As far as actual implementation goes I'm not too familiar with the best libraries in Node.js, but the components that glue this process together are available on Heroku as add-ons.

    Queue: AMQP is a well-supported queue protocol and the CloudAMQP add-on can serve as the message queue between your web and worker dynos.

    Shared state: You can use one of the Postgres add-ons to share the state of an job being processed or something more performant such as Memcache or Redis.

    So, to summarize, you must use an intermediate add-on component to communicate between dynos on Heroku. While this approach involves a little more engineering, the result is a properly-decoupled and scalable architecture.

    这篇关于如何在Heroku上使用Node.js与Web和Worker dynos进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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