通过简单的例子演示Netty进行TCP请求,解释Netty运行的过程
使用Maven环境引入Netty依赖
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.43.Final</version>
</dependency>
</dependencies>
服务端
使用Netty编写服务端,端口号6668
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;
public class NettyService {
public static void main(String[] args) throws InterruptedException {
// 创建BossGroup 处理连接请求
EventLoopGroup bossGroup = new NioEventLoopGroup();
// 创建WorkerGroup 处理业务处理
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// 创建服务器端启动对象,配置启动参数。
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)// 设置两个线程组
.channel(NioServerSocketChannel.class) // 使用NIOSocketChannel 作为服务器的通道实现
.option(ChannelOption.SO_BACKLOG, 128) // 设置线程队列等待连接的个数
.childOption(ChannelOption.SO_KEEPALIVE, true) // 设置保持活动的连接状态
.childHandler(new ChannelInitializer<SocketChannel>() { // 创建一个通道初始化对象
// 给pipeline设置处理器
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new NettyServiceHandler());
}
}); // 给workerGroup的EventGroup对应的管道设置处理器
// 以上——————服务器已经准备好了。
ChannelFuture cf = serverBootstrap.bind(6668).sync();
// 对关闭通道进行监听
cf.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
其中使用的服务端Handler为自定义Handler
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* 自定义一个handler需要集成Netty规定好的某个HandlerAdapter
*/
public class NettyServiceHandler extends ChannelInboundHandlerAdapter {
// 重写方法
/**
* 数据读取
*
* @param ctx:上下文对象 包含 管道pipeline,通道channel,地址
* @param msg:客户端发送的数据,默认为Object类型
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 将 msg 转成一个ByteBuf ByteBuf是Netty提供的,
ByteBuf buf = (ByteBuf) msg;
System.out.print("地址:" + ctx.channel().remoteAddress());
System.out.println("客户端发送的消息是:" + buf.toString(CharsetUtil.UTF_8));
}
/**
* 数据读取完毕
*
* @param ctx
* @throws Exception
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// 写入并刷新缓存区 write + flush
// 对发送的数据编码
ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端", CharsetUtil.UTF_8));
}
/**
* 处理异常
*
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// 关闭通道
ctx.close();
}
}
客户端模拟数据的发送
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
// 需要一个事件循环组
NioEventLoopGroup group = new NioEventLoopGroup();
try {
// 创建一个启动对象
Bootstrap bootstrap = new Bootstrap();
// 设置相关参数
bootstrap.group(group) // 设置线程组
.channel(NioSocketChannel.class) // 设置客户端通道的实现类
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new NettyClientHandler() ); // 加入自己的处理器
}
});
// 以上————客户端设置好了————
// 启动客户端连接服务器
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6668).sync();
// 对通道的关闭进行监听,异步关闭
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}
客户端handler如下
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
/**
* 当通道就绪触发该方法
*
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("hello,Service ", CharsetUtil.UTF_8));
}
/**
* 当通道有读取事件时触发
*
* @param ctx
* @param msg
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf = (ByteBuf) msg;
System.out.print("服务器地址:" + ctx.channel().remoteAddress());
System.out.println("服务器回复的消息:" + byteBuf.toString(CharsetUtil.UTF_8));
}
/**
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}