the comet technology on jetty

26
http://blog.csdn.net/liu251 基于 基于 基于 基于Jetty Jetty Jetty Jetty的 的Comet Comet Comet Comet应用 应用 应用 应用

Upload: wavefly

Post on 27-May-2015

1.152 views

Category:

Technology


5 download

DESCRIPTION

The comet technology on Jetty, such as websocket, ajax long poll, frame streaming

TRANSCRIPT

http://blog.csdn.net/liu251

基于基于基于基于JettyJettyJettyJetty的的的的CometCometCometComet应用应用应用应用

目录

1111

3333

集群

comet

实现(jetty)

2222 应用

4444

服务器优化5555

CometCometCometComet

• “服务器推”技术的应用– 监控系统

– 即时通信系统:im– 即时报价系统:股票、证券

• 实现方案实现方案实现方案实现方案– ActiveX– Java Applet– Flash XMLSocket– JavaScript-Ajax

基于基于基于基于HttpHttpHttpHttp长连接的长连接的长连接的长连接的CometCometCometComet

LifeCycle

LifeCycle

Browser Server

RequestResponse

RequestResponse

Event

A ja x (p o llin g )

LifeCycle

LifeCycle

Browser Server

Request

Response Event

A jax P ush (Long poll)

Request

Response

SuspendTim

eout

基于基于基于基于HttpHttpHttpHttp长连接的长连接的长连接的长连接的CometCometCometComet

LifeCycle

Timeout

Browser Server

Request

Response Event

A jax P ush (Stream ing)

Response Event

LifeCycleTim

eout

Browser Server

Request

Response Event

WebSocket

Response Event

应用应用应用应用• Ajax long poll

http://2.113.web0.im.weibo.com/im?

jsonp=parent.org.cometd.script._callback79&message=%5B%7B%22channel%22%3A%22%2Fmeta%2Fconnect%22%2C%22connectionType%22%3A%22callback-polling%22%2C%22id%22%3A80%2C%22clientId%22%3A%22sir4xctmetaqi52dtk%22%7D%5D&1340098082636

• Streaming (iFrame)

应用•基于Jetty

– Google App Engine– Taobao webIM– Sina WeiboIM– ......

•优点:– lightweight,mini,pluggable,nature async

•Anti-jetty– Grails内置server被转成tomcat– JBoss通信web容器被替换成tomcat

实现•svnsvnsvnsvn地址:地址:地址:地址:https://svn.ws.netease.com/microblog/products/butter-https://svn.ws.netease.com/microblog/products/butter-https://svn.ws.netease.com/microblog/products/butter-https://svn.ws.netease.com/microblog/products/butter-microblog/branches/websocket-cometmicroblog/branches/websocket-cometmicroblog/branches/websocket-cometmicroblog/branches/websocket-comet

•代码代码代码代码websocket-comet/src/testwebsocket-comet/src/testwebsocket-comet/src/testwebsocket-comet/src/test– 执行执行执行执行com.netease.samples.jetty.handler.HttpServletServercom.netease.samples.jetty.handler.HttpServletServercom.netease.samples.jetty.handler.HttpServletServercom.netease.samples.jetty.handler.HttpServletServer

•Jetty Continuation long pollingJetty Continuation long pollingJetty Continuation long pollingJetty Continuation long polling– http://127.0.0.1:8080/testhtml/poll.htmlhttp://127.0.0.1:8080/testhtml/poll.htmlhttp://127.0.0.1:8080/testhtml/poll.htmlhttp://127.0.0.1:8080/testhtml/poll.html

•Jetty Continuation streamingJetty Continuation streamingJetty Continuation streamingJetty Continuation streaming– http://127.0.0.1:8080/testhtml/stream.htmlhttp://127.0.0.1:8080/testhtml/stream.htmlhttp://127.0.0.1:8080/testhtml/stream.htmlhttp://127.0.0.1:8080/testhtml/stream.html

•Servlet3 StreamServlet3 StreamServlet3 StreamServlet3 Stream– http://127.0.0.1:8080/testhtml/stream2.htmlhttp://127.0.0.1:8080/testhtml/stream2.htmlhttp://127.0.0.1:8080/testhtml/stream2.htmlhttp://127.0.0.1:8080/testhtml/stream2.html

