uWSGI 的 Django 部署的方式
0

准备工作

1、django,uWSGI, nginx安装请自理

2、创建一个django项目:uwsgitest

3、部署目录

/web/node1/uwsgitest
/web/node2/uwsgitest
/web/node3/uwsgitest

4、本地开发host:dev.test.com
5、nginx配置

server {
    listen       80;
    server_name dev.test.com;

    access_log  /usr/local/etc/openresty/logs/testuwsgi.access.log;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    location / {
        proxy_pass http://127.0.0.1:8011;
    }
}

实践

1.基于http协议配置(node1)

在node1目录下创建uwsgi.ini

[uwsgi]
chdir=/web/node1/uwsgitest
http=:8011
processes=4
harakiri=200
max-request=5000
pythonpath=/web/node1/uwsgitest
wsgi-file=/web/node1/uwsgitest/uwsgitest/wsgi.py
daemonize=/web/node1/uwsgitestnode2.log
master=true
pidfile=/web/node1/uwsgitest.pid
procname-prefix=uwsgitest_
procname-master=uwsgitestmaster

uwsgi --ini uwsgi.ini
注:http方式自己会产http进程(可以认为与nginx同一层)负责路由http请求给worker, http进程和worker之间使用的是uwsgi协议,简单理解不用代理也可以方法。
访问:http://dev.test.com/hello/

2.基于uwsgi协议配置(node2)

[uwsgi]
chdir=/web/node1/uwsgitest
socket=127.0.0.1:8012
processes=4
harakiri=200
max-request=5000
pythonpath=/web/node1/uwsgitest
wsgi-file=/web/node1/uwsgitest/uwsgitest/wsgi.py
daemonize=/web/node1/uwsgitestnode2.log
master=truepidfile=/web/node1/uwsgitest.pid
procname-prefix=uwsgitest_
procname-master=uwsgitestmaster

启动:uwsgi --ini uwsgi.ini 访问下。如果你这就访问了,那就GG了 当前基于的uwsgi协议,nginx与web应用服务应该使用uwsgi协议通信,强大的nginx无所不能~ 对应修改

location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8012;#        proxy_pass http://127.0.0.1:8011;
    }

这时访问下:http://dev.test.com/hello/ 也可直接通过ip:port的方式访问,你会发现访问不了~~

3.FastRouter&Subscription server方式(node3 推荐使用)

FastRouter共有5种配置方式:

  • Way 1: –fastrouter-use-base
  • Way 2: –fastrouter-use-pattern
  • Way 3: –fastrouter-use-cache
  • Way 4: –fastrouter-subscription-server
  • Way 5: –fastrouter-use-code-string

这里我使用Way4,为啥呢,官网是这么说的
This is probably one of the best way for massive auto-scaling hosting.
大体意思就是可以更快的水平扩展,负载均衡。uwsgi帮你完成服务注册和发现机制,所以水平扩展很快。如果按照上两种方式增加web应用节点可能需要修改nginx和uwsgi配置,以及重启nginx。

(1) 创建fastrouter subscription server

这是个独立的server,我们在根目录下创建uwsgi目录,创建router.ini

fastrouter=:1717[1]
fastrouter-subscription-server=127.0.0.1:2626[2]
daemonize=/uwsgi/router.logstats=10.24.235.157:6667[3]
pidfile=/uwsgi/router.pid
master=true

[1]1717为当前uWSGI fastrouter server的端口,前面的空代表当前主机地址。(nginx会用到这个端口)
[2]fastrouter-subscription-server 表示当前uWSGI fastrouter server的订阅地址。(web应用服务会用到)
[3]stats:uWSGI的统计服务机制,访问会返回一个json对象,都是统计信息(http://uwsgi-docs.readthedocs.io/en/latest/StatsServer.html

启动:uwsgi --ini router.ini 查看日志如果如下就表示成功

uWSGI fastrouter bound on :1717 fd 3
uWSGI fastrouter subscription server bound on 127.0.0.1:2626 fd 4
Python version: 2.7.10 (default, Oct 23 2015, 19:19:21)  [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)]*** Python threads support is disabled. You can enable it with --enable-threads ***Python main interpreter initialized at 0x7fa578e089a0
your mercy for graceful operations on workers is 60 seconds*** Operational MODE: no-workers ***spawned uWSGI master process (pid: 11115)*** Stats server enabled on 127.0.0.1:6667 fd: 10 ***spawned uWSGI fastrouter 1 (pid: 11116)

