一,为什么选择Netty
Netty 是业界最流行的NIO框架之一,它的健壮性、功能、性能、可定制性和可扩展性在同类框架中都是屈指可数的,它已经得到成百上千的商用项目的验证。
Netty 具有如下优点 :
1、API 使用简单,开发门槛低
2、 功能强大 ,预置了多种编码功能,支持多种主流协议
3、定制能力强,可以通过ChannelHandler 对通信框架进行灵活地扩展
4、性能高,通过与其他业界主流框架对比,Netty的综合性能最优
5、成熟,稳定,Netty 已经修复了已经发现的所有JDK NIO BUG
正是因为这些优点,Netty 逐渐成为Java NIO 编程的首选框架 ,当然在我们学习Netty之前,我们必须先了解 Java NIO 。
二、NIO入门
网络编程的基本模型是Client/Server 模型, 也就是两个进程之间进行相互通信,其中服务端提供位置信息,客户端通过连接操作服务端监听的地址发起请求连接,通过三次握手建立连接,如果连接成功,双方就可以通过网络套接字(Socket)进行通信。在这一小节,我们分别对JDK 的 BIO 、NIO 和 JDK1.7 提供的 NIO2.0 的使用进行说明,通过代码,体会到随着Java I/O 类库的不断发展和改进。
1、传统的 BIO 编程 :
通过上图可以了解BIO的服务端通信模型:采用BIO通信模型的服务端,通常由一个独立的Accept 线程负责监听客户端的连接,它接受到客户端连接后,为每个客户端创建一个新的线程进行链路处理,处理完成后,通过输出流返回给客户端,线程销毁。下面通过代码进行分析 。
TimeServer :
public class TimeServer {
public static void main(String[] args) {
int port = 8088;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
Socket socket = null;
while (true) {
socket = serverSocket.accept();
// 开启线程
new Thread(new TimeServerHandler(socket)).start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
System.out.println(" TimeServer closing");
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public class TimeServerHandler implements Runnable {
private Socket socket;
public TimeServerHandler(Socket socket) {
this.socket = socket;
}
public void run() {
BufferedReader reader = null;
PrintWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
String currentTime = null;
String body = null;
while (true) {
body = reader.readLine();
if (body == null)
break;
System.out.println(" ===== the thread is "+Thread.currentThread().getName());
Thread.sleep(2000);
System.out.println("The timeServer receive order : "+body);
currentTime = "Query time order".equals(body) ? new Date().toString() : "Bad_Query";
writer.println(currentTime);
}
} catch (Exception e) {
e.printStackTrace();
if (reader!=null){
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (writer!=null){
writer.close();
}
} finally {
if (this.socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
TimeClient:
public class TimeClient {
public static void main(String[] args) {
int port = 8088;
Socket socket = null;
Socket socket1 = null;
BufferedReader reader = null;
PrintWriter writer = null;
BufferedReader reader1 = null;
PrintWriter writer1 = null;
try {
socket = new Socket("127.0.0.1", port);
socket1 = new Socket("127.0.0.1", port);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(),true);
reader1 = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
writer1 = new PrintWriter(socket1.getOutputStream(),true);
writer.println("Query time order");
writer1.println("Query time order");
System.out.println("Send order to server succeed");
System.out.println(" ===== Send order to server succeed");
String resp = reader.readLine();
String resp1 = reader1.readLine();
System.out.println("Received time is :" + resp);
System.out.println("===== Received time is :" + resp1);
} catch (Exception e) {
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (writer != null) {
writer.close();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader1 != null) {
try {
reader1.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (writer1 != null) {
writer1.close();
}
if (socket1 != null) {
try {
socket1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
} catch (Exception e) { } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (writer != null) { writer.close(); } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (reader1 != null) { try { reader1.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (writer1 != null) { writer1.close(); } if (socket1 != null) { try { socket1.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
输出结果如下 :
通过对结果的分析 可以得出 : 虽然服务端对两个客户端连接的请求处理是并行的 ,但是BIOTimeServer:
accept Socket[addr=/127.0.0.1,port=39915,localport=8088]
accept Socket[addr=/127.0.0.1,port=39916,localport=8088]
===== the thread is Thread-0
===== the thread is Thread-1
// 时隔两秒
The timeServer receive order : Query time order
The timeServer receive order : Query time order
主要问题在于有一个新的客户端请求接入时,服务端必须创建一个新的线程处理,在高性能服务器应用领域下,往往有成千上万个客户端并发连接,这种模型显然无法满足。
2、 NIO 模型、
在了解NIO 之前,我们先介绍几个概念 :
(1)、缓冲区(Buffer): Buffer 是一个对象 ,它包含一些要写入或者要读出的数据。缓冲区实质上就是一个数组,但是提供了对数据的结构化访问以及维护读写位置(limit)等信息。
(2)、通道 (Channel) : 通道可以通过它读取和写入数据,它就像自来水管一样。通道与流不同的事通道是双向的,流只是在一个方向上移动,而通道可以用于读、写或者同时读写。
(3)、多路复用器(Selector) : Selector 是NIO 编程的基础。Selector 具有选择已经就绪的任务功能,Selector 会不断的轮询注册在其上的Channel ,如果某个Channel 上面有新的TCP 连接接入 、读和写事件,这个Channel 就会处于就绪状态,会被Selector轮询出来,然后通过SelectionKey 可以获取就绪Channel的集合 ,进行相应的 I/O 操作。一个Selector 可以同时轮询多个Channel。
NIO 服务端序列图 :
NIO 客户端序列图 :
上代码 :
TImerServer :
public class NIOTimeServer {
public static void main(String[] args) {
int port = 8080;
MultiplexerTimeServer multiplexerTimeServer = new MultiplexerTimeServer(port);
new Thread(multiplexerTimeServer,"NIO-MultiplexerTimeServer-001").start();
}
}
public class MultiplexerTimeServer implements Runnable {
private ServerSocketChannel serverSocketChannel;
private Selector selector;
// 记录是否停止 volatile : 使该字段能在线程之间通信
private volatile Boolean stop =false ;
public MultiplexerTimeServer(int port) {
try {
// 创建多路复用
selector = Selector.open();
// 打开 serverSocketChannel ,用于监听客户端的连接 ,他是所有客户端连接的父管道
serverSocketChannel = ServerSocketChannel.open();
// 绑定监听的端口 ,设置连接为非阻塞模式
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(port), 1024);
// 将serverSocketChannel 注册到 Selector ,监听 ACCEPT事件
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("The timeServer is start in port : " + port);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public void stop() {
this.stop = true;
}
public void run() {
// 启动线程
while (!stop) {
try {
// Selects a set of keys whose corresponding channels are ready for I/O operations
selector.select(1000);
// 获取已经准备就绪的 Key
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
SelectionKey key = null;
// 循环 对 Key 进行 I/O 操作
while (iterator.hasNext()) {
key = iterator.next();
iterator.remove();
try {
// I/O c操作
handleInput(key);
} catch (Exception e) {
if (key != null) {
key.cancel();
if (key.channel() != null) {
key.channel().close();
}
}
}
}
} catch (Throwable t) {
t.printStackTrace();
}
}
if (selector != null) {
try {
selector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void handleInput(SelectionKey key) throws IOException {
// 判断 key 是否有效
if (key.isValid()) {
// 处理新接入的请求 判断是否可操作
if (key.isAcceptable()) {
ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
// 注册 selector 监听read 事件
sc.register(selector, SelectionKey.OP_READ);
}
if (key.isReadable()) {
SocketChannel sc = (SocketChannel) key.channel();
// 字节缓冲区
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = sc.read(readBuffer);
if (readBytes > 0) {
/* 官方解释: 在一系列read或者put 操作后 ,执行用来准备接下来的 write 或者get操作
After a sequence of channel-read or <i>put</i> operations, invoke
* this method to prepare for a sequence of channel-write or relative
* <i>get</i> operations. */
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
// 将 ByteBuffer 内容写入 bytes
readBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println(" The timeServer receive order : " + body);
String currentTime = "Query Time Order".equals(body) ? new Date().toString()
: "Bad query";
// 将 响应消息 写回客户端
doWrite(sc, currentTime);
} else if (readBytes < 0) {
key.cancel();
sc.close();
} else {
}
}
}
}
private void doWrite(SocketChannel sc, String resp) throws IOException {
if (resp != null && resp.trim().length() > 0) {
byte[] bytes = resp.getBytes();
ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);
writeBuffer.put(bytes);
writeBuffer.flip();
sc.write(writeBuffer);
if (!writeBuffer.hasRemaining()){
System.out.println(resp);
}
}
}
}
TimeClient ;
public class NIOTimeClient {
public static void main(String[] args) {
int port = 8080;
new Thread(new TimeClientHandler("127.0.0.1",port)).start();
}
}
public class TimeClientHandler implements Runnable {
private String host;
private int port;
private Selector selector;
private SocketChannel socketChannel;
protected volatile Boolean stop = false;
public TimeClientHandler(String host, int port) {
this.host = host;
this.port = port;
try {
selector = Selector.open();
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public void stop() {
this.stop = true;
}
public void run() {
try {
doConnect();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
while (!stop) {
try {
selector.select(1000);
/*获得所有当前已经准备好的SelectionKey,SelectionKey中包含事件类型
包含每个事件相应的SocketChannel*/
Set<SelectionKey> selectKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key= iterator.next();
iterator.remove();
try{
handleInput(key);
}catch (Exception e){
e.printStackTrace();
if (key!=null){
key.cancel();
if (key.channel()!=null){
System.out.println("==");
key.channel().close();
}
}
}
}
stop();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
if (selector!=null){
try {
selector.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
private void handleInput(SelectionKey key) throws IOException {
if (key.isValid()) {
if (key.isConnectable()) {
if (socketChannel.finishConnect()) {
SocketChannel socketChannel = (SocketChannel) key.channel();
doWrite(socketChannel);
socketChannel.register(this.selector,SelectionKey.OP_READ);
} else {
System.exit(1);
}
System.out.println(key.isReadable());
// if (key.isReadable()) {
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
int readBytes = socketChannel.read(readBuffer);
if (readBytes > 0) {
readBuffer.flip();
byte[] bytes = new byte[readBuffer.remaining()];
readBuffer.get(bytes);
String body = new String(bytes, "UTF-8");
System.out.println("NOw is " + body);
} else if (readBytes < 0) {
key.cancel();
socketChannel.close();
} else {
}
// }
}
}
}
private void doConnect() throws IOException {
if (socketChannel.connect(new InetSocketAddress(host, port))) {
socketChannel.register(selector, SelectionKey.OP_READ);
doWrite(socketChannel);
} else {
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
}
private void doWrite(SocketChannel socketChannel)throws IOException {
byte[] req = "Query Time Order".getBytes();
ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);
writeBuffer.put(req);
writeBuffer.flip();
socketChannel.write(writeBuffer);
if (!writeBuffer.hasRemaining()){
System.out.println("Send order to server succeed ");
}
}
}
相应的操作代码上都有注释(Server 和 Cilent 大部分相同)。
注意 : JDK NIO 臭名昭著的 epoll bug ,它会导致Selector 空轮询 ,最终导致CPU 100% 。在大多数场景下,不建议直接使用NIO 。