•WebSocketWebSocketWebSocketWebSocket– http://127.0.0.1:8080/testhtml/websocket.htmlhttp://127.0.0.1:8080/testhtml/websocket.htmlhttp://127.0.0.1:8080/testhtml/websocket.htmlhttp://127.0.0.1:8080/testhtml/websocket.html

实现•效果图效果图效果图效果图•long polling long polling long polling long polling •http://127.0.0.1:8080http://127.0.0.1:8080http://127.0.0.1:8080http://127.0.0.1:8080/testhtml/poll.html/testhtml/poll.html/testhtml/poll.html/testhtml/poll.html

实现•效果图效果图效果图效果图•streaming streaming streaming streaming http://127.0.0.1:8080/testhtml/stream.htmlhttp://127.0.0.1:8080/testhtml/stream.htmlhttp://127.0.0.1:8080/testhtml/stream.htmlhttp://127.0.0.1:8080/testhtml/stream.html

实现•效果图效果图效果图效果图•WebSocket WebSocket WebSocket WebSocket http://127.0.0.1:8080/testhtml/websocket.htmlhttp://127.0.0.1:8080/testhtml/websocket.htmlhttp://127.0.0.1:8080/testhtml/websocket.htmlhttp://127.0.0.1:8080/testhtml/websocket.html

实现•Jetty Continuation Long polling/StreamingJetty Continuation Long polling/StreamingJetty Continuation Long polling/StreamingJetty Continuation Long polling/Streaming

– 实现类:实现类:实现类:实现类:ScheduleContinuationServlet/ScheduleContinuationStreamServletScheduleContinuationServlet/ScheduleContinuationStreamServletScheduleContinuationServlet/ScheduleContinuationStreamServletScheduleContinuationServlet/ScheduleContinuationStreamServlet– 典型代码:典型代码:典型代码:典型代码:

• Continuation continuation = Continuation continuation = Continuation continuation = Continuation continuation = ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);

• continuation.suspend();continuation.suspend();continuation.suspend();continuation.suspend();• continuation.resume();continuation.resume();continuation.resume();continuation.resume();

•Servlet3 StreamingServlet3 StreamingServlet3 StreamingServlet3 Streaming– 实现类:实现类:实现类:实现类:Schedule3StreamServletSchedule3StreamServletSchedule3StreamServletSchedule3StreamServlet– 典型代码:典型代码:典型代码:典型代码:

• AsyncContext asyncContext = request.startAsync();AsyncContext asyncContext = request.startAsync();AsyncContext asyncContext = request.startAsync();AsyncContext asyncContext = request.startAsync();• asyncContext .getResponse().getWriter().write();asyncContext .getResponse().getWriter().write();asyncContext .getResponse().getWriter().write();asyncContext .getResponse().getWriter().write();

•WebSocketWebSocketWebSocketWebSocket– 实现类:实现类:实现类:实现类:WebSocketLogServletWebSocketLogServletWebSocketLogServletWebSocketLogServlet– 典型代码:典型代码:典型代码:典型代码:

• 实现实现实现实现WebSocketServletWebSocketServletWebSocketServletWebSocketServlet抽象类抽象类抽象类抽象类• 实现实现实现实现WebSocketWebSocketWebSocketWebSocket接口接口接口接口

实现•Jetty Continuation / AsyncServlet PatternJetty Continuation / AsyncServlet PatternJetty Continuation / AsyncServlet PatternJetty Continuation / AsyncServlet Pattern

– Suspend Resume PatternSuspend Resume PatternSuspend Resume PatternSuspend Resume Pattern::::• Continuation Continuation Continuation Continuation ::::

– Continuation continuation = Continuation continuation = Continuation continuation = Continuation continuation = ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);

– continuation.suspend();/write()continuation.suspend();/write()continuation.suspend();/write()continuation.suspend();/write()– continuation.resume();continuation.resume();continuation.resume();continuation.resume();

• AsyncServlet AsyncServlet AsyncServlet AsyncServlet ::::– AsyncContext asyncContext = AsyncContext asyncContext = AsyncContext asyncContext = AsyncContext asyncContext =

