The comet technology on Jetty

1,668 views
1,390 views

Published on

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

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
  • 代码下载不了,能发一份给我吗?
    luning.sun@gmail.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
1,668
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
32
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

The comet technology on Jetty

  1. 1. 基于Jetty的Comet应用基于Jetty Comet应用 Jetty的 http://blog.csdn.net/liu251
  2. 2. 目录1 comet2 应用3 实现(jetty)4 集群5 服务器优化
  3. 3. Comet• “服务器推”技术的应用 – 监控系统 – 即时通信系统:im – 即时报价系统:股票、证券• 实现方案 – ActiveX – Java Applet – Flash XMLSocket – JavaScript-Ajax
  4. 4. 基于Http长连接的Comet 基于Http 长连接的Comet Http长连接的 A jax P u sh(Long poll) A ja x(p o llin g) Browser ServerBrowser Server LifeC ycle LifeC ycle Request Request Suspend Response Even t Response Event LifeC ycle Request LifeC ycle Response Request Timeout Response
  5. 5. 基于Http长连接的Comet 基于Http 长连接的Comet Http长连接的 A jax P u sh(Stream ing) W ebSocketBrowser Server Browser Server LifeC ycle LifeC ycle Request Request Response Even t Response Even t Timeout Timeout Response Even t Response Even t
  6. 6. 应用 • Ajax long poll • Streaming (iFrame)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
  7. 7. 应用•基于Jetty – Google App Engine – Taobao webIM – Sina WeiboIM – ......•优点: – lightweight,mini,pluggable,nature async•Anti-jetty – Grails内置server被转成tomcat – JBoss通信web容器被替换成tomcat
  8. 8. 实现•svn地址:https://svn.ws.netease.com/microblog/products/butter- svn 地址:https://svn.ws.netease.com/microblog/products/butter- svn地址:microblog/branches/websocket-comet•代码websocket-comet/src/test 代码 代码websocket-comet/src/test – 执行com.netease.samples.jetty.handler.HttpServletServer 执行com.netease.samples.jetty.handler.HttpServletServer Jetty•Jetty Continuation long polling – http://127.0.0.1:8080/testhtml/poll.html Jetty•Jetty Continuation streaming – http://127.0.0.1:8080/testhtml/stream.html•Servlet3 Stream Servlet3 – http://127.0.0.1:8080/testhtml/stream2.html WebSocket•WebSocket – http://127.0.0.1:8080/testhtml/websocket.html
  9. 9. 实现 效果图•效果图 long•long polling http://127.0.0.1:8080•http://127.0.0.1:8080/testhtml/poll.html
  10. 10. 效果图•效果图 实现 streaming•streaming http://127.0.0.1:8080/testhtml/stream.html
  11. 11. 效果图•效果图 实现 WebSocket•WebSocket http://127.0.0.1:8080/testhtml/websocket.html
  12. 12. 实现 Jetty•Jetty Continuation Long polling/Streaming – 实现类:ScheduleContinuationServlet/ScheduleContinuationStreamServlet 实现类:ScheduleContinuationServlet/ScheduleContinuationStreamServlet – 典型代码: • Continuation continuation = ContinuationSupport.getContinuation(request); • continuation.suspend(); • continuation.resume(); Servlet3•Servlet3 Streaming – 实现类:Schedule3StreamServlet 实现类:Schedule3StreamServlet – 典型代码: • AsyncContext asyncContext = request.startAsync(); • asyncContext .getResponse().getWriter().write(); WebSocket•WebSocket – 实现类:WebSocketLogServlet 实现类:WebSocketLogServlet – 典型代码: • 实现WebSocketServlet抽象类 实现WebSocketServlet WebSocketServlet抽象类 • 实现WebSocket接口 实现WebSocket WebSocket接口
  13. 13. 实现 Jetty•Jetty Continuation / AsyncServlet Pattern – Suspend Resume Pattern: Pattern: • Continuation : – Continuation continuation = ContinuationSupport.getContinuation(request); – continuation.suspend();/write() – continuation.resume(); • AsyncServlet : – AsyncContext asyncContext = request.startAsync();/write() – asyncContext .dispatch();
  14. 14. 实现 Jetty•Jetty Continuation / AsyncServlet Pattern – Suspend Continue Pattern: Pattern: • Continuation : – Continuation continuation = ContinuationSupport.getContinuation(request); – continuation.suspend(); – write(), continuation.complete(); • AsyncServlet : – AsyncContext asyncContext = request.startAsync(); – write(), asyncContext .complete();
  15. 15. 实现 应用程序保存当前异步请求的上下文数据•应用程序保存当前异步请求的上下文数据
  16. 16. 实现-Async Jetty•Jetty Continuation/Servlet3
  17. 17. HttpServlet.service(ServletRequest req, ServletResponse res) 实现-Async流程 IDLE h an d lin g( ) su sp en d( ) / Dispatched Async Started resu m e( ) /d isp at c h( ) startAsync( ) su sp en d( ) / Completing startAsync( ) Async Wait R e-Dispatching tim eout U n-Completed R e-Dispatch Completed R e-Despatched
  18. 18. 实现-浏览器 实现-• 每个页面一个连接• 多个页面共享一个链接 – 轮询cookie – flash share object – html5 localStore
  19. 19. 集群(WebSocket) 浏览器窗口 浏览器窗口 HAProxy Jetty集群 Jetty Jetty Jetty
  20. 20. 集群(WebSocket) 集群(WebSocket)• 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 保持 – 通过配置,支持WebSocket协议
  21. 21. 集群(WebSocket) 集群(WebSocket)•环境 –hardware: –linux version: –java version:•测试 – HAProxy(1) + Jetty(1) – 连接数:10w – 共2个客户端,每5w个WebSocket连接
  22. 22. 集群(WebSocket)-服务端结果集群(WebSocket)-
  23. 23. 集群(WebSocket)-客户端结果集群(WebSocket)-
  24. 24. 服务器优化• JVM – -XX:+UseParallelGC – -XX:+UseParallelOldGC – -Xms2G – -Xmx8G – -XX:NewRatio=1 – -XX:PermSize=64m – -XX:MaxPermSize=512m• 代码 – google guava cache
  25. 25. 服务器优化• 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
  26. 26. Q/A

×