NIO非阻塞式网络编程

Java 1.4 开始提供了新的非阻塞 API,用意是替代 Java IO 和 Java Networking 相关的 API。
NIO 中有三个核心组件Buffer 缓冲区Channel 通道Selector 选择器

1. Buffer

缓冲区本质上是一个可以写入数据的内存块,也可以再次读取。此内存块包含在Buffer对象中,该对象提供了一组方法,可以轻松地使用内存块(数组操作的封装)。

Buffer 有三个重要的属性
  1. capacity 容量
    作为一个内存块,buffer 具有一个固定的大小,成为“容量”。
  2. position 位置
    写入模式时代表写数据的位置,读取模式时代表读数据的位置。
  3. limit 限制
    写入模式,限制等同于 buffer 的容量。读取模式,限制等同于写入的数据量。

以下是 Buffer 操作的例子

public class BufferDemo {
    public static void main(String[] args) {
        // 构建一个 byte 字节缓冲区,容量是4
        ByteBuffer buffer = ByteBuffer.allocate(4);
        // 默认是写入模式,查看三个重要的属性
        System.out.println(String.format("初始化,容量:%s,位置:%s,限制:%s", buffer.capacity(), buffer.position(), buffer.limit()));
        // 写入3字节数据
        buffer.put((byte)1);
        buffer.put((byte)2);
        buffer.put((byte)3);
        // 再次查看三个属性
        System.out.println(String.format("写入3字节后,容量:%s,位置:%s,限制:%s", buffer.capacity(), buffer.position(), buffer.limit()));
        // 开始读取数据(不转入读取模式也是可以读取的,但是 position 记录的位置会不对)
        System.out.println("开始读取");
        buffer.flip();
        byte a = buffer.get();
        System.out.println(a);
        byte b = buffer.get();
        System.out.println(b);
        System.out.println(String.format("读取两字节后,容量:%s,位置:%s,限制:%s", buffer.capacity(), buffer.position(), buffer.limit()));
        // 下面将 buffer 填满
        // 若是在读取模式下写入,只能覆盖一条数据
        // clear() 方法清除整个缓冲区,compact() 方法仅清除已阅读的数据。转为写入模式
        buffer.compact();
        buffer.put((byte)3);
        buffer.put((byte)4);
        buffer.put((byte)5);
        System.out.println(String.format("最后的状态,容量:%s,位置:%s,限制:%s", buffer.capacity(), buffer.position(), buffer.limit()));
    }
}

打印结果为

初始化,容量:4,位置:0,限制:4
写入3字节后,容量:4,位置:3,限制:4
开始读取
1
2
读取两字节后,容量:4,位置:2,限制:3
最后的状态,容量:4,位置:4,限制:4
ByteBuffer 内存类型

ByteBuffer 提供了直接内存(direct堆外)非直接内存(heap堆)两种实现。
堆外内存直接向操作系统申请内存,获取方式:ByteBuffer directByteBuffer = ByteBuffer.allocateDirect(4);

堆外内存的好处:
  1. 进行网络 IO 或文件 IO 时比堆内内存少一次拷贝。socket/file -> 系统内存 -> jvm内存堆
    GC 会移动对象内存,在写 file 或 socket 的过程中,也许 JVM 让操作系统读取数据往 file huo socket 中写的时候,GC 触发了,然后 GC 把数据移到别的地方去了,这样操作系统就读不到,所以JVM 的实现,会先把数据复制到堆外,在进行写入。
  2. 在 GC 范围之外,可以降低 GC 压力。DirectByteBuffer 中有一个 Cleaner 对象(PhantomReference),Cleaner 被 GC 前会执行 clean 方法,触发 DirectByteBuffer 中定义的 Deallocator。
建议
  1. 性能确实客观的时候才去使用;分配给大型、长寿命;(网络传输、大文件读写)。
  2. 通过虚拟机参数 MaxDirectMemorySize 限制大小,防止耗尽整个机器的内存。

