具有相同 RabbitMQ 代理后端进程的多个 Celery 项目

2022-01-11 00:00:00 python celery rabbitmq

问题描述

如何使用两个不同的 celery 项目,它们使用来自单个 RabbitMQ 安装的消息.

通常,如果我为它们使用不同的 rabbitmq,这些脚本可以正常工作.但是在生产机器上,我需要为他们共享相同的 RabbitMQ 后端.

注意:由于某些限制,我无法合并现有项目中的新项目,因此它将是两个不同的项目.

解决方案

RabbitMQ 具有创建虚拟消息代理的能力,称为 virtual主机或虚拟主机.每一个本质上都是一个带有自己队列的 mini-RabbitMQ 服务器.这让您可以安全地将一个 RabbitMQ 服务器用于多个应用程序.

rabbitmqctl add_vhost 命令创建一个虚拟主机.

默认情况下,Celery 使用 / 默认虚拟主机:

<块引用>

celery worker --broker=amqp://guest@localhost//

但您可以使用任何自定义虚拟主机:

<块引用>

celery worker --broker=amqp://guest@localhost/myvhost

示例:

rabbitmqctl add_vhost new_hostrabbitmqctl add_vhost/another_host

<块引用>

celery worker --broker=amqp://guest@localhost/new_host

celery worker --broker=amqp://guest@localhost//another_host

How can I use two different celery project which consumes messages from single RabbitMQ installation.

Generally, these scripts work fine if I use different rabbitmq for them. But on production machine, I need to share the same RabbitMQ backend for them.

Note: Due to some constraint, I cannot merge new projects in existing, so it will be two different project.

解决方案

RabbitMQ has the ability to create virtual message brokers called virtual hosts or vhosts. Each one is essentially a mini-RabbitMQ server with its own queues. This lets you safely use one RabbitMQ server for multiple applications.

rabbitmqctl add_vhost command creates a vhost.

By default Celery uses the / default vhost:

celery worker --broker=amqp://guest@localhost//

But you can use any custom vhost:

celery worker --broker=amqp://guest@localhost/myvhost

Examples:

rabbitmqctl add_vhost new_host
rabbitmqctl add_vhost /another_host

celery worker --broker=amqp://guest@localhost/new_host

celery worker --broker=amqp://guest@localhost//another_host

相关文章