介绍:
Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。
使用:
hadoop、dubbo、akka等分布式功能框架
io和NIO区别:
NIO面向缓冲区,非阻塞(是通过选择器实现的)
IO面向流,是阻塞的。
1.pom文件
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha1</version>
</dependency>
2.服务端
public class nettyService {
public static void main(String[] args) throws InterruptedException {
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);//TCP的缓冲区设置
serverBootstrap.option(ChannelOption.SO_SNDBUF, 32 * 1024);//设置发送缓冲的大小
serverBootstrap.option(ChannelOption.SO_RCVBUF, 32 * 1024);//设置接收缓冲区大小
serverBootstrap.option(ChannelOption.SO_KEEPALIVE, true);//保持连续
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());//拆包粘包定义结束字符串(第一种解决方案)
sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf));//在管道中加入结束字符串
sc.pipeline().addLast(new StringDecoder());//定义接收类型为字符串把ByteBuf转成String
sc.pipeline().addLast(new ServerHandler());//在这里配置具体数据接收方法的处理
}
});
ChannelFuture future = serverBootstrap.bind(8765).sync();//绑定端口
future.channel().closeFuture().sync();//等待关闭(程序阻塞在这里等待客户端请求)
bossGroup.shutdownGracefully();//关闭线程
workerGroup.shutdownGracefully();//关闭线程
}
}
3.服务端处理类
public class ServerHandler extends ChannelHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String) msg;
System.out.println("" + body);//前面已经定义了接收为字符串,这里直接接收字符串就可以
//服务端给客户端的响应
String response = body + "我收到了 你好啊!$_";//发送的数据以定义结束的字符串结尾
ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));//发送必须还是ByteBuf类型
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
4.客户端
public class Client {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup worker = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(worker)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("$_".getBytes());//拆包粘包定义结束字符串(第一种解决方案)
sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf));//在管道中加入结束字符串
sc.pipeline().addLast(new StringDecoder());//定义接收类型为字符串把ByteBuf转成String
sc.pipeline().addLast(new ClientHandler());//在这里配置具体数据接收方法的处理
}
});
ChannelFuture f = b.connect("127.0.0.1", 8765).sync();// 连接端口
Scanner scanner = new Scanner(System.in);
System.out.println("请输入发送的消息:");
String next = scanner.next() + "$_";
f.channel().writeAndFlush(Unpooled.copiedBuffer(next.getBytes()));// 发送信息给server
f.channel().closeFuture().sync();
worker.shutdownGracefully();
}
}
5.客户端处理类
public class ClientHandler extends ChannelHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
System.out.println("1" + msg.toString());
} finally {
ReferenceCountUtil.release(msg);//释放缓冲区
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}