netty的性能非常高,能达到8000rps以上,见

各个web服务器的性能对比测试

1.准备好需要的jar包

  1. spring.jar            //spring包
  2. netty-3.2.4.Final.jar   // netty库
  3. commons-dbcp.jar    // dbcp数据库连接池
  4. mysql-connector-java-5.1.6.jar // dbcp数据库连接池需要依赖
  5. commons-logging.jar  //spring.jar需要依赖
  6. commons-pool.jar   // dbcp数据库连接池需要依赖

2.新建java工程TestNettyServer

2.1导入netty的例子
HttpServer.java

  1. package org.jboss.netty.example.http.snoop;
  2. import java.net.InetSocketAddress;
  3. import java.util.concurrent.Executors;
  4. import org.jboss.netty.bootstrap.ServerBootstrap;
  5. import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
  6. public class HttpServer {
  7. public static void main(String[] args) {
  8. // Configure the server.
  9. ServerBootstrap bootstrap = new ServerBootstrap(
  10. new NioServerSocketChannelFactory(
  11. Executors.newCachedThreadPool(),
  12. Executors.newCachedThreadPool()));
  13. // Set up the event pipeline factory.
  14. bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
  15. // Bind and start to accept incoming connections.
  16. bootstrap.bind(new InetSocketAddress(8081));
  17. }
  18. }

HttpServerPipelineFactory.java

  1. package org.jboss.netty.example.http.snoop;
  2. import static org.jboss.netty.channel.Channels.*;
  3. import org.jboss.netty.channel.ChannelPipeline;
  4. import org.jboss.netty.channel.ChannelPipelineFactory;
  5. import org.jboss.netty.handler.codec.http.HttpContentCompressor;
  6. import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
  7. import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
  8. public class HttpServerPipelineFactory implements ChannelPipelineFactory {
  9. public ChannelPipeline getPipeline() throws Exception {
  10. // Create a default pipeline implementation.
  11. ChannelPipeline pipeline = pipeline();
  12. // Uncomment the following line if you want HTTPS
  13. //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
  14. //engine.setUseClientMode(false);
  15. //pipeline.addLast("ssl", new SslHandler(engine));
  16. pipeline.addLast("decoder"new HttpRequestDecoder());
  17. // Uncomment the following line if you don't want to handle HttpChunks.
  18. //pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
  19. pipeline.addLast("encoder"new HttpResponseEncoder());
  20. // Remove the following line if you don't want automatic content compression.
  21. pipeline.addLast("deflater"new HttpContentCompressor());
  22. pipeline.addLast("handler"new HttpRequestHandler());
  23. return pipeline;
  24. }
  25. }

