在elastic-beanstalk上部署Django通道的问题

suzh9iv8  于 2023-10-21  发布在  Go
关注(0)|答案(2)|浏览(152)

我正在Elastic Beanstalk上部署我的django应用,使用gunicorn为我的wsgi和daphne处理asgi。我成功部署了我的应用程序,但websockets无法正常运行。
通过在我的弹性beanstalk环境中运行下面的代码,我能够测试EC2示例连接到redis缓存:

eb ssh

source /var/app/venv/*/bin/activate
cd /var/app/current/
python manage.py shell

>>> import channels.layers
>>> from asgiref.sync import async_to_sync
>>> channel_layer = channels.layers.get_channel_layer()
>>> async_to_sync(channel_layer.send)('test_channel', {'foo': 'bar'})
>>> async_to_sync(channel_layer.receive)('test_channel')
>>> {'foo': 'bar'} # <---------- I was able to receive a response

但是,我无法在我的EC2示例上手动运行initiate the daphne server:daphne -b 0.0.0.0 -p 5000 tattoovalley.asgi:application上面的代码生成了以下错误:ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
然而,我已经在我的.ebextensions配置文件中配置了DJANGO_SETTINGS_MONTHEAD环境模块:.ebextensions/01_env.config

option_settings:  
    aws:elasticbeanstalk:environment:proxy:staticfiles:
        /static: static
        value: static/

    aws:elasticbeanstalk:container:python:     
        WSGIPath: tattoovalley/wsgi.py
    aws:elasticbeanstalk:application:environment:
        DJANGO_SETTINGS_MODULE: tattoovalley.settings
        PYTHONPATH: /opt/python/current/app/tattoovalley:$PYTHONPATH

    aws:elbv2:listener:80:
        DefaultProcess: http
        ListenerEnabled: 'true'
        Protocol: HTTP
        Rules: ws
    aws:elbv2:listenerrule:ws:
        PathPatterns: /ws/*
        Process: websocket
        Priority: 1
    aws:elasticbeanstalk:environment:process:http:
        Port: '80'
        Protocol: HTTP
    aws:elasticbeanstalk:environment:process:websocket:
        Port: '5000'
        Protocol: HTTP

enter image description here
我还检查了端口5000是否处于活动状态,没有发现任何活动。
会有什么问题呢?有没有解决方案可以用来修复WebSocket连接

更新

下面是我的asgi.py文件:

import os
import django
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

from chat.routing import websocket_urlpatterns

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tattoovalley.settings")
django.setup()
django_asgi_app = get_asgi_application()

import chat.routing

application = ProtocolTypeRouter(
    {
        "http": django_asgi_app,
        "websocket": AllowedHostsOriginValidator(
            AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
        ),
    }
)
zphenhs4

zphenhs41#

看看你的asgi.py文件,我想你可以做一些修改:

import os
from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tattoovalley.settings")

django_asgi_app = get_asgi_application()

# Now you can safely import other Django-related modules
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator

from chat.routing import websocket_urlpatterns

import chat.routing    <---- NOT SURE IF YOU STILL NEED THIS

application = ProtocolTypeRouter(
    {
        "http": django_asgi_app,
        "websocket": AllowedHostsOriginValidator(
            AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
        ),
    }
)

我希望这些变化能让一切正常。它基本上归结为这样一个事实,即你试图导入第三方Django包/库,这些包/库还没有在你的设置文件中示例化。这需要在您可以导入它们之前首先进行。
据我所知,import djangodjango.setup()已经过时了。

bfhwhh0e

bfhwhh0e2#

这可能与您的asgi.py文件有关。如果你能发出去,我可以看看。

相关问题