简体   繁体   English

在Heroku上部署Redis Celery代理时无法访问

[英]Redis Celery broker not reachable when deployed on Heroku

I want to deploy celery in production on Heroku. 我想在Heroku的生产中部署芹菜。 Until now I was working under development, now I want to deploy the celery in production. 到目前为止,我一直在开发中,现在我想在生产中部署芹菜。 Its a Django app. 它是Django应用。 It works fine locally. 它在本地工作正常。

My current settings are : 我当前的设置是:

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

But when I deploy it, and try to test a celery task, the server (Heroku) logs says 但是当我部署它并尝试测试celery任务时,服务器(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..

Obviously, BROKER_URL being linked to localhost:6379 would not be connected. 显然,链接到localhost:6379 BROKER_URL无法连接。

Which URL should I point to when deploying in production?? 在生产环境中部署时应指向哪个URL?

UPDATED 更新

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',
)

_ init _.py _ 初始化 _.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 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'

BROKER_URL = 'redis://127.0.0.1:6379/0' won't work on the heroku server. BROKER_URL ='redis://127.0.0.1:6379/0'在heroku服务器上不起作用。 You need to install any of the two add-on in heroku for redis 您需要在heroku中安装两个附加组件中的任何一个以进行Redis

both add-ons provide free tier plan. 这两个插件均提供免费的套餐。

after that you can specify your brocker url 之后,您可以指定您的Brocker网址

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

so it will work on both local and heroku server. 因此它可以在本地和heroku服务器上使用。

You can find detailed documentation in 您可以在以下位置找到详细的文档

One more thing. 还有一件事。 I personally prefer redistogo for the heroku. 我个人更喜欢redistogo作为heroku。 Because redistogo has got more features than rediscloud. 因为redistogo具有比rediscloud更多的功能。

Note: There are more redis add-ons available in heroku. 注意:heroku中有更多可用的redis附加组件。 but they don't provide free tier plan. 但他们不提供免费套餐。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM