Django Channels 2和EC2/Elastic Beanstalk

3

我有一个简短的问题,如何在AWS Elasticbeanstalk上设置Django-channels v2?下面是更长、不那么吸引人的问题。

谢谢您提前的帮助!


我正在尝试使用Django Channels 2.02设置elasticbeanstalk实例。我遵循了这个https://hackernoon.com/setting-up-django-channels-on-aws-elastic-beanstalk-716fd5a49c4a教程。该教程是为Channels 1编写的。
Elasticbeanstalk实例使用应用程序负载均衡器创建。Redis 4.0实例正在运行0.0.0.0:6379,通过netstat确认。在redis实例上自定义TCP,端口范围:6379,源:0.0.0.0/0。
在.ebekstension中的alb_listener.config文件:
option_settings:   aws:elbv2:listener:80:
    DefaultProcess: http
    ListenerEnabled: 'true'
    Protocol: HTTP   aws:elasticbeanstalk:environment:process:http:
    Port: '8000'
    Protocol: HTTP

Daphne服务器使用命令"Daphne .asgi:application"启动。监听TCP地址0.0.0.0:8000。它返回:

2018-03-12 08:59:30,778 ERROR    Exception inside application: [Errno -2] Name or service not known
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/channels/consumer.py", line 54, in __call__
    await await_many_dispatch([receive, self.channel_receive], self.dispatch)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/channels/utils.py", line 47, in await_many_dispatch
    result = task.result()
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/channels_redis/core.py", line 167, in receive
    task.result()
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/channels_redis/core.py", line 187, in receive_loop
    real_channel, message = await self.receive_single(general_channel)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/channels_redis/core.py", line 224, in receive_single
    async with self.connection(index) as connection:
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/channels_redis/core.py", line 403, in __aenter__
    self.conn = await aioredis.create_redis(**self.kwargs)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/aioredis/commands/__init__.py", line 174, in create_redis
    loop=loop)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/aioredis/connection.py", line 107, in create_connection
    timeout, loop=loop)
  File "/usr/lib64/python3.6/asyncio/tasks.py", line 339, in wait_for
    return (yield from fut)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/aioredis/stream.py", line 19, in open_connection
    lambda: protocol, host, port, **kwds)
  File "/usr/lib64/python3.6/asyncio/base_events.py", line 733, in create_connection
    infos = f1.result()
  File "/usr/lib64/python3.6/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/lib64/python3.6/socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
  [Errno -2] Name or service not known

浏览器控制台中没有Websocket错误。

它在我的本地运行服务器上可以工作。问题出在哪里?请帮我编写一个配置脚本,这样我就不需要手动启动Daphne了吗?


嘿,你找到解决办法了吗?我在EC2上经常遇到这个问题。谢谢。 - BoCode
你好 @BoCode,抱歉我没有找到答案。 - Espen Finnesand
我也在尝试同样的事情!如果你成功了,请告诉我。 - Karl Zillner
这对我来说目前还是个谜。一旦我找到解决方案,我会通知你的。 - BoCode
Channels 2 不使用 Daphne……我也在尝试让 Channels 2 在 Elastic Beanstalk 上运行……但我的错误不同。 - Karl Zillner
https://stackoverflow.com/questions/52167242/channels2-working-on-aws-elastic-beanstalk - Karl Zillner
1个回答

0

非常晚的回复。

教程中的问题在于config:hosts URL。从URL中删除redis://部分。

例如,

REDIS_PUBLIC_DNS = "YOUR-REDIS-DNS.amazonaws.com"

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [(REDIS_PUBLIC_DNS, 6379)],
        }
    }
}


您可以使用redis-cli检查连接,
redis-cli -h REDIS-PUBLIC-DNS-url ping ;you should expect PONG in response.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接