目录
Netty - 入门
1. Netty开发环境的搭建
使用maven构建项目:
io.netty netty 3.9.4.Final
2. Time程序的开发
2.1. TimeServer开发
TimeServer:
public class TimeServer { public static void main(String[] args) { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (NumberFormatException e) { // } } new TimeServer().bind(port); } private void bind(int port) { //配置服务端的NIO线程组 /* Reactor线程组 bossGroup: 服务端接受客户端连接 workerGroup: 进行SocketChannel的网络读写 */ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { //ServerBootstarp: 用于启动NIO服务端的辅助启动类 ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup); /* 创建的Channel伪NioServerSocketChannel 对应JDK NIO中的ServerSocketChannel */ b.channel(NioServerSocketChannel.class); b.option(ChannelOption.SO_BACKLOG, 1024); //IO事件处理类 b.childHandler(new ChildChannelHandler()); //绑定端口,同步等待成功 ChannelFuture f = b.bind(port).sync(); //等待服务端监听端口关闭 f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); } } private class ChildChannelHandler extends ChannelInitializer{ @Override public void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new TimeServerHandler()); } }}
TimeServerHandler:
public class TimeServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { //将消息发送队列中的消息写入到SocketChannel中发送给对方 /** * 调用write()方法只是把待发送的消息放到发送缓冲数组中, * 调用flush()方法,将发送缓冲区中的消息全部写到SocketChannel中 */ ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { /* ByteBuf类似JDK中的java.nio.ByteBuffer ByteBuf.readableBytes: 获取缓冲区可读的字节数 ByteBuf.readBytes: 将缓冲区的字节数组复制到新建的byte数组中 */ //将msg转换为Netty的ByteBuf对象 ByteBuf buf = (ByteBuf) msg; //创建一个字节数组 byte[] req = new byte[buf.readableBytes()]; //将buf中的内容复制到字节数组 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); }}
2.2. TimeClient开发
TimeClient:
public class TimeClient { public void connect(int port, String host) throws Exception{ //配置客户端NIO线程组 EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group); b.channel(NioSocketChannel.class); b.option(ChannelOption.TCP_NODELAY, true); b.handler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHandler()); } }); //发起异步连接请求 ChannelFuture f = b.connect(host, port).sync(); //等待客户端链路关闭 f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if (args != null && args.length > 0){ try { port = Integer.valueOf(args[0]); }catch (NumberFormatException e){ // 采用默认值 } } new TimeClient().connect(port, "127.0.0.1"); }}
TimeClientHandler:
public class TimeClientHandler extends ChannelInboundHandlerAdapter { 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 { //释放资源 System.out.println("Unexpected exception from downstram: " + cause.getMessage()); ctx.close(); }}
3. 总结
Server端
创建一个Reactor线程组
EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();
创建一个用于启动NIO服务的辅助启动类
ServerBootstrap b = new ServerBootstrap();
设置参数
// 设置主从“线程池”b.group(bossGroup, workerGroup);// 指定Channel通道的类型b.channel(NioServerSocketChannel.class);// 设置一些参数b.option(ChannelOption.SO_BACKLOG, 1024);// 设置子通道SocketChannel的处理类b.childHandler(new ChildChannelHandler());
绑定并监听某个端口
ChannelFuture f = b.bind(port).sync();
端口关闭,服务停止
f.channel().closeFuture().sync();
Client端
创建一个Reactor线程组
EventLoopGroup group = new NioEventLoopGroup();
创建一个辅助启动类实例
Bootstrap b = new Bootstrap();
设置参数
//初始化线程池b.group(group);//指定Channel通道的类型b.channel(NioSocketChannel.class);//设置一些参数b.option(ChannelOption.TCP_NODELAY, true);//设置SocketChannel的处理器b.handler(new ChannelInitializer
() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHandler()); }}); 发起一个异步连接请求,连接指定的服务地址
ChannelFuture f = b.connect(host, port).sync();
端口关闭,服务停止
f.channel().closeFuture().sync();
从上面的一些分析可以看出,服务端和客户端最基础的配置都是5个步骤。先创建一个线程组EventLoopGroup
和辅助启动类(服务端为ServerBootstrap
,客户端为Bootstrap
),接着设置一系列的参数:线程池、通道类型、通道参数、处理器等,最后启动服务,服务端为bind()
监听,客户端为connect()
连接。
具体的处理类需要继承ChannelHandlerAdapter
,这里存在一个问题,《Netty权威指南》第2版使用的Netty版本为5.x,但现在已经废弃,所以使用的是4.x的版本。书中原来继承的是ChannelHandlerAdapter
,在4.x的版本中这个接口并没有channelRead
方法,所以用channelInboundHandlerAdapter
代替。
在TimeClientHandler
中,当客户端和服务端TCP链路建立成功后,NIO线程会调用channelActive
方法,发送内容给服务端。当服务端返回应答消息时,会调用channelRead
方法。
在channelRead
中,用到了一个很重要的Netty中的类:ByteBuf
。ByteBuf是一个很好的经过优化的数据容器。ByteBuf有两部分:一个用于读,一个用于写。我们可以按顺序读取数据,并且可以跳到开始再读一遍。具体的内容可以参考这个博文: