本文仅适用与Netty4.0.32版本,其他版本是否适用表示并不清楚...
Netty服务器启动流程:
1、创建线程池
创建处理连接的线程池:bossGroup
创建处理所有事件的线程池:workerGroup
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
2、设定辅助启动类。ServerBootStrap
传入1中开辟的线程池
指定连接该服务器的channel类型
指定需要执行的childHandler
设置部分参数,如AdaptiveRecvByteBufAllocator缓存大小
.Option用于设置bossGroup相关参数
.childOption用于设置workerGroup相关参数
2.5、此处可处理一个问题:超长字符串在服务端handler无法被一次接收完
可通过此句进行设置:.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, MAX_LENGTH_OF_MSG, 65536))
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.75、构建Handler处理流程
样例如下:
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是不会被执行到的。
3、同步等待绑定指定端口
此处可多次执行bind语句绑定多个端口
ChannelFuture channelFuture = serverBootstrap.bind(8080).sync();
channelFuture = serverBootstrap.bind(8081).sync();
...
4、同步等待服务器关闭信息
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());
}
}
}
注:原文请参考 http://www.cnblogs.com/mochenmochen/p/5826062.html