我目前正试图使用django框架与django-channels来设置一个谷歌应用引擎的Flex,对于我目前的项目,我需要一个websocket,所以我试图重建Django-channels在网站上提供的教程。https:/channels.readthedocs.ioenlatesttutorial。
目前,我卡在添加redis到我的google-app-flex实例上。我按照google的文档设置了一个redis连接--不幸的是这个例子是在Flask中。google doc 我认为我的错误是微不足道的,我只需要将django CHANNEL_LAYERS按比例连接到redis。
执行 sudo gcloud redis instances describe <redisname> --region=us-central1
给我以下回应。
图片:"Redis描述 "Redis Describtion"
执行 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.commemorystoredocsredisconnect-redis-instance-flex。
如何从谷歌的标准应用引擎(Python 3.7)连接到Redis实例(memoryystore)
https:/cloud.google.commemorystoredocsredisconnect-redis-instance-flex。
https:/cloud.google.commemorystoredocsredisquickstart-gcloud。
我的错误是在settings.py中。
正确的版本。
#settings.py
#settings stuff...
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) #this is not needed
# Channels
ASGI_APPLICATION = "django_channels_heroku.routing.application"
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [(redis_host, redis_port)],
},
},
}