Netty是什么
Netty是一個高性能的異步的,基於事件驅動的NIO框架,它是JBOSS提供的一個開源框架,用以快速開發高性能,高可靠性的網絡服務器和客戶端程序。
netty的架構
Netty官網
https://netty.io/index.html 這里可以找到jar包或者maven依賴
類似框架
Apache 的 Mina
java和netty
Java使用netty,建議jdk版本為1.5以后的,這是netty官網摘錄的
開始-------------
我使用maven構建的項目,所以就使用maven依賴,也可以普通項目導入jar。
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.6.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>4.1.34.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
<version>4.1.7.Final</version>
</dependency>
在maven項目的pom.xml文件中添加即可
創建一個 DiscardServer 類
代碼如下
package com.demo.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
/**
* @ Author : yjp
* @ Date : 2019/5/7 20:56
* @ Version : 1.0
* @ Desciption :
*/
public class DiscardServer {
private int port;
public DiscardServer(int port) {
this.port = port;
}
public void run(){
ServerBootstrap bootstrap = new ServerBootstrap();
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(8);
//設置管道
bootstrap.channel(NioServerSocketChannel.class);
//處理pipeline
bootstrap.group(bossGroup, workerGroup);
bootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());//設置http報文解碼
ch.pipeline().addLast("http-response-encoder", new HttpResponseEncoder());//設置http報文編碼
ch.pipeline().addLast("http-servlet", new DiscardServerHandler());
}
});
try {
ChannelFuture sync = bootstrap.bind(port).sync();
System.out.println("服務啟動----------------------");
sync.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) {//啟動測試
new DiscardServer(8080).run();
}
}
業務處理類 DiscardServerHandler
package com.demo.netty;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
/**
* @ Author : yjp
* @ Date : 2019/5/7 20:51
* @ Version : 1.0
* @ Desciption :
*/
public class DiscardServerHandler extends SimpleChannelInboundHandler {//也可以在main函數類里面,可以寫成內部類的形式。
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
//設置響應
DefaultFullHttpResponse defaultFullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
//設置響應頭
defaultFullHttpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8");
//設置響應內容
defaultFullHttpResponse.content().writeBytes("第一次測試netty框架".getBytes());
//把內容加入管道
channelHandlerContext.writeAndFlush(defaultFullHttpResponse).addListener(ChannelFutureListener.CLOSE);
}
}
運行main函數
因為模擬的是http請求,所以打開瀏覽器輸入localhost:8080
成功。