如何设置Django + RabbitMQ +芹菜与Docker?



我试图设置我的Django应用程序有推送通知功能。对于调度通知,我尝试使用芹菜,对于消息代理,我选择了RabbitMQ。我的应用程序在Docker容器中运行,我正在努力让RabbitMQ工作。当运行docker-compose up时,我得到一个错误信息Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.。以下是我的docker-compose.ymlceleryrabbitmq3服务:

celery:
restart: always
build:
context: .
command: celery -A test_celery worker -l info
volumes:
- .:/test_celery
env_file:
- ./.env
depends_on:
- app
- rabbitmq3

rabbitmq3:
container_name: "rabbitmq"
image: rabbitmq:3-management-alpine
ports:
- 5672:5672
- 15672:15672

在我的test_celery-app中,我有一个名为celery.py的文件,其中包含以下内容:

import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_celery.settings')
app = Celery('test_celery')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

最后,在我的settings.py中,我有这个:CELERY_BROKER_URL = 'amqp://localhost'.

我应该定义CELERY_BROKER_URL不同吗?我的docker-compose文件有什么问题吗?我会感谢任何帮助与此,我的设置有什么问题?

应该可以了

CELERY_BROKER_URL=amqp://guest:guest@rabbitmq3:5672/

如果你在rabbit上有一个vhost

CELERY_BROKER_URL=amqp://guest:guest@rabbitmq3:5672/vhost

如果不工作,检查兔子容器的控制台寻找线索

Insettings.py定义:

RABBITMQ = {
"PROTOCOL": "amqp", # in prod change with "amqps"
"HOST": os.getenv("RABBITMQ_HOST", "localhost"),
"PORT": os.getenv("RABBITMQ_PORT", 5672),
"USER": os.getenv("RABBITMQ_USER", "guest"),
"PASSWORD": os.getenv("RABBITMQ_PASSWORD", "guest"),
}
CELERY_BROKER_URL = f"{RABBITMQ['PROTOCOL']}://{RABBITMQ['USER']}:{RABBITMQ['PASSWORD']}@{RABBITMQ['HOST']}:{RABBITMQ['PORT']}"

相关内容

  • 没有找到相关文章

最新更新