HttpRequestHandler.java

  1. package org.jboss.netty.example.http.snoop;
  2. import static org.jboss.netty.handler.codec.http.HttpHeaders.*;
  3. import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.*;
  4. import static org.jboss.netty.handler.codec.http.HttpResponseStatus.*;
  5. import static org.jboss.netty.handler.codec.http.HttpVersion.*;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import java.util.Set;
  10. import org.jboss.netty.buffer.ChannelBuffer;
  11. import org.jboss.netty.buffer.ChannelBuffers;
  12. import org.jboss.netty.channel.ChannelFuture;
  13. import org.jboss.netty.channel.ChannelFutureListener;
  14. import org.jboss.netty.channel.ChannelHandlerContext;
  15. import org.jboss.netty.channel.ExceptionEvent;
  16. import org.jboss.netty.channel.MessageEvent;
  17. import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
  18. import org.jboss.netty.handler.codec.http.Cookie;
  19. import org.jboss.netty.handler.codec.http.CookieDecoder;
  20. import org.jboss.netty.handler.codec.http.CookieEncoder;
  21. import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
  22. import org.jboss.netty.handler.codec.http.HttpChunk;
  23. import org.jboss.netty.handler.codec.http.HttpChunkTrailer;
  24. import org.jboss.netty.handler.codec.http.HttpRequest;
  25. import org.jboss.netty.handler.codec.http.HttpResponse;
  26. import org.jboss.netty.handler.codec.http.QueryStringDecoder;
  27. import org.jboss.netty.util.CharsetUtil;
  28. /**
  29.    * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
  30.    * @author Andy Taylor (andy.taylor@jboss.org)
  31.    * @author <a href="http://gleamynode.net/">Trustin Lee</a>
  32.    *
  33.    * @version $Rev: 2368 $, $Date: 2010-10-18 17:19:03 +0900 (Mon, 18 Oct 2010) $
  34.    */
  35. public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
  36. private HttpRequest request;
  37. private boolean readingChunks;
  38. /** Buffer that stores the response content */
  39. private final StringBuilder buf = new StringBuilder();
  40. @Override
  41. public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
  42. if (!readingChunks) {
  43. HttpRequest request = this.request = (HttpRequest) e.getMessage();
  44. if (is100ContinueExpected(request)) {
  45. send100Continue(e);
  46. }
  47. buf.setLength(0);
  48. buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n");
  49. buf.append("===================================\r\n");
  50. buf.append("VERSION: " + request.getProtocolVersion() + "\r\n");
  51. buf.append("HOSTNAME: " + getHost(request, "unknown") + "\r\n");
  52. buf.append("REQUEST_URI: " + request.getUri() + "\r\n\r\n");
  53. for (Map.Entry<String, String> h: request.getHeaders()) {
  54. buf.append("HEADER: " + h.getKey() + " = " + h.getValue() + "\r\n");
  55. }
  56. buf.append("\r\n");
  57. QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
  58. Map<String, List<String>> params = queryStringDecoder.getParameters();
  59. if (!params.isEmpty()) {
  60. for (Entry<String, List<String>> p: params.entrySet()) {
  61. String key = p.getKey();
  62. List<String> vals = p.getValue();
  63. for (String val : vals) {
  64. buf.append("PARAM: " + key + " = " + val + "\r\n");
  65. }
  66. }
  67. buf.append("\r\n");
  68. }
  69. if (request.isChunked()) {
  70. readingChunks = true;
  71. else {
  72. ChannelBuffer content = request.getContent();
  73. if (content.readable()) {
  74. buf.append("CONTENT: " + content.toString(CharsetUtil.UTF_8) + "\r\n");
  75. }
  76. writeResponse(e);
  77. }
  78. else {
  79. HttpChunk chunk = (HttpChunk) e.getMessage();
  80. if (chunk.isLast()) {
  81. readingChunks = false;
  82. buf.append("END OF CONTENT\r\n");
  83. HttpChunkTrailer trailer = (HttpChunkTrailer) chunk;
  84. if (!trailer.getHeaderNames().isEmpty()) {
  85. buf.append("\r\n");
  86. for (String name: trailer.getHeaderNames()) {
  87. for (String value: trailer.getHeaders(name)) {
  88. buf.append("TRAILING HEADER: " + name + " = " + value + "\r\n");
  89. }
  90. }
  91. buf.append("\r\n");
  92. }
  93. writeResponse(e);
  94. else {
  95. buf.append("CHUNK: " + chunk.getContent().toString(CharsetUtil.UTF_8) + "\r\n");
  96. }
  97. }
  98. }
  99. private void writeResponse(MessageEvent e) {
  100. // Decide whether to close the connection or not.
  101. boolean keepAlive = isKeepAlive(request);
  102. // Build the response object.
  103. HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
  104. response.setContent(ChannelBuffers.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
  105. response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
  106. if (keepAlive) {
  107. // Add 'Content-Length' header only for a keep-alive connection.
  108. response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes());
  109. }
  110. // Encode the cookie.
  111. String cookieString = request.getHeader(COOKIE);
  112. if (cookieString != null) {
  113. CookieDecoder cookieDecoder = new CookieDecoder();
  114. Set<Cookie> cookies = cookieDecoder.decode(cookieString);
  115. if(!cookies.isEmpty()) {
  116. // Reset the cookies if necessary.
  117. CookieEncoder cookieEncoder = new CookieEncoder(true);
  118. for (Cookie cookie : cookies) {
  119. cookieEncoder.addCookie(cookie);
  120. }
  121. response.addHeader(SET_COOKIE, cookieEncoder.encode());
  122. }
  123. }
  124. // Write the response.
  125. ChannelFuture future = e.getChannel().write(response);
  126. // Close the non-keep-alive connection after the write operation is done.
  127. if (!keepAlive) {
  128. future.addListener(ChannelFutureListener.CLOSE);
  129. }
  130. }
  131. private void send100Continue(MessageEvent e) {
  132. HttpResponse response = new DefaultHttpResponse(HTTP_1_1, CONTINUE);
  133. e.getChannel().write(response);
  134. }
  135. @Override
  136. public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
  137. throws Exception {
  138. e.getCause().printStackTrace();
  139. e.getChannel().close();
  140. }
  141. }

DatabaseUtil.java

  1. package org.jboss.netty.example.http.snoop;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import org.apache.commons.dbcp.BasicDataSource;
  8. /**
  9.  * 连接和使用数据库资源的工具类
  10.  * 
  11.  * @author yifangyou
  12.  * @version gtm 2010-09-27
  13.  */
  14. public class DatabaseUtil {
  15. /**
  16.      * 数据源
  17.      */
  18. private BasicDataSource dataSource;
  19. /**
  20.      * 数据库连接
  21.      */
  22. public Connection conn;
  23. /**
  24.      * 获取数据源
  25.      * @return 数据源
  26.      */
  27. public BasicDataSource getDataSource() {
  28. return dataSource;
  29. }
  30. /**
  31.      * 设置数据源
  32.      * @param dataSource 数据源
  33.      */
  34. public void setDataSource(BasicDataSource dataSource) {
  35. this.dataSource = dataSource;
  36. }
  37. /**
  38.      * 获取数据库连接
  39.      * @return conn
  40.      */
  41. public Connection getConnection() {
  42. try {
  43. conn = dataSource.getConnection();
  44. catch (Exception e) {
  45. e.printStackTrace();
  46. return null;
  47. }
  48. return conn;
  49. }
  50. /**
  51.      * 关闭数据库连接
  52.      * @param conn
  53.      */
  54. public void closeConnection(Connection conn) {
  55. if (null != conn) {
  56. try {
  57. conn.close();
  58. conn = null;
  59. catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. }
  64. /**
  65.      * 获取执行SQL的工具
  66.      * @param conn 数据库连接
  67.      * @param sql SQL语句
  68.      * @return prepStmt
  69.      */
  70. public PreparedStatement getPrepStatement(Connection conn, String sql) {
  71. PreparedStatement prepStmt = null;
  72. try {
  73. prepStmt = conn.prepareStatement(sql);
  74. catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. return prepStmt;
  78. }
  79. /**
  80.      * 关闭数据库资源
  81.      * @param prepStmt
  82.      */
  83. public void closePrepStatement(PreparedStatement prepStmt) {
  84. if (null != prepStmt) {
  85. try {
  86. prepStmt.close();
  87. prepStmt = null;
  88. catch (SQLException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. }

2.2 导入jar包
在工程下添加lib目录
把jar包拷进去

点击工程的右键“propertis”->Java Build Path->Libraries->Add JARS

3.分析如何注入
Netty的运行过程是

因此我们需要
Spring注入入口在HttpServer里的main函数
把HttpRequestHandler注入到HttpServerPipelineFactory,
把HttpServerPipelineFactory注入到HttpServer
另外我们在HttpRequestHandler需要用到mysql连接池,因此还要把mysql连接池注入到HttpRequestHandler