2. Channel

Buffer 不仅仅是提供了一些接口,其主要是给 Channel 用的。
BIO 中需要用到 net 和 io 包


net + io

而 NIO 的话只需要用到 nio 包


nio

Channel 的 API 涵盖了 TCP/UDP 网络和文件 IO。有 FileChannelDatagramChannel, SocketChannel,ServerSocketChannel

下面是 SocketChannel 和 ServerSocketChannel 的例子

服务端

public class NIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false); // 设置为非阻塞式
        serverSocketChannel.socket().bind(new InetSocketAddress(8080)); // 绑定端口
        System.out.println("启动成功");
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept(); // 获取 tcp 连接通道
            if (socketChannel != null) {
                System.out.println("收到新的连接: " + socketChannel.getRemoteAddress());
                socketChannel.configureBlocking(false); // 默认是阻塞的,要设置成非阻塞
                try {
                    ByteBuffer requestByteBuffer = ByteBuffer.allocate(1024);
                    // 长连接情况下,需要手动判断数据有没有读取结束(这里做一个简单的判断,超过 0 就认为请求结束了)
                    while (socketChannel.isOpen() && socketChannel.read(requestByteBuffer) != -1) { // 读到 -1 表示连接已经断开
                        if (requestByteBuffer.position() > 0) {
                            break;
                        }
                    }
                    if (requestByteBuffer.position() == 0) continue; // 如果没有数据 则不继续处理
                    requestByteBuffer.flip();
                    byte[] content = new byte[requestByteBuffer.limit()];
                    requestByteBuffer.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 responseBuffer = ByteBuffer.wrap(response.getBytes());
                    while (responseBuffer.hasRemaining()) {
                        socketChannel.write(responseBuffer);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

客户端

public class NIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
        while (!socketChannel.finishConnect()) {
            // 没连上就一直等待
            Thread.yield();
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入");
        // 发送内容
        String msg = scanner.nextLine();
        ByteBuffer byteBuffer = ByteBuffer.wrap(msg.getBytes());
        while (byteBuffer.hasRemaining()) {
            socketChannel.write(byteBuffer);
        }
        // 读取响应
        System.out.println("收到服务端响应");
        ByteBuffer responseBuffer = ByteBuffer.allocate(1024);

        while (socketChannel.isOpen() && socketChannel.read(responseBuffer) != -1) {
            if (responseBuffer.position() > 0) break;
        }
        responseBuffer.flip();
        byte[] content = new byte[responseBuffer.limit()];
        responseBuffer.get(content);
        System.out.println(new String(content));
        scanner.close();
        socketChannel.close();
    }
}

先启动服务端,然后启动两个客户端,服务端打印如下

启动成功
收到新的连接: /127.0.0.1:1481
123
收到数据,来自:/127.0.0.1:1481
收到新的连接: /127.0.0.1:1487
123
收到数据,来自:/127.0.0.1:1487

因为使用了判断socketChannel.isOpen() && socketChannel.read(requestByteBuffer) != -1当一个客户端断开连接时才会继续,所以要对客户端进行修改,使其能同时连接多个客户端。

下面使用一个列表保存已经连接的 channel,当没有新的连接时去处理这些 channel 的数据

public class NIOServer2 {

    private static ArrayList channels = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false); // 设置为非阻塞式
        serverSocketChannel.socket().bind(new InetSocketAddress(8080)); // 绑定端口
        System.out.println("启动成功");
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept(); // 获取 tcp 连接通道
            if (socketChannel != null) {
                System.out.println("收到新的连接: " + socketChannel.getRemoteAddress());
                socketChannel.configureBlocking(false); // 默认是阻塞的,要设置成非阻塞
                channels.add(socketChannel);
            } else if (socketChannel == null) {
                // 没有新连接的情况下,就去处理现有连接数据,处理完就删掉
                Iterator iterator = channels.iterator();
                while (iterator.hasNext()) {
                    SocketChannel theSocketChnnel = iterator.next();
                    try {
                        ByteBuffer requestByteBuffer = ByteBuffer.allocate(1024);
                        if (theSocketChnnel.read(requestByteBuffer) == 0) {
                            // 等于 0 就代表这个通道没有数据要处理,就等会再处理
                            continue;
                        }
                        // 长连接情况下,需要手动判断数据有没有读取结束(这里做一个简单的判断,超过 0 就认为请求结束了)
                        while (theSocketChnnel.isOpen() && theSocketChnnel.read(requestByteBuffer) != -1) { // 读到 -1 表示连接已经断开
                            if (requestByteBuffer.position() > 0) {
                                break;
                            }
                        }
                        if (requestByteBuffer.position() == 0) continue; // 如果没有数据 则不继续处理
                        requestByteBuffer.flip();
                        byte[] content = new byte[requestByteBuffer.limit()];
                        requestByteBuffer.get(content);
                        System.out.println(new String(content));
                        System.out.println("收到数据,来自:" + theSocketChnnel.getRemoteAddress());
                        // 响应结果 200
                        String response = "HTTP/1.1 200 OK\r\n" +
                                "Content-Length: 11\r\n\r\n" +
                                "Hello World";
                        ByteBuffer responseBuffer = ByteBuffer.wrap(response.getBytes());
                        while (responseBuffer.hasRemaining()) {
                            theSocketChnnel.write(responseBuffer);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    iterator.remove();
                }
            }
        }
    }
}

3. Selector

使用循环检查的效率非常低,NIO 中提供了 Selector 组件,可以检查一个或多个 NIO 通道,并确定哪些通道已经准备好进行读取或写入。实现了单个线程可以管理多个通道,从而管理多个网络连接。

Selector 可以监听 Channel 四个事件,分别对应 SelectionKey 四个常量

  1. Connect 连接(SelectionKey.OP_CONNECT)
  2. Accept 准备就绪(SelectionKey.OP_ACCEPT)
  3. Read 读取(SelectionKey.OP_READ)
  4. Write 写入(SelectionKey.OP_WRITE)

实现一个线程处理多个通道的核心概念是事件驱动机制
非阻塞的网络通道下,开发者对 Selector 注册感兴趣的事件类型,线程通过监听事件来触发相应的代码执行。
(更底层是操作系统的多路复用机制)

下面是使用 Selector 的一个例子

public class NIOServer3 {
    public static void main(String[] args) throws IOException {
        // 1. 创建网络服务端
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false); // 设置为非阻塞式
        // 2. 构建一个 Selector 选择器,并且把 Channel 注册上去
        Selector selector = Selector.open();
        SelectionKey selectionKey = serverSocketChannel.register(selector, 0, serverSocketChannel);
        selectionKey.interestOps(SelectionKey.OP_ACCEPT); // 对 serverSocketChannel 的 accept 事件感兴趣
        // 3. 绑定端口
        serverSocketChannel.socket().bind(new InetSocketAddress(8080)); // 绑定端口
        System.out.println("启动成功");
        while (true) {
            // 不再轮询通道,改为轮询事件的方式,select 方法有阻塞效果,直到有时间通知才会有返回
            selector.select();
            // 获取事件
            Set selectionKeys = selector.selectedKeys();
            // 遍历事件
            Iterator iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                // 被封装的查询结果
                SelectionKey key = iterator.next();
                iterator.remove();
                // 对 accept 事件的处理
                if (key.isAcceptable()) {
                    ServerSocketChannel server = (ServerSocketChannel) key.attachment();
                    // 将拿到的客户端连接通道注册到 selector 上面
                    SocketChannel clientSocketChannel = server.accept();
                    clientSocketChannel.configureBlocking(false);
                    clientSocketChannel.register(selector, SelectionKey.OP_READ, clientSocketChannel);
                    System.out.println("收到新的连接:" + clientSocketChannel.getRemoteAddress());
                }
                // 对 read 事件的处理
                if (key.isReadable()){
                    SocketChannel socketChannel = (SocketChannel)key.attachment();
                    try {
                        ByteBuffer requestByteBuffer = ByteBuffer.allocate(1024);
                        while (socketChannel.isOpen() && socketChannel.read(requestByteBuffer) != -1) {
                            if (requestByteBuffer.position() > 0) {
                                break;
                            }
                        }
                        if (requestByteBuffer.position() == 0) continue;
                        requestByteBuffer.flip();
                        byte[] content = new byte[requestByteBuffer.limit()];
                        requestByteBuffer.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 responseBuffer = ByteBuffer.wrap(response.getBytes());
                        while (responseBuffer.hasRemaining()) {
                            socketChannel.write(responseBuffer);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        key.cancel(); // 取消事件订阅
                    }
                }
            }
        }
    }
}

