Celery 错误:尽管凭证有效并且代理连接成功,但仍无法连接到 Redis 结果后端

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

我的 Celery 设置遇到问题,无法连接到 Redis 结果后端。尽管提供了有效的凭据并成功连接到代理,我还是收到以下错误消息:

[2023-07-14 16:55:22,255: ERROR/MainProcess] consumer: Cannot connect to redis://:**@eu1-brave-turtle-39167.upstash.io:39167//: Connection closed by server.. Trying again in 8.00 seconds... (4/100)

我已经验证用于 Redis 结果后端的所有凭据都是正确的,并且代理连接的建立没有任何问题。但是,与结果后端的连接始终失败。

以下是我的设置的详细信息:

Celery version: 5.2.3
Redis Cloud provider: Upstash Redis Horizontal
Redis URL for the result backend: redis://:<PASSWORD>@<HOST>:<PORT>/0
Redis URL for the broker: redis://:<PASSWORD>@<HOST>:<PORT>
Celery configuration: 

代码:

app = Flask(__name__)
CORS(app, resources={r"*": {"origins": "*"}})
# Enable debug mode
app.debug = True

# Set the Upload Folder
app.config["UPLOAD_FOLDER"] = os.path.join(os.path.dirname(__file__), "Uploads")

# Setup the Celery Config in Flask Application
app.config["UPSTASH_REDIS_URL"] = dotenv.get_key(".env", "UPSTASH_REDIS_URL")


BrokerURL = app.config["UPSTASH_REDIS_URL"]
ResultBackend = app.config["UPSTASH_REDIS_URL"]

print("Broker URL: ", BrokerURL)
print("Result Backend: ", ResultBackend)

# Initialize Celery
celery = Celery(
    app.name,
)

celery.conf.broker_url = BrokerURL
celery.conf.result_backend = f"{ResultBackend}/0"

我已经尝试过以下故障排除步骤:

1. Double-checked the Redis URL and credentials for the result backend.

2.Verified network connectivity and ensured that there are no firewall restrictions blocking the connection.

3. Confirmed the Redis Upstash Server instance is running and accessible.

4.Tested the connection using a Redis client library, which was successful.

5. Enabled logging in Celery to check for any additional error messages.

尽管做出了这些努力,我仍然无法建立与 Redis 结果后端的连接。我可以尝试哪些方法来解决这个问题?

PS:我已经尝试在本地计算机上通过 WSL 使用 Redis,一切似乎都很好,但现在我必须使用生产 Redis 数据库,所以我使用 Upstash,因为它存在于 Heroku 附加组件中。

我已经尝试了上面提到的以下故障排除步骤,并尝试使用不同的Redis Provider“Redis Cloud Entreprise”,但Worker仍然使用命令运行

celery -A app.celery worker -l INFO

但是几秒钟后我收到了上面的错误。

python flask redis celery
1个回答
1
投票

更新:解决方案

经过几个小时的调试和寻找解决方案,问题不是来自 Redis 的 End,甚至不是 Upstash。

我在Celery的文档中发现,当使用TLS/SSL与Redis连接时,协议应更改为其他内容。

这是我之前的环境文件变量:

# Broker
CELERY_BROKER_URL="redis://<USERNAME>:<PASSWORD>@<HOST>:<PORT>"

# Back-end
CELERY_RESULT_BACKEND='redis://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/0'

这就是它的正确工作方式:

# Broker
CELERY_BROKER_URL="rediss://<USERNAME>:<PASSWORD>@<HOST>:<PORT>?ssl_cert_reqs=required"

# Back-end
CELERY_RESULT_BACKEND='rediss://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/0?ssl_cert_reqs=required'

更多信息请访问 Celery 文档

不同之处在于,我发现 Upstash 使用 TLS/SSL 来建立 Redis 连接,因此协议应从“redis”更改为“rediss”,再加上一些额外的 URL 参数来设置它。

© www.soinside.com 2019 - 2024. All rights reserved.