kombu.exceptions.OperationalError:[Errno 111]连接被拒绝

问题描述 投票:0回答:1

我正在尝试在容器 Docker 上将 django 与 celery 和rabbitmq 一起使用。它在没有 docker 的情况下运行良好,但是当我在容器上运行时,我收到此错误: kombu.exceptions.OperationalError: [Errno 111] 连接被拒绝。

这是我的 docker-compose:

version: '3.4'

services:
  rabbitmq:
    image: rabbitmq:3.12.0-management
    container_name: 'rabbitmq'
    ports:
      - 5672:5672
      - 15672:15672
    restart: always
  celery:
    build:
      context: .
    command: celery -A loan  worker --loglevel=info
    depends_on:
      - rabbitmq
    restart: always
  loan:
    image: loan
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - 8000:8000
    depends_on:
      - rabbitmq
      - celery

我看到有人说放

from .celery import app as celery_app

__all__ = ('celery_app',)

on init.py,但我已经穿上了。

django docker docker-compose rabbitmq celery
1个回答
0
投票

您没有提供您的

celery.py
文件。确保将这些行添加到您的
celery.py
文件中。

import os
from celery import Celery
from django.conf import settings

# set settings for celery cmd program
os.environ.setdefault('DJANGO-SETTINGS-MODULE', 'projectfolder.settings')

app = Celery('video_streaming_backend', broker=settings.CELERY_BROKER_URL)

# automatically discover tasks given in different apps
app.autodiscover_tasks(settings.INSTALLED_APPS)

app.conf.update(
    result_backend='django-db',
)

不要忘记在

CELERY_BROKER_URL
文件中添加
settings.py

然后在 docker compose 文件中,您需要在

CELERY_BROKER_URL
celery
服务的环境部分添加
loan
,如下所示。

version: '3.4'

services:
  rabbitmq:
    image: rabbitmq:3.12.0-management
    container_name: 'rabbitmq'
    ports:
      - 5672:5672
      - 15672:15672
    restart: always
  celery:
    build:
      context: .
    environment:
      - CELERY_BROKER_URL=amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@${RABBITMQ_HOST}:${RABBITMQ_PORT}
    command: celery -A loan  worker --loglevel=info
    depends_on:
      - rabbitmq
    restart: always
  loan:
    image: loan
    build:
      context: .
      dockerfile: ./Dockerfile
    environment:
      - CELERY_BROKER_URL=amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@${RABBITMQ_HOST}:${RABBITMQ_PORT}
    ports:
      - 8000:8000
    depends_on:
      - rabbitmq
      - celery

最后将这些变量添加到环境变量文件中

RABBITMQ_HOST=host.docker.internal
RABBITMQ_PORT=5673
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
© www.soinside.com 2019 - 2024. All rights reserved.