服务端输出结果

启动成功
收到新的连接:/127.0.0.1:10380
收到新的连接:/127.0.0.1:10387
123
收到数据,来自:/127.0.0.1:10387
123
收到数据,来自:/127.0.0.1:10380

4. BIO 与 NIO 的区别

Tomcat 8 中已经完全去除 BIO 相关的网络处理代码,默认采用 NIO 进行网络处理。

5. NIO 与多线程结合的改进方案

一个 Selector 监听所有事件,一个线程处理所有时间显然是不合理的,性能会成为瓶颈。

Doug Lea 有一篇著名的文章《Scalable IO in Java》,使用基于 Reactor 的线程模型。

  • 单 Reactor 模式,Reactor 线程接受请求 - > 分发给线程池处理请求


  • 多 Reactor 模式,mainReactor 接收 -> 分发给 subReactor 读写 -> 具体业务逻辑分发给单独的线程池处理


以下是多 Reactor 模式的例子

public class NIOServerV3 {
    // 处理业务操作的线程
    private static ExecutorService workPool = Executors.newCachedThreadPool();

    /**
     * 封装了 selector.select() 等事件轮询代码
     */
    abstract class ReactorThread extends Thread {
        Selector selector;
        LinkedBlockingQueue taskQueue = new LinkedBlockingQueue<>();

