本文仅适用与Netty4.0.32版本,其余版本是否适用表示并不清楚...bootstrap
Netty服务器启动流程:缓存
一、建立线程池服务器
建立处理链接的线程池:bossGroup
建立处理全部事件的线程池:workerGroupsocket
EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup();
二、设定辅助启动类。ServerBootStrap
传入1中开辟的线程池
指定链接该服务器的channel类型
指定须要执行的childHandler
设置部分参数,如AdaptiveRecvByteBufAllocator缓存大小
.Option用于设置bossGroup相关参数
.childOption用于设置workerGroup相关参数ide
2.五、此处可处理一个问题:超长字符串在服务端handler没法被一次接收完
可经过此句进行设置:.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, MAX_LENGTH_OF_MSG, 65536))oop
ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class)//设置channel类型 .childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, MAX_LENGTH_OF_MSG, 65536)) .childHandler(new childChannelHandler());//选择执行handler
此处的MAX_LENGTH_OF_MSG必须为2的次幂,否则确定不会是你设置的那个值,具体会变成什么,源码还没看,等看了再补充...编码
2.7五、构建Handler处理流程spa
样例以下:.net
public class childChannelHandler extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel ch) throws Exception { //TODO 添加各类功能handler 消息加解密,消息规范检测,构建返回码 ch.pipeline().addLast(new NettyServerHandler()); } }
当要添加多个handler时,就必须注意添加的顺序。线程
这里的handler分为两种类型:
一种继承ChannelInboundHandler,用于处理来自客户端的消息,好比对客户端的消息进行解码,读取等等。该类型在pipeline中的执行顺序与添加顺序一致。
一种继承ChannelOutboundHandler,用于处理即将发往客户端的消息,好比对该消息进行编辑,编码等等。该类型在pipeline中的执行顺序与添加顺序相反。
并且ChannelOutboundHandler的全部handler,放在ChannelInboundHandler下面是执行不到的。
好比:
public class childChannelHandler extends ChannelInitializer<SocketChannel>{ @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new OutboundHandler1()); //handler1 ch.pipeline().addLast(new OutboundHandler2()); //handler2 ch.pipeline().addLast(new InboundHandler1()); //handler3 ch.pipeline().addLast(new InboundHandler2()); //handler4 } }
以上4个handler的实际执行顺序分别为handler3 -> handler4 -> handler2 ->handler1
若是在handler4下方加上OutboundHandler3,那么这个handler是不会被执行到的。
三、同步等待绑定指定端口
此处可屡次执行bind语句绑定多个端口
ChannelFuture channelFuture = serverBootstrap.bind(8080).sync(); channelFuture = serverBootstrap.bind(8081).sync(); ...
四、同步等待服务器关闭信息
channelFuture.channel().closeFuture().sync();
五、最后关闭此前开辟的两个线程池
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
最后整段服务器代码以下:
package Netty; 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; import io.netty.channel.AdaptiveRecvByteBufAllocator; public class NettyServer { public void startServerInPort(int port) throws Exception{ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try{ //设置启动辅助类 ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class)//设置channel类型 .childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 2048, 65536)) .childHandler(new childChannelHandler());//选择执行handler //阻塞等待服务器彻底启动 ChannelFuture channelFuture = serverBootstrap.bind(port).sync(); channelFuture.channel().closeFuture().sync(); }finally{ bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public class childChannelHandler extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel ch) throws Exception { //TODO 添加各类功能handler 消息加解密,消息规范检测,构建返回码 ch.pipeline().addLast(new NettyServerHandler()); } } }
客户端的这部分代码和服务器端差很少,就不另开一文啰嗦了。之间贴代码:
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; public class NettyClient { public void sendMsgToServer() throws Exception{ EventLoopGroup group = new NioEventLoopGroup(); try{ //设置辅助启动类信息 Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class)//选择channel类型 .option(ChannelOption.TCP_NODELAY, true) .handler(new childChannelHandler()); //阻塞等待成功链接服务器 ChannelFuture channelFuture = bootstrap.connect(localhost,8000).sync(); //阻塞等待来自服务器的处理结果 channelFuture.channel().closeFuture().sync(); }finally{ group.shutdownGracefully(); } } private class childChannelHandler extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel ch) throws Exception { //TODO 添加其余功能处理Handler,如消息加解密 ch.pipeline().addLast(new NettyClientHandler()); } } }