### NettyServer开发示例
```
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* @program: learn
* @author: Robert
* @create: 2018-06-28
**/
public class NettyServer {
public void bind(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChildChannelHandler());
ChannelFuture channelFuture = bootstrap.bind(port).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
private class ChildChannelHandler extends ChannelInitializer {
@Override
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(new ServerChannelHandler());
}
}
public static void main(String[] args) {
new NettyServer().bind(8089);
}
}
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.time.LocalDateTime;
/**
* @program: learn
* @author: Robert
* @create: 2018-06-28
**/
public class ServerChannelHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ReadHandler.read(msg);
String now = LocalDateTime.now().toString();
ByteBuf buffer = Unpooled.copiedBuffer(now.getBytes());
ctx.write(buffer);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
```
![](https://img.kancloud.cn/cb/4c/cb4cacaddb5c4b43215cb6cb7a8f47ed_700x398.png)
### NettyClient
```
import io.netty.bootstrap.Bootstrap;
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.NioSocketChannel;
/**
* @program: learn
* @author: Robert
* @create: 2018-06-28
**/
public class NettyClient {
public void connect(String host, int port) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.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 ClientChannelHandler());
}
});
ChannelFuture future = bootstrap.connect(host, port).sync();
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) {
new NettyClient().connect("localhost", 8089);
}
}
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
/**
* @program: learn
* @author: Robert
* @create: 2018-06-28
**/
public class ClientChannelHandler extends ChannelInboundHandlerAdapter {
private ByteBuf message;
public ClientChannelHandler() {
byte[] bytes = "hello,world".getBytes();
message = Unpooled.buffer(bytes.length);
message.writeBytes(bytes);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(message);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.err.println("read response");
ReadHandler.read(msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
```
- 概述
- Netty&Tomcat的区别
- NIO基础知识
- 同步阻塞式IO
- 伪异步IO编程
- 同步IO之IO-multiplexing
- NIO基础概念
- NIO服务流程
- Netty基础知识
- NettyServer开发示例
- 零拷贝
- TCP粘包和拆包问题
- LineBasedFrameDecoder&StringDecoder
- 应用层消息处理方式
- ByteBuf
- ChannelHandler
- Netty核心组件
- Channel接口
- ChannelHandler
- ChannelInboundHandlerAdapter
- SimpleChannelInboundHandler
- SimpleChannelInboundHandler && ChannelInboundHandler
- ChannelInitializer
- EventLoop接口
- ChannelFuture接口
- ChannelPipeline接口
- 序列化
- JAVA序列化