简体   繁体   English

Symfony Messenger 不同应用服务器的不同消费者

[英]Symfony Messenger different consumers for different app servers

I have a Symfony application that is running on several servers behind a load balancer.我有一个 Symfony 应用程序,它在负载均衡器后面的多台服务器上运行。 So I have separate hosts www1, www2, www3, etc.所以我有单独的主机 www1、www2、www3 等。

At the moment I'm running messenger:consume only on www1, in fear of race conditions and potentially messages being handled twice.目前我正在运行messenger:consume只在 www1 上运行,担心竞争条件和可能的消息被处理两次。

Now I have a scenario where I need to execute a command on each host.现在我有一个场景,我需要在每个主机上执行一个命令。

I was thinking of using separate transports for each host and running messenger:consume on each, consuming only messages from its respective queue.我正在考虑为每个主机使用单独的传输并在每个主机上运行messenger:consume ,只消耗来自其各自队列的消息。 However I want the configuration to be dynamic, ie I don't want to do another code release with different transports configuration when a new host is added or removed.但是我希望配置是动态的,即我不想在添加或删除新主机时使用不同的传输配置进行另一个代码发布。

Can you suggest a strategy to achieve this?你能提出一个实现这一目标的策略吗?

If you want to use different queues and different consumers... just have different DSNs for each www, stored on environment variables (not code).如果你想使用不同的队列和不同的消费者......只需为每个 www 使用不同的 DSN,存储在环境变量(不是代码)中。 Then you could have different queues or transports for each server.然后您可以为每个服务器使用不同的队列或传输。

The Transport Configuration can include the desired queue name on the DSN, and best practice is to store that on an environment variable, not as code, so you wouldn't need "another code release with different transports config when a new host is added or removed" . 传输配置可以在 DSN 上包含所需的队列名称,最佳实践是将其存储在环境变量中,而不是作为代码存储,因此您不需要“在添加新主机时使用不同传输配置的另一个代码版本或删除” Simply add the appropriate configuration when the host is deployed, same as you do with the rest of the configuration.只需在部署主机时添加适当的配置,就像您对其余配置所做的一样。

But if all the hosts are actually running the same app, generally, you'd use a single consumer, and al the instances should publish to the same queue.但是,如果所有主机实际上都在运行相同的应用程序,通常,您将使用单个使用者,并且所有实例都应发布到同一个队列。

The consumer does not even need to run on the same server than any of the web instances, simply be configured to consume from the appropriate transport/queue.消费者甚至不需要在与任何 Web 实例相同的服务器上运行,只需将其配置为从适当的传输/队列进行消费。

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

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