PHP 中消息队列和工作系统的有效架构?

Valid Architecture for a Message Queue & Worker System in PHP?

我正在努力思考要在 PHP 应用程序中实现的消息队列模型和作业:

问题:

对我的架构有建议或更正吗?

我会推荐一条不同的路线,那就是使用套接字。 ZMQ 是一个已经编写的基于套接字的库的示例。使用套接字,您可以创建 Q 并在消息传入时管理如何处理消息。机器将处于待机模式并在等待消息传入时使用最少的资源。

我的建议基本上可以归结为:保持简单

考虑到这一点,我的第一个建议是放弃 DispatcherWorker。根据我目前的理解,worker 的唯一目的是监听 MAIN 队列并将消息转发到不同的任务队列。您的应用程序应该负责将正确的消息排入正确的队列(或主题)。

回答您的问题:

My workers, would be written in PHP they all have to be polling the cloud queue service? that could get expensive especially when you have a lot of workers.

是的,天下没有免费的午餐。当然,您可以通过 day/week 时间(如果您的用户在特定时间处于活动状态)应用程序使用情况(当更多消息到达时增加轮询率)来调整和优化您的工作人员轮询率,等等。请记住,工程成本可能很快就会高于未优化的轮询。

相反,您可以考虑 推送队列(见下文)。

I was thinking maybe have 1 worker just for polling the queue, and if there are messages, notify the other workers that they have jobs, i just have to keep this 1 worker online using supervisord perhaps? is this polling method better than using a MQ that can notify? How should I poll the MQ, once every second or as fast as it can poll? and then increase the polling workers if I see it slowing down?

这听起来太复杂了。通信是不可靠的,但是有可靠的消息队列。如果您不想丢失数据,请坚持使用消息队列并且不要发明自定义协议。

I was also thinking of having a single queue for all the messages, then the worker monitoring that distributes the messages to other cloud MQs depending on where they need to be processed, since 1 message might need to be processed by 2 diff workers.

如前所述,应用程序应根据需要将您的消息排入多个队列。这使事情变得简单和到位。

Would I still need gearman to manage my workers or can I just use supervisord to spin workers up and down?

消息队列如此之多,使用它们的方法也更多。一般来说,如果您使用 轮询队列,您需要自己让您的工作人员保持活力。但是,如果您使用 推送队列 ,队列服务将调用您指定的端点。因此,您只需要确保您的员工有空。

Basically how could I check the MQ as efficiently and as effectively as possible?

这取决于您的业务要求和您的员工所做的工作。什么时间跨度是关键的?秒、分钟、小时、天?如果您使用 worker 发送电子邮件,则不应花费数小时,最好是几秒钟。每 3 秒或每 15 秒轮询(对用户而言)有区别吗?

正在解决您的问题(使用推送队列):

My goal is to offload messages / data that needs to be sent to multiple third party APIs, so accessing them doesnt slow down the client. So sending the data to a message queue is ideal. I considered using just Gearman to hold the MQ/Jobs, but I wanted to use a Cloud Queue service like SQS or Rackspace Cloud Queues so i wouldnt have to manage the messages.

确实,您描述的场景非常适合消息队列。 正如您提到的,您不想管理消息队列本身,也许您也不想管理工作人员?这是 推送队列 出现的地方。

推送队列基本上呼叫你的工作人员。例如,Amazon ElasticBeanstalk 工作环境在后台执行繁重的工作(轮询),并使用包含队列消息 (refer to the docs for details). I have personally used the AWS push queues and have been happy with how easy they are. Note, that there are other push queue providers like Iron.io.

的 HTTP 请求简单地调用您的应用程序

正如您提到的,您正在使用 PHP,Symfony 有 QPush Bundle,它处理传入的消息请求。您可以查看代码来推出自己的解决方案。