Nio原生Api很复杂
Nio存在epoll bug,会产生空轮询,导致cpu被占用100%
Netty是基于Nio进行包装,性能上会更高
Netty学习成本和使用成本更低
摘抄自其他文章, Netty的优点总结:
通过对Netty的分析,我们将它的优点总结如下。 ◎ API使用简单,开发门槛低; ◎ 功能强大,预置了多种编解码功能,支持多种主流协议; ◎ 定制能力强,可以通过ChannelHandler对通信框架进行灵活地扩展; ◎ 性能高,通过与其他业界主流的NIO框架对比,Netty的综合性能最优; ◎ 成熟、稳定,Netty修复了已经发现的所有JDK NIO BUG,业务开发人员不需要再为NIO的BUG而烦恼; ◎ 社区活跃,版本迭代周期短,发现的BUG可以被及时修复,同时,更多的新功能会加入; ◎ 经历了大规模的商业应用考验,质量得到验证。Netty在互联网、大数据、网络游戏、企业应用、电信软件等众多行业已经得到了成功商用,证明它已经完全能够满足不同行业的商业应用了。 正是因为这些优点,Netty逐渐成为了Java NIO编程的首选框架。
NettyServer
package com.itmayiedu.demo;
import io.netty.bootstrap.ServerBootstrap;
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.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
/**
* 搭建NettyServer
*
*/
public class NettyServerDemo {
static final int port = 8080;
public static void main(String[] args) {
// 1. 创建两个线程池,分别作为boss线程池和work线程池
// boss线程池
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
// work线程池
NioEventLoopGroup workGroup = new NioEventLoopGroup();
// 创建NettyServer
ServerBootstrap serverBootstrap = new ServerBootstrap();
// 将线程池加入到serverBootStrap中 设置为服务端: NioServerSocketChannel.class
serverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class)
// 添加消息处理
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new ServerHandler());
}
});
try{
ChannelFuture channelFuture = serverBootstrap.bind(port);
System.out.println("服务端已经启动,port: " + port);
channelFuture.channel().closeFuture().sync();
}catch(Exception e) {
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
ServerHandler
package com.itmayiedu.demo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.CharsetUtil;
public class ServerHandler extends SimpleChannelInboundHandler {
/**
* 服务端回复数据的方法
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for (int i = 0; i < 10; i++) {
ctx.writeAndFlush(Unpooled.copiedBuffer("每天学习一小时\n", CharsetUtil.UTF_8));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
super.exceptionCaught(ctx, cause);
}
// 读取消息的方法
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf = (ByteBuf) msg;
if (msg instanceof HttpRequest) {
String request = byteBuf.toString(CharsetUtil.UTF_8);
System.out.println("服务端接收到消息:" + request);
}
}
}
NettyClient
package com.itmayiedu.demo;
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;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.io.IOException;
import java.net.InetSocketAddress;
public class NettyClientDemo {
public static void main(String[] args) throws IOException {
NioEventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress("127.0.0.1", 8080))
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new ClientHandler());
}
});
try{
ChannelFuture sync = bootstrap.connect().sync();
sync.channel().closeFuture().sync();
}catch (Exception e) {
}finally {
group.shutdownGracefully();
}
}
}
ClientHandler
package com.itmayiedu.demo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
public class ClientHandler extends SimpleChannelInboundHandler {
/**
* 发送消息的方法
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for (int i = 0; i < 10; i++) {
ctx.writeAndFlush(Unpooled.copiedBuffer("每天平均学习多久?\n", CharsetUtil.UTF_8));
}
}
/**
* 获取消息的方法
* @param ctx
* @param msg
* @throws Exception
*/
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf = (ByteBuf) msg;
String request = byteBuf.toString(CharsetUtil.UTF_8);
System.out.println("客户端接收到消息: " + request);
}
}