request.startAsync();/write()request.startAsync();/write()request.startAsync();/write()request.startAsync();/write()– asyncContext .dispatch();asyncContext .dispatch();asyncContext .dispatch();asyncContext .dispatch();

实现•Jetty Continuation / AsyncServlet PatternJetty Continuation / AsyncServlet PatternJetty Continuation / AsyncServlet PatternJetty Continuation / AsyncServlet Pattern

– Suspend Continue PatternSuspend Continue PatternSuspend Continue PatternSuspend Continue Pattern::::• Continuation Continuation Continuation Continuation ::::

– Continuation continuation = Continuation continuation = Continuation continuation = Continuation continuation = ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);ContinuationSupport.getContinuation(request);

– continuation.suspend();continuation.suspend();continuation.suspend();continuation.suspend();– write(), continuation.complete();write(), continuation.complete();write(), continuation.complete();write(), continuation.complete();

• AsyncServlet AsyncServlet AsyncServlet AsyncServlet ::::– AsyncContext asyncContext = request.startAsync();AsyncContext asyncContext = request.startAsync();AsyncContext asyncContext = request.startAsync();AsyncContext asyncContext = request.startAsync();– write(), asyncContext .complete();write(), asyncContext .complete();write(), asyncContext .complete();write(), asyncContext .complete();

实现

•应用程序保存当前异步请求的上下文数据应用程序保存当前异步请求的上下文数据应用程序保存当前异步请求的上下文数据应用程序保存当前异步请求的上下文数据

实现-Async•Jetty Continuation/Servlet3Jetty Continuation/Servlet3Jetty Continuation/Servlet3Jetty Continuation/Servlet3

实现-Async流程

HttpServlet.service(ServletR

equest req, ServletResponse res)

Dispatched Async Started

R e -Dispatch

su sp en d( ) /startAsync( )

R e -Despatched

U n -Completed

Completed

Async Wait

tim eout

I D L E

hand ling( )

su sp en d( ) /startAsync( )

R e -Dispatching

resu m e( ) / d isp atc h( )

Completing

• 每个页面一个连接

• 多个页面共享一个链接– 轮询cookie– flash share object– html5 localStore

实现实现实现实现----浏览器浏览器浏览器浏览器

集群(WebSocket)

Jetty集群

浏览器窗口

Jetty

浏览器窗口

HAProxy

Jetty Jetty

• HAProxy特性– 支持4层到7层协议(nginx支持7层),支持MySQL 负载均衡

– 负载均衡算法

• roundrobin(static-rr)• ip hash• uri hash• least connection• url_param hash• head["name"] hash• cookie["name] hash

• 引入原因

– url_hash– session 保持

– 通过配置,支持通过配置,支持通过配置,支持通过配置,支持WebSocketWebSocketWebSocketWebSocket协议协议协议协议

集群集群集群集群(WebSocket)(WebSocket)(WebSocket)(WebSocket)

•环境–hardware:– linux version:– java version:

•测试– HAProxy(1) + Jetty(1)– 连接数:10w– 共2个客户端,每5w个WebSocket连接

集群集群集群集群(WebSocket)(WebSocket)(WebSocket)(WebSocket)

集群集群集群集群(WebSocket)-(WebSocket)-(WebSocket)-(WebSocket)-服务端结果

集群集群集群集群(WebSocket)-(WebSocket)-(WebSocket)-(WebSocket)-客户端结果

• JVM– -XX:+UseParallelGC– -XX:+UseParallelOldGC– -Xms2G– -Xmx8G– -XX:NewRatio=1– -XX:PermSize=64m– -XX:MaxPermSize=512m

• 代码– google guava cache

服务器优化服务器优化服务器优化服务器优化

• Linux– TCP(client)

• Caused by: java.net.BindException: Cannot assign requested address• at sun.nio.ch.Net.connect(Native Method)• at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:500)• at org.eclipse.jetty.websocket.WebSocketClient.open(WebSocketClient.java:346)• at org.eclipse.jetty.websocket.WebSocketClient.open(WebSocketClient.java:305)

– 文件句柄(server)

服务器优化服务器优化服务器优化服务器优化

TCP(Server)#vi /etc/sysctl.conf net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 #sysctl -p

Q/A