[toc]
Netty中分隔符×××代码示例与分析
通过特别×××的使用,可以解决Netty中TCP的粘包问题,上一篇《Netty中LineBasedFrameDecoder×××使用与分析:解决TCP粘包问题》通过行×××的使用来解决TCP粘包问题,这意味着Netty的服务端和客户端在每次发送请求消息前,都需要在消息的尾部拼接换行符。
除了使用行×××外,Netty还提供了通用的分隔符×××,即可以自定义消息的分隔符,那就是DelimiterBasedFrameDecoder
分隔符×××。
下面的示例代码来自于《Netty权威指南》第5章,这个例子模拟的是echo服务,即类似于ping,客户端向服务端echo一个消息,服务端就会进行回应。
仍然需要注意的是,代码做了部分修改,也加入了个人的一些注释和分析。
服务端
EchoServer.java
package cn.xpleaf.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
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.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
public class EchoServer {
public void bind(int port) throws Exception {
// 配置服务端的NIO线程组
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 ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// 构建DelimiterBasedFrameDecoder×××的分隔符
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
// 添加DelimiterBasedFrameDecoder×××到pipeline中
// 1024表示单条消息的最大长度,当达到该长度后仍然没有查找到分隔符,就抛出TooLongFrameExceptio异常
// 防止由于异常码流缺失分隔符导致的内存溢出,这是Netty×××的可靠性保护
// 第二个参数就是分隔符缓冲对象
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
// 添加StringDecoder×××,将ByteBuf解码成字符串对象
ch.pipeline().addLast(new StringDecoder());
// 添加业务处理handler
ch.pipeline().addLast(new EchoServerHandler());
}
});
// 绑定端口,同步等待成功
ChannelFuture f = b.bind(port).sync();
// 等待服务端监听端口关闭
f.channel().closeFuture().sync();
} finally {
// 优雅退出,释放线程池资源
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 8080;
if(args != null && args.length > 0) {
try {
port = Integer.valueOf(port);
} catch (NumberFormatException e) {
// TODO: handle exception
}
}
new EchoServer().bind(port);
}
}
EchoServerHandler.java
package cn.xpleaf.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
private int counter = 0;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String) msg;
// counter的作用是标记这是第几次收到客户端的请求
System.out.println("This is : " + ++counter + " times receive client : [" + body + "]");
// 由于设置DelimiterBasedFrameDecoder过滤掉了分隔符,
// 所以返回给客户端时需要在请求消息尾部拼接分隔符"$_"
body += "$_";
ByteBuf echo = Unpooled.copiedBuffer(body.getBytes());
ctx.write(echo);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// 发生异常,关闭链路
ctx.close();
}
}
客户端
EchoClient.java
package cn.xpleaf.netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
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;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
public class EchoClient {
public void connect(int port, String host) throws Exception {
// 配置客户端NIO线程组
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 ch) throws Exception {
// 构建DelimiterBasedFrameDecoder×××的分隔符
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
// 添加DelimiterBasedFrameDecoder×××到pipeline中
// 1024表示单条消息的最大长度,当达到该长度后仍然没有查找到分隔符,就抛出TooLongFrameExceptio异常
// 防止由于异常码流缺失分隔符导致的内存溢出,这是Netty×××的可靠性保护
// 第二个参数就是分隔符缓冲对象
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
// 添加StringDecoder×××,将ByteBuf解码成字符串对象
ch.pipeline().addLast(new StringDecoder());
// 添加业务处理handler
ch.pipeline().addLast(new EchoClientHandler());
}
});
// 发起异步连接操作
ChannelFuture f = b.connect(host, port).sync();
// 等待客户端链路关闭
f.channel().closeFuture().sync();
} finally {
// 优雅退出,释放NIO线程组
group.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 8080;
if(args != null && args.length > 0) {
try {
port = Integer.valueOf(port);
} catch (NumberFormatException e) {
// 采用默认值
}
}
new EchoClient().connect(port, "localhost");
}
}
EchoClientHandler.java
package cn.xpleaf.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class EchoClientHandler extends ChannelInboundHandlerAdapter {
private int counter;
static final String ECHO_REQ = "Hi, xpleaf. Welcome to Netty.$_";
public EchoClientHandler() {
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
// 构建ByteBuf对象
ByteBuf req = Unpooled.copiedBuffer(ECHO_REQ.getBytes());
// 每次调用ctx.write()写入ByteBuf对象时,必须要重新构建一个ByteBuf对象
// 即不能对一个ByteBuf对象多次使用ctx.write(),否则会发生异常
// 向服务端发送10次请求
for(int i = 0; i < 10; i++) {
ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes()));
// ctx.writeAndFlush(req);
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String) msg;
// counter的作用是标记这是第几次收到服务端的回应
System.out.println("This is : " + ++counter + " times receive server : [" + body + "]");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
测试
服务端输出:
This is : 1 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 2 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 3 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 4 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 5 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 6 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 7 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 8 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 9 times receive client : [Hi, xpleaf. Welcome to Netty.]
This is : 10 times receive client : [Hi, xpleaf. Welcome to Netty.]
客户端输出:
This is : 1 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 2 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 3 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 4 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 5 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 6 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 7 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 8 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 9 times receive server : [Hi, xpleaf. Welcome to Netty.]
This is : 10 times receive server : [Hi, xpleaf. Welcome to Netty.]
转载于:https://blog.51cto.com/xpleaf/2071237