【发布时间】:2020-09-14 03:23:11
【问题描述】:
我目前正在尝试使用带有 django-channels 的 django 框架设置 Google 应用引擎 flex。对于我当前的项目,我需要一个 websocket,所以我正在尝试重建 Django-channels 在网站上提供的教程:https://channels.readthedocs.io/en/latest/tutorial/
目前我一直坚持将 redis 添加到我的 google-app-flex 实例。我按照关于设置 redis 连接的 google 文档进行操作 - 不幸的是,示例在 Flask 中:google doc 我认为我的错误是微不足道的,我只需要将 django CHANNEL_LAYERS 正确连接到 redis。
执行sudo gcloud redis instances describe <redisname> --region=us-central1 会给我以下响应:
执行sudo gcloud app describe,这个响应:
我将我的app.yaml 配置如下:
# app.yaml
# [START runtime]
runtime: python
env: flex
entrypoint: daphne django_channels_heroku.asgi:application --port $PORT --bind 0.0.0.0
runtime_config:
python_version: 3
automatic_scaling:
min_num_instances: 1
max_num_instances: 7
# Update with Redis instance IP and port
env_variables:
REDISHOST: '<the ip in "host" from "Redis Describtion" image above>'
REDISPORT: '6379'
# Update with Redis instance network name
network:
name: default
# [END runtime]
..在我的 settings.py 中,我将其添加为 redis 连接(顺便说一句,感觉真的不对):
#settings.py
import redis
#settings.py stuff...
#connect to redis
redis_host = os.environ.get('REDISHOST', '127.0.0.1')
redis_port = int(os.environ.get('REDISPORT', 6379))
redis_client = redis.StrictRedis(host=redis_host, port=redis_port)
# Channels
ASGI_APPLICATION = "django_channels_heroku.routing.application"
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
我做错了什么。如何正确使用 Django 连接到 Redis?
这里有一些链接:
https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-flex
Django, Redis: Where to put connection-code
Deploying Django channels app on google flex engine
How to connect to Redis instance (memorystore) from Google's Standard App Engine (Python 3.7)
https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-flex
https://cloud.google.com/memorystore/docs/redis/quickstart-gcloud
【问题讨论】:
-
很抱歉在这里提出这个问题,但是您是否能够完全部署并正常工作?我自己在进行 Django/Channels/Redis/Flex 的类似设置时遇到了麻烦,并且永远无法启动实际的应用程序。
-
好详细的问题
-
@Shane 是的,我确实得到了工作......抱歉没有看到你的评论。你解决了吗?
-
不用担心。确实我做到了。我不记得我的问题是什么,但现在一切正常。
标签: django google-app-engine redis django-channels google-app-engine-python