首先,对不起这个长期问题,我希望你们中的一些人能够耐心等待。
我正在遵循本指南Deploying Django Channels Using Daphne,因此我可以运行一些实时应用程序(使用WebSockets)。如果没有nginx,并从命令行运行worker(python manage.py runworker)和接口(daphne),我可以访问正确的渠道使用者类,如下面的日志中所示(这些是从javascript客户端触发的) :
2017-10-09 21:10:35,210 - DEBUG - worker - Got message on websocket.connect (reply daphne.response.CYeWgnNQoY!mwuQrazQtv)
2017-10-09 21:10:35,211 - DEBUG - runworker - websocket.connect
2017-10-09 21:10:35,211 - DEBUG - worker - Dispatching message on websocket.connect to api.consumers.OrderConsumer
2017-10-09 21:10:48,132 - DEBUG - worker - Got message on websocket.receive (reply daphne.response.CYeWgnNQoY!mwuQrazQtv)
2017-10-09 21:10:48,132 - DEBUG - runworker - websocket.receive
2017-10-09 21:10:48,132 - DEBUG - worker - Dispatching message on websocket.receive to api.consumers.OrderConsumer
这些事件是由以下javascript调用触发的:
ws = new WebSocket("ws://localhost:8000/order/1/")
ws.send("test")
使用nginx,并在systemd上运行interface和worker,尽管使用完全相同的触发器输入,我仍然得到以下日志。
2017-10-09 20:38:35,503 - DEBUG - worker - Got message on websocket.connect (reply daphne.response.PPGuXtBmQD!EgUfaNZjUj)
2017-10-09 20:38:35,503 - DEBUG - runworker - websocket.connect
2017-10-09 20:38:35,503 - DEBUG - worker - Dispatching message on websocket.connect to channels.routing.connect_consumer
2017-10-09 20:38:42,993 - DEBUG - worker - Got message on websocket.receive (reply daphne.response.PPGuXtBmQD!EgUfaNZjUj)
2017-10-09 20:38:42,993 - DEBUG - runworker - websocket.receive
2017-10-09 20:38:42,993 - DEBUG - worker - Dispatching message on websocket.receive to channels.routing.null_consumer
请注意,接收频道正在路由到null_consumer
。我认为这里的问题仅仅是channels.routing
设置得不好的事实。由于我在两个版本中都使用相同的设置(Django设置文件),这可能意味着设置本身未正确加载。请考虑以下文件。
## rest-api/farmaApp/settings.py
...
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'asgi_redis.RedisChannelLayer',
'CONFIG': {
'hosts': [('localhost', 6379)],
},
'ROUTING': 'farmaApp.routing.channel_routing',
}
}
...
应将channels.routing
设置为:
## rest-api/farmaApp/routing.py
from channels.routing import route
from api.consumers import ws_connect, ws_disconnect, OrderConsumer
channel_routing = [
route('websocket.connect', ws_connect, path=r'^/users/'),
route('websocket.disconnect', ws_disconnect, path=r'^/users/'),
OrderConsumer.as_route(path=r'^/order/(?P<order_id>[\d+])/'),
]
同样,我不认为配置本身是错误的,因为它没有systemd。最后,这是我的系统配置:
## /etc/systemd/system/daphne.service
[Unit]
Description=daphne daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/rest-api
Environment=DJANGO_SETTINGS_MODULE=farmaApp.settings
ExecStart=/home/ubuntu/rest-api/env/bin/daphne --access-log /home/ubuntu/rest-api/access.log -b 0.0.0.0 -p 8001 farmaApp.asgi:channel_layer
[Install]
WantedBy=multi-user.target
## /etc/systemd/system/django_worker.service
[Unit]
Description=django_worker daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/rest-api
Environment=DJANGO_SETTINGS_MODULE=farmaApp.settings
ExecStart=/home/ubuntu/rest-api/env/bin/python manage.py runworker -v 2
[Install]
WantedBy=multi-user.target
请注意我根据链接指南将DJANGO_SETTINGS_MODULE变量导出到环境的两个配置文件。我认为这不符合预期。
答案 0 :(得分:2)
我刚刚部署了我的django频道应用,并且以下systemd服务文件在没有使用主管的情况下为我工作:
/etc/systemd/system/django-channels-daphne.service
[Unit]
Description=daphne server script for my project
After=network.target
[Service]
User=webuser
Group=webuser
WorkingDirectory=/path/to/myproject
Environment=DJANGO_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Environment=DJANGO_ALLOWED_HOSTS=myapp.chatbot.ai
ExecStart=/path/to/python/virtualenv/bin/daphne -b 0.0.0.0 -p 8000 myproject.asgi:channel_layer
Restart=always
[Install]
WantedBy=multi-user.target
/etc/systemd/system/django-channels-runworker.service
[Unit]
Description=python runworker server for myproject
After=network.target
[Service]
User=webuser
Group=webuser
WorkingDirectory=/path/to/myproject
Environment=DJANGO_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Environment=DJANGO_ALLOWED_HOSTS=myapp.chatbot.ai
ExecStart=/path/to/python/virtualenv/bin/python /path/to/myproject/manage.py runworker --threads 4
Restart=always
[Install]
WantedBy=multi-user.target
/path/to/myproject/myproject/asgi.py
import os
import channels
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
channel_layer = channels.asgi.get_channel_layer()
/path/to/myproject/myproject/settings.py中的一些行:
ALLOWED_HOSTS = [os.environ['DJANGO_ALLOWED_HOSTS']]
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
答案 1 :(得分:0)
是的,主管确实有效。 /etc/supervisor/conf.d/project_name.conf的关键位是(注释部分中的一些其他链接):
[program:platform_asgi_daphne]
; # https://stackoverflow.com/questions/17055951/how-to-set-environment-variables-in-supervisor-service
; # https://github.com/django/daphne/pull/37
; # https://wiki.cac.washington.edu/display/infra/Extracting+Certificate+and+Private+Key+Files+from+a+.pfx+File
; # daphne -e ssl:8443:privateKey=localhost.key:certKey=localhost.crt <channel_layer>
environment =
DJANGO_SECRET_KEY='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
DJANGO_DEBUG='True'
directory=/path/to/project/
; # http://channels.readthedocs.io/en/stable/deploying.html
; command=/path/to/venv/bin/daphne --port 80 --bind 0.0.0.0 classact.asgi:channel_layer
command=/path/to/venv/bin/daphne -e ssl:443:privateKey=../keys/server.key:certKey=../keys/server.crt --bind 0.0.0.0 projectfoldername.asgi:channel_layer
;user=webapps
;group=webapps
user=root
group=webapps
[program:platform_asgi_workers]
; # https://github.com/django/channels/issues/408#issuecomment-276384104
environment =
DJANGO_SECRET_KEY='xxxxxxxxxxxxxxxxxxxxxxxxx',
DJANGO_DEBUG='True'
command=/path/to/venv/bin/python /path/to/project/manage.py runworker
process_name=asgi_worker%(process_num)s
numprocs=2
;user=webapps
user=root
group=webapps
我有几个问题正在运行,因为用户的webapps还没有整理好,所以它们以root身份运行(写入带有奇数烫发的加密文件夹)。我没有达到我不在每个部分重复一堆环境变量的程度(可能有一种方法)。还有其他几个像数据库,用户,静态root等,具体取决于平台(prod或dev)。
我也有一些证明要处理,所以也显示(如果不需要,可以取出ssl部分)。还要注意跑两个工人。
答案 2 :(得分:0)
在你发布的配置中,daphne端口似乎已关闭。
你写道:ws = new WebSocket("ws://localhost:8000/order/1/")
ws.send("test")`
在systemd启动文件中使用8001
ExecStart=/home/ubuntu/rest-api/env/bin/daphne --access-log /home/ubuntu/rest-api/access.log -b 0.0.0.0 -p 8001 farmaApp.asgi:channel_layer