netty是什么?
netty是一个nio的开发框架,他可以让开发者快速的搭建一个基于nio的服务器。
nio是什么
nio也叫非阻塞式io而BIO叫做阻塞式io。NIO是对BIO的升华。在过去我们使用网络传输协议时,如果没有连接,则这个线程会一直处于等待的状态,而不会继续执行后面的代码。对于java虚拟机来说,线程是一个很珍贵的资源,为了充分利用线程资源,于是非阻塞式IO应运而生。
非阻塞式IO可以允许线程不用等待用户的连接而去处理后面的内容。这样就会让我们线程利用率更加高效。
NIO代码如下
package com.study.hc.net.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
/**
* 直接基于非阻塞的写法
*/
public class NIOServer {
public static void main(String[] args) throws Exception {
// 创建网络服务端
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false); // 设置为非阻塞模式
serverSocketChannel.socket().bind(new InetSocketAddress(8080)); // 绑定端口
System.out.println("启动成功");
while (true) {
SocketChannel socketChannel = serverSocketChannel.accept(); // 获取新tcp连接通道
// tcp请求 读取/响应
if (socketChannel != null) {
System.out.println("收到新连接 : " + socketChannel.getRemoteAddress());
socketChannel.configureBlocking(false); // 默认是阻塞的,一定要设置为非阻塞
try {
ByteBuffer requestBuffer = ByteBuffer.allocate(1024);
while (socketChannel.isOpen() && socketChannel.read(requestBuffer) != -1) {
// 长连接情况下,需要手动判断数据有没有读取结束 (此处做一个简单的判断: 超过0字节就认为请求结束了)
if (requestBuffer.position() > 0) break;
}
if(requestBuffer.position() == 0) continue; // 如果没数据了, 则不继续后面的处理
requestBuffer.flip();
byte[] content = new byte[requestBuffer.limit()];
requestBuffer.get(content);
System.out.println(new String(content));
System.out.println("收到数据,来自:"+ socketChannel.getRemoteAddress());
// 响应结果 200
String response = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 11\r\n\r\n" +
"Hello World";
ByteBuffer buffer = ByteBuffer.wrap(response.getBytes());
while (buffer.hasRemaining()) {
socketChannel.write(buffer);// 非阻塞
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 用到了非阻塞的API, 在设计上,和BIO可以有很大的不同.继续改进
}
}
但是我们会发现在接受数据的时候会存在一个一个问题,似乎这个线程一次只能有一个用户连接,这就是又是一个问题了。这里我们可以使用一个类似于池的东西,使用List将用户的请求创建的channel放入到一个list,然后我们在后续的处理中,我们便利这个list中的channel。图如下
在nio中我们并不是使用一个List进行遍历而是为我们提供了一个叫做selector的类。这个类可以理解为一个注册中心,他比list强大多了。它可以让channel在这里进行注册,当这个channel收到对应的请求时,他会通知这个channel进行处理。代码如下
public class TonyNioHttpServer {
public static Selector selector;
// 定义线程池
public static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(25, 25, 25,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
private static ServerSocketChannel socketChannel;
private static final int port = 8080;
public static void main(String[] args) throws Exception {
// serversocket
socketChannel = ServerSocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.bind(new InetSocketAddress(port));
System.out.println("NIO启动:" + port);
// 获取一个选择器
// 底层的事件通知机制
// 老板娘 selector
TonyNioHttpServer.selector = Selector.open();
// 登记: 表示对这个通道上OP_ACCEPT事件感兴趣,并且返回一个标记
// 此处表示,希望收到socket通道8080端口上建立连接这个通知
SelectionKey selectionKey = socketChannel.register(TonyNioHttpServer.selector, 0);
selectionKey.interestOps(selectionKey.OP_ACCEPT);
while (true) { // 带几个美女,坐在大厅
// 如果没有新的socket与服务器有连接或者是数据交互,这里就会等待1秒
TonyNioHttpServer.selector.select(1000);
// 开始处理
Set<SelectionKey> selected = TonyNioHttpServer.selector.selectedKeys();
Iterator<SelectionKey> iter = selected.iterator();
while (iter.hasNext()) {
// 获取注册在上面标记
SelectionKey key = iter.next();
if (key.isAcceptable()) { // 判断是否OP_ACCEPT的通知
// 处理连接
System.out.println("有新的连接啦,当前线程数量:"
+ TonyNioHttpServer.threadPoolExecutor.getActiveCount());
// 有新的连接,赶紧接客
SocketChannel chan = socketChannel.accept();
// 问一下价格多少,需要什么样服务...
chan.configureBlocking(false);
// 注册一个新监听。
// 表示希望收到该连接上OP_READ数据传输事件的通知
chan.register(TonyNioHttpServer.selector, SelectionKey.OP_READ);
} else if (key.isReadable()) { // OP_READ
// 取出附着在上面的信息,也就是上面代码中附着的连接信息
SocketChannel socketChannel = (SocketChannel) key.channel();
// 处理中,不需要收到任何通知
key.cancel();
// tomcat 大保健旗舰店 有200技师,只有付钱的客户才会享受技师 泰式、保shen,
socketChannel.configureBlocking(false);
TonyNioHttpServer.threadPoolExecutor.execute(() -> {
try {
// 读取里面的内容,请注意,此处大小随意写的。
// tomcat中会根据Http协议中定义的长度来读取数据,或者一直读到通道内无数据为止
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
socketChannel.read(byteBuffer);
byteBuffer.flip(); // 转为读模式
String request = new String(byteBuffer.array());
System.out.println("收到新数据,当前线程数:"
+ TonyNioHttpServer.threadPoolExecutor.getActiveCount()
+ ",请求内容:" + request);
// 给一个当前时间作为返回值
// 随意返回,不是Http的协议
byteBuffer.clear();
ByteBuffer wrap = ByteBuffer
.wrap(("tony" + System.currentTimeMillis()).getBytes());
socketChannel.write(wrap);
wrap.clear();
socketChannel.configureBlocking(false);
// 注册一个新监听。 表示希望收到该连接上OP_READ事件的通知
socketChannel.register(TonyNioHttpServer.selector,
SelectionKey.OP_READ);
} catch (Exception e) {
// e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 服务器线程处理完毕,当前线程数:"
+ threadPoolExecutor.getActiveCount());
});
}
// 取出后删除
iter.remove();
}
selected.clear();
// 过掉cancelled keys
TonyNioHttpServer.selector.selectNow();
}
}
}
到了这里你可能会发现上面代码还是存在问题,没错就是上面图中的等待部分,如果用户并不给我们发送信息,那我们岂不是卡死了?没错,该如何解决,当然是使用多线程将该部分进行一次分离,这样我们就可以让这个main线程只处理接收消息,也就演化成了一个accept线程
代码如下
public class TonyNioHttpServer {
public static Selector selector;
// 定义线程池
public static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(25, 25, 25,
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
private static ServerSocketChannel socketChannel;
private static final int port = 8080;
public static void main(String[] args) throws Exception {
// serversocket
socketChannel = ServerSocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.bind(new InetSocketAddress(port));
System.out.println("NIO启动:" + port);
// 获取一个选择器
// 底层的事件通知机制
// 老板娘 selector
TonyNioHttpServer.selector = Selector.open();
// 登记: 表示对这个通道上OP_ACCEPT事件感兴趣,并且返回一个标记
// 此处表示,希望收到socket通道8080端口上建立连接这个通知
SelectionKey selectionKey = socketChannel.register(TonyNioHttpServer.selector, 0);
selectionKey.interestOps(selectionKey.OP_ACCEPT);
while (true) { // 带几个美女,坐在大厅
// 如果没有新的socket与服务器有连接或者是数据交互,这里就会等待1秒
TonyNioHttpServer.selector.select(1000);
// 开始处理
Set<SelectionKey> selected = TonyNioHttpServer.selector.selectedKeys();
Iterator<SelectionKey> iter = selected.iterator();
while (iter.hasNext()) {
// 获取注册在上面标记
SelectionKey key = iter.next();
if (key.isAcceptable()) { // 判断是否OP_ACCEPT的通知
// 处理连接
System.out.println("有新的连接啦,当前线程数量:"
+ TonyNioHttpServer.threadPoolExecutor.getActiveCount());
// 有新的连接,赶紧接客
SocketChannel chan = socketChannel.accept();
// 问一下价格多少,需要什么样服务...
chan.configureBlocking(false);
// 注册一个新监听。
// 表示希望收到该连接上OP_READ数据传输事件的通知
chan.register(TonyNioHttpServer.selector, SelectionKey.OP_READ);
} else if (key.isReadable()) { // OP_READ
// 取出附着在上面的信息,也就是上面代码中附着的连接信息
SocketChannel socketChannel = (SocketChannel) key.channel();
// 处理中,不需要收到任何通知
key.cancel();
// tomcat 大保健旗舰店 有200技师,只有付钱的客户才会享受技师 泰式、保shen,
socketChannel.configureBlocking(false);
TonyNioHttpServer.threadPoolExecutor.execute(() -> {
try {
// 读取里面的内容,请注意,此处大小随意写的。
// tomcat中会根据Http协议中定义的长度来读取数据,或者一直读到通道内无数据为止
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
socketChannel.read(byteBuffer);
byteBuffer.flip(); // 转为读模式
String request = new String(byteBuffer.array());
System.out.println("收到新数据,当前线程数:"
+ TonyNioHttpServer.threadPoolExecutor.getActiveCount()
+ ",请求内容:" + request);
// 给一个当前时间作为返回值
// 随意返回,不是Http的协议
byteBuffer.clear();
ByteBuffer wrap = ByteBuffer
.wrap(("tony" + System.currentTimeMillis()).getBytes());
socketChannel.write(wrap);
wrap.clear();
socketChannel.configureBlocking(false);
// 注册一个新监听。 表示希望收到该连接上OP_READ事件的通知
socketChannel.register(TonyNioHttpServer.selector,
SelectionKey.OP_READ);
} catch (Exception e) {
// e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 服务器线程处理完毕,当前线程数:"
+ threadPoolExecutor.getActiveCount());
});
}
// 取出后删除
iter.remove();
}
selected.clear();
// 过掉cancelled keys
TonyNioHttpServer.selector.selectNow();
}
}
}
此时的结构就变成了下图
这时我们的数据收发已经分离出了我们接收的线程,我们就可以继续接收了,你数据的发送和接收自己去玩吧,对我没有影响了。当然对于数据的收发这里,我们也可以在进行线程的创建从而使数据处理更加的高效。
ps:为了大家观感舒服,所以借用了老师的代码,感谢老师教导