【问题标题】:Redis Celery broker not reachable when deployed on HerokuRedis Celery 代理在 Heroku 上部署时无法访问
【发布时间】:2014-04-24 10:16:53
【问题描述】:

我想在 Heroku 的生产环境中部署 celery。到目前为止,我一直在开发中,现在我想在生产中部署 celery。它是一个 Django 应用程序。它在本地运行良好。

我目前的设置是:

BROKER_URL = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

但是当我部署它并尝试测试芹菜任务时,服务器(Heroku)日志显示

Connection error: Error 111 connecting localhost:6379. Connection refused.. Trying again in 20.0 seconds...
2014-03-18T15:24:03.823030+00:00 app[web.1]: 15:24:03 celery.1 | [2014-03-18 15:24:03,822: ERROR/MainProcess] consumer: Cannot connect to redis://localhost:6379/0: Error 111 connecting localhost:6379. Connection refused..

显然,链接到localhost:6379BROKER_URL 将无法连接。

在生产中部署时我应该指向哪个 URL?

更新

celery.py

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

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'LlumprsWebsite.settings')

app = Celery('LlumprsWebsite')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)

_初始化_.py

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

settings.py

BROKER_URL = 'redis://127.0.0.1:6379/0'

#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

【问题讨论】:

  • 你试过127.0.0.1 吗?由于您的部署似乎在 heroku 上,因此 localhost 可能没有用作别名。
  • 我几乎没有使用过heroku,但也许你有一个用于生产中部署的实例的特定URL?也许在您项目的 Redis 设置页面中?
  • 我已经用项目中包含的 celery 设置更新了我的问题。请看..
  • 看起来 Heroku 上的 Redis 有一个特定的 URL 调用 REDIS_URL
  • 它要求我验证我的帐户。验证账户后,它要求我添加信用卡详细信息。以为它的基本版是免费的,但我没有信用卡。

标签: python django heroku celery


【解决方案1】:

BROKER_URL = 'redis://127.0.0.1:6379/0' 在 heroku 服务器上不起作用。你需要在heroku中为redis安装这两个插件中的任何一个

两个插件都提供免费套餐。

之后您可以指定您的代理网址

BROKER_URL = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')

所以它可以在本地和heroku服务器上工作。

您可以在

中找到详细的文档

还有一件事。对于heroku,我个人更喜欢redistogo。因为redistogo比rediscloud有更多的功能。

注意:heroku 中提供了更多的 redis 插件。但他们不提供免费套餐。 :)

【讨论】:

  • 这里是 Redis Cloud 倡导者 - 感谢您的提及,但 AFAIK 我们提供更多,包括更改计划而无需停机和无限扩展。你指的是什么功能?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 2012-05-26
  • 2019-11-19
  • 1970-01-01
  • 2019-03-25
  • 2021-05-27
相关资源
最近更新 更多