650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131227/1622342X4-0.jpg" title="tornado.jpg" alt="101638437.jpg" />
最近實現了一個http的介面 ~ http的方式,訪問結果為json
nginx做負載,web分離,url轉寄 ~
tornado做資料操作和非同步呼叫lua的介面 ~
memcached做session的共用 (為啥不用redis做session,一方面是為了不和redis有kv衝突,伺服器的中斷冗餘,更主要的方面是用redis儲存session我沒有做出來,鬱悶呀。。。。)
redis做kv資料庫和隊列系統
後端的nginx lua是mysql的http方式的json展現介面
nginx 的配置大家都懂的,就是利用upstream負載。
worker_processes 4; error_log /var/log/nginx/error.log;pid /var/run/nginx.pid; events { worker_connections 30000; useepoll;} http { charset utf-8; # Enumerate all the Tornado servers here upstream frontends { server 127.0.0.1:8000; server 127.0.0.1:8001; server 127.0.0.1:8002; server 127.0.0.1:8003; } include/path/to/nginx.mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; keepalive_timeout 65; proxy_read_timeout 200; sendfile on; tcp_nopush on; tcp_nodelay on; gzip on; gzip_min_length 1000; gzip_proxied any; gzip_types text/plain text/css text/xml application/x-javascript application/xml application/atom+xml text/javascript; # Only retry ifthere was a communication error, not a timeout # on the Tornado server (to avoid propagating "queries of death" # to all frontends) proxy_next_upstream error; server { listen 80; location ^~ /static/ { root /path/to/app; if($query_string) { expires max; } } location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_pass http://frontends; } }}
對於後端節點,監控的檢查請用nginx的check模組 ~
模組:https://github.com/cep21/healthcheck_nginx_upstreams;模組名稱:ngx_http_healthcheck_module
upstream myhttpd { server 127.0.0.1:8800weight=5; server 127.0.0.1:8801weight=5; healthcheck_enabled; 開啟健全狀態檢查功能 healthcheck_delay 1000;對同一台後端伺服器兩次檢測之間的時間間隔,單位毫秒,預設為1000。 healthcheck_timeout 1000;進行一次健康檢測的逾時時間,單位為毫秒,預設值2000。 healthcheck_failcount 3;對一台後端伺服器檢測成功或失敗多少次之後方才確定其為成功或失敗,並實現啟用或禁用此伺服器 healthcheck_send "GET/.health HTTP/1.0";對一台後端伺服器檢測成功或失敗多少次之後方才確定其為成功或失敗,並實現啟用或禁用此伺服器,只支援http 1.0的協議
uwsgi tornado的對比:
有朋友問過我,uwsgi的效能也可以呀。。。
以前我用flask的時候,經常要和uwsgi配合一起用,但是我和群裡的人多方面測試壓力,uwsgi不是很穩定,經常出現訪問錯誤的情況,看debug也是看不出啥東西來。
這麼多的tornado進程該如何開啟?
用screen ,用 >/dev/null,用nohup ? 太搓了點吧~
推薦大家用supervisord管理雜亂的tornado服務和別的python服務。
源地址 http://rfyiamcool.blog.51cto.com/1030776/1301410
這裡是centos supervisord 的配置
配置supervisor
yum install supervisorpip install supervisor
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf
[program:tornado8000]command=python /ops/tornado8000.pyprocess_name=%(program_name)sstdout_logfile=/ops/access.logstderr_logfile=/ops/error.log[program:tornado8001]command=python /ops/tornado8001.pyprocess_name=%(program_name)sstdout_logfile=/ops/access.logstderr_logfile=/ops/error.log[program:tornado8002]command=python /ops/tornado8002.pyprocess_name=%(program_name)sstdout_logfile=/ops/access.logstderr_logfile=/ops/error.log[program:tornado8003]command=python /ops/tornado8003.pyprocess_name=%(program_name)sstdout_logfile=/ops/access.logstderr_logfile=/ops/error.log
儲存完配置後,咱們啟動下: run~
/usr/bin/supervisord -c /etc/supervisord.conf
Supervisord安裝完成後有兩個可用的命令列supervisor和supervisorctl,命令使用解釋如下:
supervisord,初始啟動Supervisord,啟動、管理配置中設定的進程。
supervisorctl stop programxxx,停止某一個進程(programxxx),programxxx為[program:chatdemon]
裡配置的值,這個樣本就是chatdemon。
supervisorctl start programxxx,啟動某個進程
supervisorctl restart programxxx,重啟某個進程
supervisorctl stop groupworker: ,重啟所有屬於名為groupworker這個分組的進程(start,restart同理)
supervisorctl stop all,停止全部進程,註:start、restart、stop都不會載入最新的設定檔。
supervisorctl reload,載入最新的設定檔,停止原有進程並按新的配置啟動、管理所有進程。
supervisorctl update,根據最新的設定檔,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟。
注意:顯示用stop停止掉的進程,用reload或者update都不會自動重啟。
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131227/16223430S-1.png" title="2013-09-25 11:47:15的螢幕.png" alt="114739607.png" />
然後用裝飾器判斷狀態。。。
@tornado.web.authenticated
代碼在這裡 ,大家配置自己的系統搞一下。
https://github.com/mmejia27/tornado-memcached-sessions
關於lua的用法,大家可以看看我以前的博文,對於mysql有詳細的用法的。
模組代碼:
https://github.com/agentzh/lua-resty-mysql
執行個體代碼:
upstream nginxbackend { drizzle_server 127.0.0.1:3306 dbname=nginx password=youpassword user=yourdbname protocol=mysql charset=utf8; drizzle_keepalive max=100 mode=single overflow=reject; } location = /getuser { internal; set_unescape_uri $user $remote_user; set_unescape_uri $passwd $remote_passwd; set_quote_sql_str $user $user; set_quote_sql_str $passwd $passwd; drizzle_query "select count(*) as count from user where user=$user and passwd=$passwd"; drizzle_pass nginxbackend; rds_json on; }
總之,我們要多方面的測試壓力,才能找到適合自己業務的架構。
本文出自 “峰雲,就她了。” 部落格,請務必保留此出處http://rfyiamcool.blog.51cto.com/1030776/1301410