Netty实战
Posted therunningfish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Netty实战相关的知识,希望对你有一定的参考价值。
一、Netty异步和事件驱动
1.Java网络编程回顾
socket.accept 阻塞
socket.setsockopt /非阻塞
2.NIO异步非阻塞
a).nio 非阻塞的关键时使用选择器(java.nio.channels.Selector)来实现;可以监控多个socket读写的完成状态来协调其他socket的读写,以提高资源使用率;
b).异步事件驱动,socket请求发起,立即响应,后端执行处理请求,处理完毕通知客户端;
c).Channel 链接实体的连接,请求和响应的载体;Netty通过回调来处理事件,时间触发->ChannelHandler->channelActive()来处理事件;
d).ChannelFuture通过ChannelFutureListener来监听事件提供通知处理完成的结果;Future是需要手动去获取结果;每个I/O操作都会立即返回一个ChannelFuture,不会阻塞,后台处理I/O,至于何时处理完成,异步时间驱动通知;
3.Nttey的异步变成模型建立在Future和回调的基础之上,再将时间派发到ChannelHandler之上进行处理;
二、Netty Demo
1.EchoServerHandler
package chap01; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; @ChannelHandler.Sharable public class EchoServerHandler extends ChannelInboundHandlerAdapter @Override public void channelRead(ChannelHandlerContext ctx,Object msg) ByteBuf in = (ByteBuf) msg; System.out.println("Server received: "+ in.toString(CharsetUtil.UTF_8)); ctx.write(in); @Override public void channelReadComplete(ChannelHandlerContext ctx) ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); @Override public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) cause.printStackTrace(); ctx.close();
2.EchoServer
package chap01; 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.SocketChannel; import io.netty.channel.socket.nio.NioserverSocketChannel; import java.net.InetSocketAddress; public class EchoServer private final int port; public EchoServer(int port) this.port = port; public static void main(String[] args) throws Exception // if (args.length !=-1) // System.err.println("Usage: " + EchoServer.class.getSimpleName()+"<port>"); // // int port = Integer.parseInt(args[0]); // new EchoServer(port).start(); new EchoServer(9001).start(); public void start() throws Exception final EchoServerHandler serverHandler = new EchoServerHandler(); EventLoopGroup group = new NioEventLoopGroup(); try ServerBootstrap b = new ServerBootstrap(); b.group(group).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() @Override public void initChannel(SocketChannel ch) throws Exception ch.pipeline().addLast(serverHandler); ); ChannelFuture f = b.bind().sync(); f.channel().closeFuture().sync(); finally group.shutdownGracefully().sync();
3.EchoClientHandler
package chap01; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.util.CharsetUtil; public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> @Override public void channelActive(ChannelHandlerContext ctx) ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8)); @Override public void channelRead0(ChannelHandlerContext ctx,ByteBuf in) System.out.println("Client received: "+in.toString(CharsetUtil.UTF_8)); @Override public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) cause.printStackTrace(); ctx.close();
4.EchoClient
package chap01; import io.netty.bootstrap.Bootstrap; 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.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import java.net.InetSocketAddress; public class EchoClient private final String host; private final int port; public EchoClient(String host,int port) this.host = host; this.port = port; public void start() throws Exception EventLoopGroup group = new NioEventLoopGroup(); try Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host,port)).handler(new ChannelInitializer<SocketChannel>() @Override public void initChannel(SocketChannel ch) throws Exception ch.pipeline().addLast(new EchoClientHandler()); ); ChannelFuture f = b.connect().sync(); finally group.shutdownGracefully().sync(); public static void main(String[] args) throws Exception // if (args.length!=2) // System.err.println("Usage: "+EchoClient.class.getSimpleName() + "<host><port>"); // return; // // String host = args[0]; // int port = Integer.parseInt(args[1]); // new EchoClient(host,port).start(); new EchoClient("127.0.0.1",9001).start();
以上是关于Netty实战的主要内容,如果未能解决你的问题,请参考以下文章