netty的性能非常高,能达到8000rps以上,见
1.准备好需要的jar包
spring.jar//spring包
netty-3.2.4.Final.jar// netty库
commons-dbcp.jar// dbcp数据库连接池
mysql-connector-java-5.1.6.jar// dbcp数据库连接池需要依赖
commons-logging.jar//spring.jar需要依赖
commons-pool.jar// dbcp数据库连接池需要依赖
2.新建java工程TestNettyServer
2.1导入netty的例子
HttpServer.java
packageorg.jboss.netty.example.http.snoop;
importjava.net.InetSocketAddress;
importjava.util.concurrent.Executors;
importorg.jboss.netty.bootstrap.ServerBootstrap;
importorg.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
publicclassHttpServer {
publicstaticvoidmain(String[] args) {
// Configure the server.
ServerBootstrap bootstrap =newServerBootstrap(
newNioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Set up the event pipeline factory.
bootstrap.setPipelineFactory(newHttpServerPipelineFactory());
// Bind and start to accept incoming connections.
bootstrap.bind(newInetSocketAddress(8081));
}
}
HttpServerPipelineFactory.java
packageorg.jboss.netty.example.http.snoop;
importstaticorg.jboss.netty.channel.Channels.*;
importorg.jboss.netty.channel.ChannelPipeline;
importorg.jboss.netty.channel.ChannelPipelineFactory;
importorg.jboss.netty.handler.codec.http.HttpContentCompressor;
importorg.jboss.netty.handler.codec.http.HttpRequestDecoder;
importorg.jboss.netty.handler.codec.http.HttpResponseEncoder;
publicclassHttpServerPipelineFactoryimplementsChannelPipelineFactory {
publicChannelPipeline getPipeline()throwsException {
// Create a default pipeline implementation.
ChannelPipeline pipeline = pipeline();
// Uncomment the following line if you want HTTPS
//SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
//engine.setUseClientMode(false);
//pipeline.addLast("ssl", new SslHandler(engine));
pipeline.addLast("decoder",newHttpRequestDecoder());
// Uncomment the following line if you don't want to handle HttpChunks.
//pipeline.addLast("aggregator", new HttpChunkAggregator(1048576));
pipeline.addLast("encoder",newHttpResponseEncoder());
// Remove the following line if you don't want automatic content compression.
pipelin