(2) web应用服务订阅fastrouter server

(2-1)编辑node3下文件uwsgi:

[uwsgi]
chdir=/web/node3/uwsgitest
socket=127.0.0.1:0 [1]
subscribe-to=127.0.0.1:2626:dev.test.com [2]
processes=4harakiri=240harakiri-verbose = truemax-request=5000buffer-size=327680max-fd=120000listen=1024wsgi-file=/web/node3/uwsgitest/uwsgitest/wsgi.py
daemonize=/web/node3/uwsgi.logmaster=truepidfile=/web/node3/uwsgi.pid
procname-prefix=node3_
procname-master=master
master-fifo=/web/node3/uwsgi.ff [3]
unsubscribe-on-graceful-reload=truelazy-appslog-master

[1]此处0的含义代表随机生成,不需要手动修改,这种方法在水平扩展上会很方便,很快。
[2]此处就是精华所在,我们通过subscribe-to变量来订阅fastrouter server(127.0.0.1:2626),冒号后跟着的是对应请求的域名,只有来自当前域名的请求才会进入当前web节点。当然这个可以设置多个subscribe-to,例如: subscribe-to=127.0.0.1:2626:test.com
[3]基于Unix命名管道(FIFO)可以通过简单的命令管理服务 详情(http://uwsgi-docs.readthedocs.io/en/latest/MasterFIFO.html

(2-2)启动:uwsgi --ini uwsgi.ini
如果有以下信息代表成功了

*** uWSGI is running in multiple interpreter mode ***spawned uWSGI master process (pid: 14589)spawned uWSGI worker 1 (pid: 14590, cores: 1)
spawned uWSGI worker 2 (pid: 14591, cores: 1)spawned uWSGI worker 3 (pid: 14592, cores: 1)spawned uWSGI worker 4 (pid: 14593, cores: 1)
subscribing to 127.0.0.1:2626:dev.test.com
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fd81ad08230 pid: 14591 (default app)WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fd81ad08230 pid: 14593 (default app)WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fd81ad08230 pid: 14592 (default app)WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x7fd81ad08230 pid: 14590 (default app)

这时我们看看fastrouter server的日志,可以看到订阅信息

spawned uWSGI fastrouter 1 (pid: 11116)
[uwsgi-subscription for pid 11116] new pool: dev.test.com (hash key: 29537)
[uwsgi-subscription for pid 11116] dev.test.com => new node: 127.0.0.1:64924

执行:uwsgi --stop uwsgi.ini 会看到

[uWSGI fastrouter pid 11116] dev.test.com => marking 127.0.0.1:60602 as failed

重启进入下一个环节
(2-3)修改nginx配置

server {
    listen       80;
    server_name dev.test.com;

    access_log  /usr/local/etc/openresty/logs/testuwsig.access.log;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    location / {
        include uwsgi_params;#3
        uwsgi_pass 127.0.0.1:1717; [1]#2      uwsgi_pass 127.0.0.1:8012;#1       proxy_pass http://127.0.0.1:8012;
    }
}
重载 nginx -s reload [1] 这里我们配置的是fastrouter server的访问地址

(2-4)增加心节点node4

重复(2-3)

(2-5)使用shell脚步管理uwsgi web应用

篇幅有限请访问:https://github.com/guoyucheng/uwsgitest/blob/master/config/fast-router-server-conf/operation.sh

总结

本方法采用的是多进程的方式(processes),曾经遇到过一位码友使用的是uwsig + gevent 协程的方法利用多线程,据说并发能力很强而且充分利用资源,有时间会尝试下。

上述 源码:https://github.com/guoyucheng/uwsgitest config目录下配置信息

bert

本帖由 Summer 于 1年前 取消沉帖
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,使用方法请见 Emoji 自动补全来咯,可用的 Emoji 请见 :metal: :point_right: Emoji 列表 :star: :sparkles:
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
  • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
  请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!