1

Djangoチャンネルを使用するDjangoでアプリケーションを構築しようとしています。私はElastic Beanstalkに展開しています。私の理解では、アプリケーションロードバランサはWebソケットをサポートしており、websocketトラフィックを適切なポートにルーティングできます。 WebSocketは私のlocalhost:8000で動作します。私は自分のチャンネル層にフリーティアのRedis Labsを使っています。EC2でDjangoチャンネルを使用したWebsocketエラー

私はこのチュートリアルに従いました。 https://blog.mangoforbreakfast.com/2017/02/13/django-channels-on-aws-elastic-beanstalk-using-an-alb/#comment-43

このチュートリアルでは、Daphneと作業者が正しいポートで動作しているようです。私は次の出力を得るためにEC2インスタンスにSSHしました。

$ sudo /usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf status 
Daphne       RUNNING pid 4240, uptime 0:05:51 
Worker:Worker_00     RUNNING pid 4242, uptime 0:05:51 
Worker:Worker_01     RUNNING pid 4243, uptime 0:05:51 
Worker:Worker_02     RUNNING pid 4244, uptime 0:05:51 
Worker:Worker_03     RUNNING pid 4245, uptime 0:05:51 
httpd       RUNNING pid 4248, uptime 0:05:51 

私のdaphne.out.logとworkers.out.logはうまく見えます。

$ cat daphne.out.log 
2017-07-20 21:41:43,693 INFO  Starting server at tcp:port=5000:interface=0.0.0.0, channel layer mysite.asgi:channel_layer. 
2017-07-20 21:41:43,693 INFO  HTTP/2 support not enabled (install the http2 and tls Twisted extras) 
2017-07-20 21:41:43,694 INFO  Using busy-loop synchronous mode on channel layer 
2017-07-20 21:41:43,694 INFO  Listening on endpoint tcp:port=5000:interface=0.0.0.0 

$ cat workers.out.log 
2017-07-20 21:41:44,114 - INFO - runworker - Using single-threaded worker. 
2017-07-20 21:41:44,120 - INFO - runworker - Using single-threaded worker. 
2017-07-20 21:41:44,121 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer) 
2017-07-20 21:41:44,121 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 
2017-07-20 21:41:44,126 - INFO - runworker - Using single-threaded worker. 
2017-07-20 21:41:44,126 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer) 
2017-07-20 21:41:44,126 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer) 
2017-07-20 21:41:44,127 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 
2017-07-20 21:41:44,127 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 
2017-07-20 21:41:44,133 - INFO - runworker - Using single-threaded worker. 
2017-07-20 21:41:44,136 - INFO - runworker - Running worker against channel layer default (asgi_redis.core.RedisChannelLayer) 
2017-07-20 21:41:44,136 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 

ここから私は、セキュリティグループの設定を更新し、ロードバランサを設定し、自分のWebページをテストしました。再ルーティングのためのパスとして/ ws /の代わりに、/ table/*を使用しました。これはWebソケットが必要なページであるためです。

私が/ ws /を使用すると、このエラーが発生します。

'WS://example.com/table/1/' へのWebSocket接続に失敗しました:のWebSocketハンドシェイク中にエラーが発生しました:予期しない応答コード:404

私は/テーブルを使用する場合は/ * Iが得ますこのエラー。

'WS://example.com/table/1/' へのWebSocket接続に失敗しました:のWebSocketハンドシェイク中にエラーが発生しました:予期しない応答コード:504

を、私は私のセキュリティグループの設定を変更した場合ポート5000でTCPを許可するようにロードバランサを設定すると、ワーカーログが変更されます。私のロードバランサで/ WS /パスの規則では、私は今、労働者のログにこれを取得する:

... 
2017-07-20 21:41:44,136 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 
Not Found: /ws/ 
Not Found: /ws/ 
Not Found: /ws/ 
Not Found: /ws/ 
Not Found: /ws/ 
Not Found: /ws/ 

私は/テーブル/ *パスを使用している場合は、私は私のログに非常に長いエラーが発生します。私はWebSocketをを使用しようとすると

2017-07-21 01:22:05,270 - ERROR - worker - Error processing message with consumer deal.consumers.ws_connect: 
Traceback (most recent call last): 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 193, in _get_session 
    return self._session_cache 
AttributeError: 'SessionStore' object has no attribute '_session_cache' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection 
    self.connect() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect 
    self.connection = self.get_new_connection(conn_params) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection 
    connection = Database.connect(**conn_params) 
    File "/opt/python/run/venv/local/lib64/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect 
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync) 
psycopg2.OperationalError: could not connect to server: Connection refused 
    Is the server running on host "localhost" (127.0.0.1) and accepting 
    TCP/IP connections on port 5432? 


The above exception was the direct cause of the following exception: 

Traceback (most recent call last): 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/channels/worker.py", line 119, in run 
    consumer(message, **kwargs) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/channels/sessions.py", line 220, in inner 
    result = func(message, *args, **kwargs) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/channels/auth.py", line 71, in inner 
    message.user = auth.get_user(fake_request) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 167, in get_user 
    user_id = _get_user_session_key(request) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/auth/__init__.py", line 59, in _get_user_session_key 
    return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY]) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 48, in __getitem__ 
    return self._session[key] 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 198, in _get_session 
    self._session_cache = self.load() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/contrib/sessions/backends/db.py", line 33, in load 
    expire_date__gt=timezone.now() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 381, in get 
    num = len(clone) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 240, in __len__ 
    self._fetch_all() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 1074, in _fetch_all 
    self._result_cache = list(self.iterator()) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/query.py", line 52, in __iter__ 
    results = compiler.execute_sql() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 846, in execute_sql 
    cursor = self.connection.cursor() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 231, in cursor 
    cursor = self.make_debug_cursor(self._cursor()) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 204, in _cursor 
    self.ensure_connection() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection 
    self.connect() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection 
    self.connect() 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect 
    self.connection = self.get_new_connection(conn_params) 
    File "/opt/python/run/venv/local/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection 
    connection = Database.connect(**conn_params) 
    File "/opt/python/run/venv/local/lib64/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect 
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync) 
django.db.utils.OperationalError: could not connect to server: Connection refused 
    Is the server running on host "localhost" (127.0.0.1) and accepting 
    TCP/IP connections on port 5432? 

は、当然のことながら、私はこのエラーを取得する:

のWebSocketはCLOSINGまたはCLOSED状態に既にあります。

私は間違っていると思いますか?

答えて

1

だからDaphneが実行するときに環境変数が表示されないという問題がありました。 supervisord.confファイルに次のコードを追加して動作させました。

[program:Daphne] 
environment=PATH="/opt/python/run/venv/bin" 
**command=/bin/bash -c "source /opt/python/current/env && /opt/python/run/venv/bin/daphne -b 0.0.0.0 -p 5000 mysite.asgi:channel_layer"** 
directory=/opt/python/current/app 
autostart=true 
autorestart=true 
redirect_stderr=true 
stdout_logfile=/tmp/daphne.out.log 

[program:Worker] 
environment=PATH="/opt/python/run/venv/bin" 
**command=/bin/bash -c "source /opt/python/current/env && python manage.py runworker"** 
directory=/opt/python/current/app 
process_name=%(program_name)s_%(process_num)02d 
numprocs=4 
autostart=true 
autorestart=true 
redirect_stderr=true 
stdout_logfile=/tmp/workers.out.log 
関連する問題