Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络应用程序提供了非常便利的框架。当前发行的 MINA 版本支持基于 Java NIO 技术的 TCP/UDP 应用程序开发、串口通讯程序(只在最新的预览版中提供),MINA 所支持的功能也在进一步的扩展中。

新项目里的web service需要高并发服务能力,tomcat目前最多支持到200个并发。远达不到要求。

0.测试环境:

client和server:CPU:单核2G,内存:4GB;

网络:局域网

1.下载mina2.0安装包
打开eclipse建立工程HelloServer,把mina-core-2.0.4.jar,mina-filter-compression-2.0.4.jar
,slf4j-api-1.6.1.jar导入,注意java必须要1.5以上
3.写代码
我的测试代码:
 
  1. package org.apache.mina.example.httpserver.codec;  
  2.     
  3.  
  4.   import org.apache.mina.core.future.IoFutureListener;  
  5. import org.apache.mina.core.service.IoHandler;  
  6.   import org.apache.mina.core.service.IoHandlerAdapter;  
  7. import org.apache.mina.core.session.IdleStatus;  
  8. import org.apache.mina.core.session.IoSession;  
  9.  
  10.     
  11.   /**  
  12.    * An {@link IoHandler} for HTTP.  
  13.    *  
  14.    * @author The Apache MINA Project (dev@mina.apache.org)  
  15.    * @version $Rev: 588178 $, $Date: 2007-10-25 18:28:40 +0900 (?, 25 10? 2007) $  
  16.    */ 
  17.   public class ServerHandler extends IoHandlerAdapter {  
  18.       @Override 
  19.       public void sessionOpened(IoSession session) {  
  20.           // set idle time to 60 seconds  
  21.  
  22.           session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60);  
  23.       }  
  24.     
  25.       @Override 
  26.       public void messageReceived(IoSession session, Object message) {  
  27.           // Check that we can service the request context  
  28.  
  29.           HttpResponseMessage response = new HttpResponseMessage();  
  30.           response.setContentType("text/plain");  
  31.           response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);  
  32.           response.appendBody("CONNECTED");  
  33.     
  34.           // msg.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);  
  35.  
  36.           // byte[] b = new byte[ta.buffer.limit()];  
  37.  
  38.           // ta.buffer.rewind().get(b);  
  39.  
  40.           // msg.appendBody(b);  
  41.  
  42.           // System.out.println("####################");  
  43.  
  44.           // System.out.println(" GET_TILE RESPONSE SENT - ATTACHMENT GOOD DIAMOND.SI="+d.si+  
  45.  
  46.           // ", "+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date()));  
  47.  
  48.           // System.out.println("#################### - status="+ta.state+", index="+message.getIndex());  
  49.  
  50.     
  51.           // Unknown request  
  52.  
  53. // response = new HttpResponseMessage();  
  54.  
  55. // response.setResponseCode(HttpResponseMessage.HTTP_STATUS_NOT_FOUND);  
  56.  
  57. // response.appendBody(String.format(  
  58.  
  59. // "<html><body><h1>UNKNOWN REQUEST %d</h1></body></html>",  
  60.  
  61. // HttpResponseMessage.HTTP_STATUS_NOT_FOUND));  
  62.  
  63.             
  64.         response = new HttpResponseMessage();  
  65.         response.setResponseCode(HttpResponseMessage.HTTP_STATUS_SUCCESS);  
  66.         response.appendBody("<html><body><h1>"+new java.text.SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss.SSS").format(new java.util.Date())+"</h1></body></html>");  
  67.     
  68.           if (response != null) {  
  69.               session.write(response).addListener(IoFutureListener.CLOSE);  
  70.           }  
  71.       }  
  72.     
  73.       @Override 
  74.       public void sessionIdle(IoSession session, IdleStatus status) {  
  75.           //IoSessionLogger.getLogger(session).info("Disconnecting the idle.");  
  76.  
  77.           session.close();  
  78.       }  
  79.     
  80.       @Override 
  81.       public void exceptionCaught(IoSession session, Throwable cause) {  
  82.           //IoSessionLogger.getLogger(session).warn(cause);  
  83.  
  84.           session.close();  
  85.       }  
  86.   } 
把代码写好,官方例子是1.x的,我在附件里把例子里的import错误改过来
 
另外为了好测试,把ServerHandler.java改为(粗体那段是我加的):
 
4.打成jar包
先运行一下工程
点击工程右键->export->Runable jar file->选择“server-HelloServer”(若是没有的话先运行一下工程就有了)->finish
5.测试
把jar上传到linux(192.168.13.120)下
执行:
 
在另外一台机器(192.168.13.105)上测试:
1)1个连接,10万次请求
  1. ab -n 100000 http://192.168.13.120:8080/
测试结果:
  1. Concurrency Level: 1
  2.  
  3. Time taken for tests: 117.718673 seconds
  4.  
  5. Complete requests: 100000
  6.  
  7. Failed requests: 0
  8.  
  9. Write errors: 0
  10.  
  11. Total transferred: 39000000 bytes
  12.  
  13. HTML transferred: 6400000 bytes
  14.  
  15. Requests per second: 849.48 [#/sec] (mean)
  16.  
  17. Time per request: 1.177 [ms] (mean)
  18.  
  19. Time per request: 1.177 [ms] (mean, across all concurrent requests)
  20.  
  21. Transfer rate: 323.53 [Kbytes/sec] received
server的负载
  1. load average: 0.07, 0.02, 0.00
2)100个连接并发.10万次请求
  1. ab -n 100000 -c 100 http://192.168.13.120:8080/
  1. Concurrency Level: 100
  2.  
  3. Time taken for tests: 9.999371 seconds
  4.  
  5. Complete requests: 100000
  6.  
  7. Failed requests: 0
  8.  
  9. Write errors: 0
  10.  
  11. Total transferred: 39003900 bytes
  12.  
  13. HTML transferred: 6400640 bytes
  14.  
  15. Requests per second: 10000.63 [#/sec] (mean)
  16.  
  17. Time per request: 9.999 [ms] (mean)
  18.  
  19. Time per request: 0.100 [ms] (mean, across all concurrent requests)
  20.  
  21. Transfer rate: 3809.14 [Kbytes/sec] received
server负载
  1. load average: 0.62, 0.19, 0.06
3)1000个连接并发,10万次请求
  1. ab -n 100000 -c 1000 http://192.168.13.120:8080/
结果:
  1. Concurrency Level: 1000
  2.  
  3. Time taken for tests: 19.697403 seconds
  4.  
  5. Complete requests: 100000
  6.  
  7. Failed requests: 0
  8.  
  9. Write errors: 0
  10.  
  11. Total transferred: 39000390 bytes
  12.  
  13. HTML transferred: 6400064 bytes
  14.  
  15. Requests per second: 5076.81 [#/sec] (mean)
  16.  
  17. Time per request: 196.974 [ms] (mean)
  18.  
  19. Time per request: 0.197 [ms] (mean, across all concurrent requests)
  20.  
  21. Transfer rate: 1933.55 [Kbytes/sec] received
负载:
  1. load average: 1.76, 0.43, 0.14
结论:mina2.0在合理的负载下1000个并发,最高能达到5076.81rps(Requests per second),基本上能满足需求
end