Nio原生Api很复杂
Nio存在epoll bug,会产生空轮询,导致cpu被占用100%
Netty是基于Nio进行包装,性能上会更高
Netty学习成本和使用成本更低
摘抄自其他文章, Netty的优点总结:
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);
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容