Celery Beat 无法连接到 Rabbitmq 容器

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

我的 celery Beat docker 容器(据我所知)在连接消息代理时出现问题。

我有一个正在运行的 RabbitMQ 容器:

....
rabbitmq:
    image: rabbitmq:3.11.13-management-alpine
    container_name: 'rabbitmq'
    ports:
      - 5672:5672
      - 15672:15672
    env_file:
      - .env
    volumes:
        - ./data:/var/lib/rabbitmq/mnesia
    networks:
      - rabbitmq_go_net
....

还有一个使用 Celery 运行 Django 应用程序的 Python 容器(还有多个容器运行后端本身、celeryworker、celerybeat 和 celeryflower,它们都使用相同的后端文件,但处理不同的任务):

....
celery-beat:
    build:
      context: .
      dockerfile: ./compose/local/django/Dockerfile
    container_name: 'beat'
    image: beat
    command: ["./wait-for-it.sh", "0.0.0.0:1337", "--", "/start-celerybeat"]
    volumes:
      - .:/app
    env_file:
      - .env
    depends_on:
      - rabbitmq
      - backend
    restart: always
....

celerybeat的启动脚本

start.sh
(由/start-celerybeat执行):

#!/bin/bash

set -o errexit
set -o nounset

rm -f './celerybeat.pid'
celery -A app beat -l INFO

应用程序/celery.py:

from __future__ import absolute_import
import os

from celery import Celery
from celery.schedules import crontab

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')

app = Celery('app')

app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()#(lambda: settings.INSTALLED_APPS)

@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    ....

应用程序/设置.py

INSTALLED_APPS = [
    ....
    'django_celery_results',
    'django_celery_beat',
    ....
]

CELERY_BROKER_URL = CONFIG.get('CELERY_BROKER_URL')
CELERY_RESULT_BACKEND = CONFIG.get('CELERY_RESULT_BACKEND')
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"

.env

RABBITMQ_USER=user
RABBITMQ_PASSWORD=password
RABBITMQ_PORT=5672
RABBITMQ_HOST=rabbitmq
CELERY_BROKER_URL=amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@localhost:${RABBITMQ_PORT}/${RABBITMQ_HOST}
CELERY_RESULT_BACKEND=amqp://

启动我的

docker-compose
时收到以下错误:

beat         | [2024-02-20 15:19:54,198: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 6.0 seconds...
beat         | [2024-02-20 15:20:00,212: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 8.0 seconds...
beat         | [2024-02-20 15:20:08,223: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 10.0 seconds...
beat         | [2024-02-20 15:20:18,235: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 12.0 seconds...
beat         | [2024-02-20 15:20:30,248: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 14.0 seconds...
beat         | [2024-02-20 15:20:44,265: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 16.0 seconds...
beat         | [2024-02-20 15:21:00,283: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 18.0 seconds...
beat         | [2024-02-20 15:21:18,307: ERROR/MainProcess] beat: Connection error: [Errno 111] Connection refused. Trying again in 20.0 seconds...

如果您需要更多信息,请随时询问您具体还需要什么。

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

你的

CELERY_BROKER_URL
错了。在 Docker 中,
localhost
表示同一 Docker 容器内的本地环回网络接口。

试试这个:

CELERY_BROKER_URL=amqp://${RABBITMQ_USER}:${RABBITMQ_PASSWORD}@rabbitmq:5672/
© www.soinside.com 2019 - 2024. All rights reserved.