一、不選擇Java原生NIO編程的原因
(1)NIO的類庫和API復雜,使用麻煩,你需要熟練掌握Selector、ServerSocketChannel、SocketChannel、ByteBuffer等
(2)需要具備其他的額外技能做鋪墊,例如熟悉Java多線程編程。這是因為NIO編程涉及到Reactor模式,你必須對多線程和網絡編程非常熟悉,才能編寫出高質量NIO程序。
(3)可靠性能力補齊,工作量和難度都非常大。例如客戶端面臨斷鏈重連、網絡閃斷、半包讀寫、失敗緩存、網絡擁塞和異常碼流的處理等問題,NIO編程的特點是功能開發相對容易,但是可靠性能力補齊的工作量和難度非常大。
(4)JDK NIO的BUG,例如臭名昭著的epoll bug,它會導致Selector空輪詢,最終導致CPU 100%。官方聲稱在JDK 1.6 版本的update 18修復了該問題,但是直到JDK 1.7版本該問題仍舊存在,只不過該BUG發生概率降低了一些而已,它並沒有得到根本性解決。
二、Netty 優點
(1)API使用簡單,開發門檻低
(2)功能強大,預置了多種編解碼功能,支持多種主流協議
(3)定制能力強,可以通過ChannelHandler 對通信框架進行靈活地擴展
(4)性能高,通過與其他業界主流的NIO框架對比,Netty的綜合性能最優
(5)成熟、穩定,Netty修復了已經發現的所有JDK NIO BUG,業務開發人員不需要再為NIO的BUG而煩惱
(6)社區活躍,版本迭代周期短,發現的BUG可以被及時修復,同時,更多的新功能會加入
(7)經歷了大規模的商業應用考驗,質量得到驗證。Netty 在互聯網、大數據、網絡游戲、企業應用、電信軟件的 眾多行業已經得到了成功商業,證明它已經完全能夠滿足不同行業的商業應用了
三、Netty服務端Demo
import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; /** * Netty 時間服務器服務端TimeServer */ public class TimeServer { public void bind(int port) { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler()); //綁定端口,同步等待成功 ChannelFuture f = b.bind(port).sync(); //等待服務端監聽端口關閉 f.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } finally { //優雅退出,釋放線程池資源 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } private class ChildChannelHandler extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new TimeServerHandler()); } } public static void main(String[] args) { int port = 8080; new TimeServer().bind(port); } } import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import java.util.Date; public class TimeServerHandler extends ChannelHandlerAdapter{ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req,"UTF-8"); System.out.println("The time server receive order : "+body); String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)?new Date(System.currentTimeMillis()).toString():"BAD ORDER"; ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes()); ctx.write(resp); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } }
四、Netty的客戶端Demo
import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; /** * Created by ThinkPad on 2019/6/17. */ public class TimeClient { public void connect(int port,String host) { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY,true).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new TimeClientHandler()); } }); //發起異步連接操作 ChannelFuture f = b.connect(host,port).sync(); //等待客戶端連接操作 f.channel().closeFuture().sync(); }catch (Exception e) { e.printStackTrace(); }finally { //優雅退出,釋放NIO線程組 group.shutdownGracefully(); } } public static void main(String[] args) { int port = 8080; new TimeClient().connect(port,"127.0.0.1"); } } import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; /** * Created by ThinkPad on 2019/6/17. */ public class TimeClientHandler extends ChannelHandlerAdapter{ private final ByteBuf firstMessage; public TimeClientHandler() { byte[] req = "QUERY TIME ORDER".getBytes(); firstMessage = Unpooled.buffer(req.length); firstMessage.writeBytes(req); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(firstMessage); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf)msg; byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req,"UTF-8"); System.out.println("Now is : "+body); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } }
五、測試運行結果
先運行Netty 服務端,然后在運行Netty 客戶端
運行結果如下: