如何在 Heroku 上与 Node.js 通信 Web 和 Worker dynos? [英] How to communicate Web and Worker dynos with Node.js on Heroku?
问题描述
Web Dynos 可以处理 HTTP 请求
Web Dynos can handle HTTP Requests
而 Web Dynos 处理它们时,Worker Dynos 可以通过它处理作业.
and while Web Dynos handles them Worker Dynos can handle jobs from it.
但我不知道如何让 Web Dynos 和 Worker Dynos 相互通信.
But I don't know how to make Web Dynos and Worker Dynos to communicate each other.
例如,我想通过 Web Dynos
,发送给Worker Dynos
,处理作业并将结果发送回Web Dynos
, process the job and send back result to Web Dynos
,在网络上显示结果.
这在 Node.js 中可行吗?(使用 RabbitMQ 或 Kue 等)?
Is this possible in Node.js? (With RabbitMQ or Kue or etc)?
我在 Heroku 文档
还是我应该在 Web Dynos 中实现所有代码并仅缩放 Web Dynos?
Or Should I implement all codes in Web Dynos and scaling Web Dynos only?
推荐答案
作为关于后台工作的高级文章和排队表明,您的 Web dynos 需要通过中间机制(通常是队列)与您的工作 dynos 进行通信.
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 dyno 接收到 Web 请求
- Web dyno 将作业添加到队列中
- Worker dyno 从队列中接收作业
- Worker dyno 执行作业,将增量进度写入共享组件
- 浏览器端轮询从 Web dyno 请求作业状态
- Web dyno 查询共享组件以获取后台作业的进度并将状态发送回浏览器
- Web dyno 查询共享组件以获取后台作业的进度并将完成状态发送回浏览器
就实际实现而言,我对 Node.js 中最好的库不太熟悉,但是将这个过程粘合在一起的组件在 Heroku 上可用 附加组件.
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.
队列:AMQP 是一个得到良好支持的队列协议,CloudAMQP 附加组件可以作为您的网络之间的消息队列和工人动态.
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.
共享状态:您可以使用 Postgres 附加组件之一来共享正在处理的作业的状态或更高性能的东西,例如 Memcache 或 Redis一>.
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.
所以,总而言之,您必须使用中间附加组件来在 Heroku 上的 dynos 之间进行通信.虽然这种方法涉及更多的工程,但结果是一个适当解耦和可扩展的架构.
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屋!