Netty费阻塞式多线程网络神器的使用(tcp)
1.在pom文件中引入netty
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.51.Final</version>
</dependency>
2.tcp服务端搭建
EventLoopGroup boosGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap boot = new ServerBootstrap();
boot.group(boosGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.localAddress(8085)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new EchoHandler());
socketChannel.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture future = boot.bind().sync();
future.channel().closeFuture().sync();
System.out.println("服务端启动");
} catch (Exception e) {
e.printStackTrace();
} finally {
boosGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
2.实现EchoHandler方法
class EchoHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf in = (ByteBuf) msg;
System.out.println(in.toString(CharsetUtil.UTF_8));
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
3.tcp客户端搭建
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap boot = new Bootstrap();
boot.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new EchoClientHandler());
socketChannel.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture future = boot.connect("localhost", 8085).sync();
future.channel().closeFuture().sync();
System.out.println("客户端启动");
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
4.实现EchoClientHandler方法
class EchoClientHandler extends ChannelInboundHandlerAdapter {
private final ByteBuf msg;
EchoClientHandler() {
msg = Unpooled.buffer(256);
msg.writeBytes("hello netty".getBytes(CharsetUtil.UTF_8));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
ctx.writeAndFlush(msg);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(((ByteBuf) msg).toString(CharsetUtil.UTF_8));
ctx.write(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
super.channelReadComplete(ctx);
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
cause.printStackTrace();
ctx.close();
}
}