        /**
         * 监听到有事件后,调用这个方法
         */
        public abstract void handler(SelectableChannel channel) throws Exception;

        private ReactorThread() throws IOException {
            selector = Selector.open();
        }

        volatile boolean running = false;

        @Override
        public void run() {
            // 轮询 Selector 事件
            while (running) {
                try {
                    Runnable task;
                    while ((task = taskQueue.poll()) != null) {
                        task.run();
                    }
                    selector.select(1000);
                    // 获取查询结果
                    Set selected = selector.selectedKeys();
                    Iterator iter = selected.iterator();
                    while (iter.hasNext()) {
                        SelectionKey key = (SelectionKey) iter.next();
                        iter.remove();
                        int readOps = key.readyOps();
                        // 关注 Read 和 Accept 两个事件
                        if ((readOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readOps == 0) {
                            try {
                                SelectableChannel channel = (SelectableChannel) key.attachment();
                                channel.configureBlocking(false);
                                handler(channel);
                                if (!channel.isOpen()) {
                                    key.cancel(); // 如果关闭了 则取消这个 key 的订阅
                                }
                            } catch (Exception e) {
                                key.cancel();
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        private SelectionKey register(SelectableChannel channel) throws Exception {
            // 为什么register要以任务提交的形式,让reactor线程去处理?
            // 因为线程在执行channel注册到selector的过程中,会和调用selector.select()方法的线程争用同一把锁
            // 而select()方法是在eventLoop中通过while循环调用的,争抢的可能性很高,为了让register能更快的执行,就放到同一个线程来处理
            FutureTask futureTask = new FutureTask<>(() -> channel.register(selector, 0, channel));
            taskQueue.add(futureTask);
            return futureTask.get();
        }

        private void doStart() {
            if (!running) {
                running = true;
                start();
            }
        }
    }

    private ServerSocketChannel serverSocketChannel;
    // accept 线程
    private ReactorThread[] mainReactorThread = new ReactorThread[1];
    // I/O 线程
    private ReactorThread[] subReactorThreads = new ReactorThread[8];

    /**
     * 初始化线程组
     *
     * @throws Exception
     */
    private void newGroup() throws Exception {
        // 创建 IO 线程,负责处理客户端连接以后的 socketChannel 连接的读写
        for (int i = 0; i < subReactorThreads.length; i++) {
            subReactorThreads[i] = new ReactorThread() {
                @Override
                public void handler(SelectableChannel channel) throws Exception {
                    SocketChannel ch = (SocketChannel) channel;
                    ByteBuffer requestBuffer = ByteBuffer.allocate(1024);
                    // 长连接情况下,需要手动判断数据有没有读取结束(这里做一个简单的判断,超过 0 就认为请求结束了)
                    while (ch.isOpen() && ch.read(requestBuffer) != -1) {
                        if (requestBuffer.position() > 0) {
                            break;
                        }
                    }
                    if (requestBuffer.position() == 0) return;
                    requestBuffer.flip();
                    byte[] content = new byte[requestBuffer.limit()];
                    requestBuffer.get(content);
                    System.out.println(new String(content));
                    System.out.println("收到数据,来自:" + ch.getRemoteAddress());

                    // TODO 业务操作
                    workPool.submit(() -> {
                    });

                    // 响应结果 200
                    String response = "HTTP/1.1 200 OK\r\n" +
                            "Content-Length: 11\r\n\r\n" +
                            "Hello World";
                    ByteBuffer responseBuffer = ByteBuffer.wrap(response.getBytes());
                    while (responseBuffer.hasRemaining()) {
                        ch.write(responseBuffer);
                    }
                }
            };
        }

        // 创建 mainReactor 线程,只负责处理 serverSockerChannel
        for (int i = 0; i < mainReactorThread.length; i++) {
            mainReactorThread[i] = new ReactorThread() {
                AtomicInteger incr = new AtomicInteger(0);

                @Override
                public void handler(SelectableChannel channel) throws Exception {
                    // 只做请求分发
                    ServerSocketChannel ch = (ServerSocketChannel) channel;
                    SocketChannel socketChannel = ch.accept();
                    socketChannel.configureBlocking(false);
                    // 收到连接建立的通知之后,分发给 I/O 线程去读取
                    int index = incr.getAndIncrement() % subReactorThreads.length;
                    ReactorThread workEventLoop = subReactorThreads[index];
                    workEventLoop.doStart();
                    SelectionKey selectionKey = workEventLoop.register(socketChannel);
                    selectionKey.interestOps(SelectionKey.OP_READ);
                    System.out.println(Thread.currentThread().getName() + "接收到新的连接:" + socketChannel.getRemoteAddress());
                }
            };
        }
    }

    private void initAndRegister() throws Exception {
        serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        int index = new Random().nextInt(mainReactorThread.length);
        mainReactorThread[index].doStart();
        SelectionKey selectionKey = mainReactorThread[index].register(serverSocketChannel);
        selectionKey.interestOps(SelectionKey.OP_ACCEPT);
    }

    private void bind() throws Exception {
        serverSocketChannel.bind(new InetSocketAddress(8080));
        System.out.println("启动完成");
    }

    public static void main(String[] args) throws Exception {
        NIOServerV3 nioServerV3 = new NIOServerV3();
        nioServerV3.newGroup(); // 1、 创建main和sub两组线程
        nioServerV3.initAndRegister(); // 2、 创建serverSocketChannel,注册到mainReactor线程上的selector上
        nioServerV3.bind(); // 3、 为serverSocketChannel绑定端口
    }
}

你可能感兴趣的:(NIO非阻塞式网络编程)