使用場景
在web開發(fā)中有時候需要實(shí)時獲取數(shù)據(jù)茫蛹,可以采用的方法也很多,比如ajax輪詢烁挟,長連接等婴洼。之前項(xiàng)目中有一個需求是實(shí)時的日志展示,實(shí)時性要求高
還有根據(jù)歷史監(jiān)控?cái)?shù)據(jù)進(jìn)行趨勢圖的繪制撼嗓,數(shù)據(jù)量巨大柬采,等待時間長欢唾。那么如果使用http請求來處理則面臨著超時的問題,如果用ajax頻繁的輪詢將對服務(wù)器造成很大的壓力粉捻。websocket提供了客戶端和服務(wù)器進(jìn)行雙向?qū)崟r的全雙工通信的方法礁遣。并且絕大多數(shù)現(xiàn)代瀏覽器都支持websocket,因此需要使用nginx對websocket服務(wù)進(jìn)行反代和負(fù)載均衡肩刃,nginx從1.3版本后開始支持websocket祟霍。項(xiàng)目用到的tornado框架也原生支持websocket,看來可以嘗試用websocket來嘗試解決問題树酪。
Tornado的支持
tornado對websocket支持的很好浅碾,通過繼承tornado.websocket.WebSocketHandler類就可以實(shí)現(xiàn)對websocket連接的處理。websocket是在標(biāo)準(zhǔn)
http上實(shí)現(xiàn)的续语,websocket中的握手和http中的握手兼容垂谢,它使用http中的Upgrade協(xié)議頭將連接從http升級到WebSocket,從源碼上可以看出
WebSocketHandler繼承了tornado.web.RequestHandler,因此websocket也可以通過get_argument方法獲取ws://URL?**=傳來的參數(shù)疮茄。
WebSocketHandler提供了一系列方法用以處理連接和消息收發(fā)滥朱,源碼中的docstring描述得很清楚,源碼是最好的文檔沒有之一力试。
class WebSocketHandler(tornado.web.RequestHandler):
"""Subclass this class to create a basic WebSocket handler.
Override `on_message` to handle incoming messages, and use
`write_message` to send messages to the client. You can also
override `open` and `on_close` to handle opened and closed
connections.
See http://dev.w3.org/html5/websockets/ for details on the
JavaScript interface. The protocol is specified at
http://tools.ietf.org/html/rfc6455.
Here is an example WebSocket handler that echos back all received messages
back to the client:
.. testcode::
class EchoWebSocket(tornado.websocket.WebSocketHandler):
def open(self):
print("WebSocket opened")
def on_message(self, message):
self.write_message(u"You said: " + message)
def on_close(self):
print("WebSocket closed")
.. testoutput::
:hide:
...
Nginx配置反向代理和負(fù)載均衡
upstream tornadoes {
server 127.0.0.1:7000;
server 127.0.0.1:7001;
}
server {
listen 8000;
server_name ***.com;
location / {
proxy_pass http://tornadoes;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
nginx默認(rèn)采用循環(huán)的方式分配請求徙邻,循環(huán)的將請求分配到upstream中定義的服務(wù)地址。location中的定義對支持websocket必不可少畸裳。