【问题标题】:celery: could not connect to rabbitmq芹菜:无法连接到rabbitmq
【发布时间】:2017-06-23 08:03:42
【问题描述】:

使用 rabbitmq 作为 celery 的代理。运行命令时出现问题

celery -A proj worker   --loglevel=info

celery 控制台显示这个

[2017-06-23 07:57:09,261: ERROR/MainProcess] consumer: Cannot connect to amqp://bruce:**@127.0.0.1:5672//: timed out.
Trying again in 2.00 seconds...

[2017-06-23 07:57:15,285: ERROR/MainProcess] consumer: Cannot connect to amqp://bruce:**@127.0.0.1:5672//: timed out.
Trying again in 4.00 seconds...

以下是来自rabbitmq的日志

=ERROR REPORT==== 23-Jun-2017::13:28:58 ===
closing AMQP connection <0.18756.0> (127.0.0.1:58424 -> 127.0.0.1:5672):
{handshake_timeout,frame_header}

=INFO REPORT==== 23-Jun-2017::13:29:04 ===
accepting AMQP connection <0.18897.0> (127.0.0.1:58425 -> 127.0.0.1:5672)

=ERROR REPORT==== 23-Jun-2017::13:29:14 ===
closing AMQP connection <0.18897.0> (127.0.0.1:58425 -> 127.0.0.1:5672):
{handshake_timeout,frame_header}

=INFO REPORT==== 23-Jun-2017::13:29:22 ===
accepting AMQP connection <0.19054.0> (127.0.0.1:58426 -> 127.0.0.1:5672)

我们将不胜感激。

【问题讨论】:

  • 检查rabbitmqctl status是否显示节点正在运行
  • 检查您的RabbitMq 服务器是否正在运行。如果没有,您可以使用:rabbitmq-server 运行它。

标签: django rabbitmq celery


【解决方案1】:

我知道很晚了

但我今天遇到了同样的问题,花了将近一个小时才找到确切的解决方法。认为它可能会帮助其他人

我使用的是 celery 4.1.0 版

希望你已经正确配置了RabbitMQ,如果没有请按照页面http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html#setting-up-rabbitmq中的说明进行配置

还要交叉检查代理 url 是否正确。这是代理 url 语法 amqp://user_name:密码@localhost/host_name

您可能不需要指定端口号,因为它会自动选择默认端口号

如果您遵循以上设置教程链接中的相同变量,您的 Brocker 网址将会像 amqp://myuser:mypassword@localhost/myvhost

遵循这个项目结构

Project
  ../app
  ../Project
     ../settings.py
     ../celery.py
     ../tasks.py
     ../celery_config.py

celery_config.py

# - - - - - - - - - -
# BROKER SETTINGS
# - - - - - - - - - -
# BROKER_URL = os.environ['APP_BROKER_URL']
BROKER_HEARTBEAT = 10
BROKER_HEARTBEAT_CHECKRATE = 2.0

# Setting BROKER_POOL_LIMIT to None disables pooling
# Disabling pooling causes open/close connections for every task.
# However, the rabbitMQ cluster being behind an Elastic Load Balancer,
# the pooling is not working correctly,
# and the connection is lost at some point.
# There seems no other way around it for the time being.
BROKER_POOL_LIMIT = None

BROKER_TRANSPORT_OPTIONS = {'confirm_publish': True}

BROKER_CONNECTION_TIMEOUT = 20
BROKER_CONNECTION_RETRY = True
BROKER_CONNECTION_MAX_RETRIES = 100

celery.py

from __future__ import absolute_import, unicode_literals
from celery import Celery

from Project import celery_config

app = Celery('Project',
            broker='amqp://myuser:mypassword@localhost/myvhost',
            backend='amqp://',
            include=['Project'])

# Optional configuration, see the application user guide.
# app.conf.update(
#     result_expires=3600,
#     CELERY_BROKER_POOL_LIMIT = None,
# )

app.config_from_object(celery_config)

if __name__ == '__main__':
    app.start()

tasks.py

 from __future__ import absolute_import, unicode_literals
 from .celery import app


 @app.task
 def add(x, y):
      return x + y

然后在项目目录中使用“celery -A Project worker -l info”启动芹菜

一切都会好起来的。

【讨论】:

    【解决方案2】:

    在 settings.py 中设置 CELERY_BROKER_POOL_LIMIT = None

    【讨论】:

      【解决方案3】:

      此解决方案适用于 GCP 用户。

      我一直在研究 GCP,遇到了同样的问题。

      错误信息是:

      [2022-03-15 16:56:00,318: ERROR/MainProcess] 消费者:无法连接 到 amqp://root:**@34.125.161.132:5672/vhost: 超时。

      我花了将近一个小时来解决这个问题,终于找到了解决方案

      我们必须在Firewall规则中添加端口号5672

      步骤:

      1. 转到防火墙
      2. 选择 default-allow-http 规则
      3. 按编辑
      4. 搜索“指定的协议和端口”
      5. 在 tcp 框中添加 5672(例如,如果要添加更多端口:80,5672,8000)

      保存更改,然后就可以了!

      【讨论】:

        猜你喜欢
        • 2014-11-06
        • 1970-01-01
        • 2019-04-23
        • 1970-01-01
        • 2014-11-11
        • 2013-11-17
        • 1970-01-01
        • 1970-01-01
        • 2020-06-29
        相关资源
        最近更新 更多