OperationalError,错误 111 连接到 127.0.0.1:6379。连接被拒绝。在 heroku 中部署后

OperationalError, Error 111 connecting to 127.0.0.1:6379. Connection refused. After deploying in heroku

我在 heroku 上部署我的网站后出现以下错误。

Error 111 connecting to 127.0.0.1:6379. Connection refused.
Request Method: POST
Request URL:    https://website.herokuapp.com/account/register
Django Version: 3.2.8
Exception Type: OperationalError
Exception Value:    
Error 111 connecting to 127.0.0.1:6379. Connection refused.
Exception Location: /app/.heroku/python/lib/python3.8/site-packages/kombu/connection.py, line 451, in _reraise_as_library_errors
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.8.12
Python Path:    
['/app',
 '/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python38.zip',
 '/app/.heroku/python/lib/python3.8',
 '/app/.heroku/python/lib/python3.8/lib-dynload',
 '/app/.heroku/python/lib/python3.8/site-packages']
Server time:    Sat, 11 Dec 2021 21:17:12 +0530

所以基本上我的网站必须在注册后发送关于 otp 的电子邮件以及一些与合同相关的电子邮件。这些电子邮件是必须发送的,因此无法避免。我早些时候在这里发布了一个问题,关于如何最大限度地减少发送电子邮件所花费的时间,以便用户不必一直等待。我被建议为此使用异步代码。所以我决定为此使用芹菜。我关注了教如何使用它的 youtube 视频。

现在我在网站上推送代码后出现此错误。我是初学者,不知道如何纠正它。请建议我该怎么做。下面是详细信息和配置。

settings.py


CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT =['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SELERLIZER = 'json'

requirements.txt

amqp==5.0.6
asgiref==3.4.1
billiard==3.6.4.0
celery==5.2.1
click==8.0.3
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.2.0
colorama==0.4.4
Deprecated==1.2.13
dj-database-url==0.5.0
Django==3.2.8
django-ckeditor==6.1.0
django-filter==21.1
django-js-asset==1.2.2
django-multiselectfield==0.1.12
dnspython==2.1.0

正如我提到的,我是初学者,请给我一个关于如何纠正这个错误的详细答案。

这是问题所在:

CELERY_BROKER_URL = 'redis://127.0.0.1:6379'

Redis 不会 运行在您的本地 dyno 上运行。您必须在其他地方 运行 它并配置您的代码以连接到它。 A common choice is to run Redis via an addon:

Once you’ve chosen a broker, create your Heroku app and attach the add-on to it. In the examples we’ll use Heroku Redis as the Redis provider but there are plenty of other Redis providers in the Heroku Elements Marketplace.

如果您选择使用 Heroku Redis,您将能够 get the connection string to your instance via the REDIS_URL environment variable:

Heroku add-ons provide your application with environment variables which can be passed to your Celery app. For example:

import os
app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
                CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])

Your Celery app now knows to use your chosen broker and result store for all of the tasks you define in it.

其他插件将提供类似的配置机制。

此处引用的所有文档和大部分链接都来自 Heroku 的 Using Celery on Heroku 文章。我建议您阅读整个文档以获取更多信息。