分布式通信Netty(一):JAVA BIO&NIO&AIO全解

分布式通信Netty(一):JAVA BIO&NIO&AIO全解

IO模型

什么是IO模型?

采用什么样的的通道进行数据的发送和接收。

JAVA支持的网络通信IO模型分为:BIO、NIO、AIO

BIO(Blocking IO)

同步阻塞模型,一个客户端连接对应一个处理线程

单线程版本:只允许一个客户端连接

在这里插入图片描述
多线程版本:允许多个客户端连接
在这里插入图片描述
BIO服务端代码示例:

public class SocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        while (true) {
            System.out.println("等待连接。。。。");
            //第一步阻塞方法
            Socket clientSocket = serverSocket.accept();
            System.out.println("有客户端连接了。。。。");
            handler(clientSocket);
            /*new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
  						handler(clientSocket);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();*/
        }
    }

    private static void handler(Socket clientSocket) throws IOException {
        byte[] bytes = new byte[1024];
        System.out.println("准备read。。。。");
        //接收客户端的数据。阻塞方法,没有数据可读时就阻塞
        int read = clientSocket.getInputStream().read(bytes);

        System.out.println("数据read完毕。。。。");

        if (read != -1) {
            System.out.println("接收到客户端的数据:" + new String(bytes, 0, read));
        }
        clientSocket.getOutputStream().write("helloClient".getBytes());
        clientSocket.getOutputStream().flush();
    }
}

BIO客户端代码示例:

public class SocketClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 9000);
        //向服务端发送数据
        socket.getOutputStream().write("Hell0Server".getBytes());
        socket.getOutputStream().flush();
        System.out.println("向服务端发送数据结束");
        byte[] bytes = new byte[1024];
        //接收服务端回传的数据

        socket.getInputStream().read(bytes);
        System.out.println("接收到服务端的数据:" + new String(bytes));
        socket.close();
    }
}

在这里插入图片描述
缺点
1、 采用线程池,也会造成大量的数据等待,阻塞线程

BIO缺点
1、IO的读操作(read)是阻塞操作,当连接不做数据读写操作会导致线程阻塞,从而浪费资源。
2、大量线程的切换,会造成资源的浪费,以及操作系统操作过重。
3、线程过多,导致服务器的线程太多,压力太大。例如C10K问题(C10K问题的本质上是操作系统的问题。对于Web 1.0/2.0时代的操作系统,传统的同步阻塞I/O模型处理方式都是requests per second。当创建的进程或线程多了,数据拷贝频繁(缓存I/O、内核将数据拷贝到用户进程空间、阻塞,进程/线程上下文切换消耗大, 导致操作系统崩溃,这就是C10K问题的本质)

应用场景
1、试用链接数目较小的架构,但是对服务器资源要求高,程序简单易理解

NIO(Non Blocking IO)

同步非阻塞,从而服务器的一个线程可以处理多个请求。由于采用多路复用器Selector上客户端发送的链接请求都会注册到这里,从而Selector采用轮询的方式处理有IO请求的连接,起始版本JDK1.4

NIO的这种方式适合在轻操作(连接数目多且lian连接比较短)的架构,例如弹幕系统,服务器之间的通讯等,但是编程比较复杂

NIO非阻塞代码实例:

package com.example.demo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NioServer {

    //保持客户端连接
    static List<SocketChannel> channelList = new ArrayList<>();

    public static void main(String[] args) throws IOException {

        //第一步创建NIO ServerSocketChannel  类似BIO的serverSocker
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(8888));
        //第二、设置ServerSocketChannel为非阻塞
        serverSocketChannel.configureBlocking(false);

        System.out.println("服务启动成功");

        while (true) {
            //第二中设置非阻塞模式 accept不会阻塞
            //NIO是由操作系统的内部实现,当部署在linux,底层调用linux内核的accept的函数
            SocketChannel socketChannel = serverSocketChannel.accept();
            //如果有客户端连接
            if (socketChannel != null) {
                System.out.println("程序连接成功");
                //设置非阻塞
                socketChannel.configureBlocking(false);
                channelList.add(socketChannel);
            }
            //遍历连接数据读取
            Iterator<SocketChannel> iterator = channelList.iterator();
            while (iterator.hasNext()) {
                SocketChannel next = iterator.next();
                ByteBuffer allocate = ByteBuffer.allocate(128);
                //非阻塞,read不会阻塞
                int read = next.read(allocate);

                //如果有数据则打印

                if (read > 0) {
                    System.out.println("收到消息: " + new String(allocate.array()));
                } else if (read == -1) {
                    //有客户端断开,从集合中剔除
                    iterator.remove();
                    System.out.println("有客户端断开连接");
                }

            }

        }

    }
}

当连接数太多,会造成大量无效遍历操作,例如有1000连接,100个写数据,900的连接未断开,造成每次轮训1000次但是有效的才1/10

NIO采用多路复用器Selector的实例:

package com.example.demo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class NioSelectorServer {
    public static void main(String[] args) throws IOException, InterruptedException {

        // 创建NIO ServerSocketChannel
        ServerSocketChannel serverSocket = ServerSocketChannel.open();
        serverSocket.socket().bind(new InetSocketAddress(8888));
        // 设置ServerSocketChannel为非阻塞
        serverSocket.configureBlocking(false);
        // 打开Selector处理Channel,即创建epoll
        Selector selector = Selector.open();
        // 把ServerSocketChannel注册到selector上,并且selector对客户端accept连接操作
        serverSocket.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("服务启动成功");

        while (true) {
            // 阻塞等待需要处理的事件
            selector.select();

            // 获取selector中注册的全部事件的 SelectionKey 实例
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();

            // 遍历SelectionKey对事件进行处理
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                // 如果是OP_ACCEPT事件,则进行连接获取和事件注册
                if (key.isAcceptable()) {
                    ServerSocketChannel server = (ServerSocketChannel) key.channel();
                    SocketChannel socketChannel = server.accept();
                    socketChannel.configureBlocking(false);
                    // 这里只注册了读事件,如果需要给客户端发送数据可以注册写事件
                    socketChannel.register(selector, SelectionKey.OP_READ);
                    System.out.println("客户端连接成功");
                } else if (key.isReadable()) {  
                    // 如果是OP_READ事件,则进行读取和打印
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    ByteBuffer byteBuffer = ByteBuffer.allocate(128);
                    int len = socketChannel.read(byteBuffer);
                    // 如果有数据,把数据打印出来
                    if (len > 0) {
                        System.out.println("接收到消息:" + new String(byteBuffer.array()));
                    } else if (len == -1) { // 如果客户端断开连接,关闭Socket
                        System.out.println("客户端断开连接");
                        socketChannel.close();
                    }
                }
                //从事件集合里删除本次处理的key,防止下次select重复处理
                iterator.remove();
            }
        }
    }
}

Channel(通道)、Buffer(缓冲区)、Selector(多路复用器)是NIO的三大核心组件

注册
注册
注册
Client1
buffer数组
SocketChannel
Client1
buffer数组
SocketChannel
Client1
buffer数组
SocketChannel
selector
Thend
Server
  channel 会注册到 selector 上,由 selector 根据 channel 读写事件的发生将其交由某个空闲的线程处理,NIO 的 Buffer 和 channel 都是既可以读也可以写.

  NIO底层在JDK1.4版本是用linux的内核函数select()或poll()来实现,跟上面的NioServer代码类似,selector每次都会轮询所有的sockchannel看下哪个channel有读写事件,有的话就处理,没有就继续遍历,JDK1.5开始引入了epoll基于事件响应机制来优化NIO。
open创建
register注册
select阻塞等待需要处理

NIO整个流程:

Created with Raphaël 2.2.0 Java调用了操作系统的内核函数来创建Socket 获取到Socket的文件描述符 创建一个Selector 对象 操作系统的Epoll描述符 获取到的Socket连接的文件描述符的事件绑定到Selector对应的Epoll文件描述符上 事件的异步通知,实现了一条线程且不需要太多无效遍历,事件处理交给操作系统内核(操作系统中断 程序实现),提高了效率

Epoll函数详解

int epoll_create(int size);

创建一个epoll实例,并返回一个非负数作为文件描述符,用于对epoll接口的所有后续调用。参数size代表可能会容纳size个描述 符,但size不是一个最大值,只是提示操作系统它的数量级,现在这个参数基本上已经弃用了。

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
使用文件描述符epfd引用的epoll实例,对目标文件描述符fd执行op操作。 参数epfd表示epoll对应的文件描述符,参数fd表示socket对应的文件描述符。
参数op
EPOLL_CTL_ADD注册新的fd到epfd中且关联事件event
EPOLL_CTL_MOD修改已经注册的fd的监听事件
EPOLL_CTL_DEL从epfd中移除fd忽略掉绑定的eventnull

I/O多路复用底层主要用的Linux 内核函数(select,poll,epoll)来实现,windows不支持epoll实现,windows底层是基于winsock2的 select函数实现的(不开源)
在这里插入图片描述

AIO(NIO 2.0)

异步非阻塞, 由操作系统完成后回调通知服务端程序启动线程去处理, 一般适用于连接数较多且连接时间较长的应用
AIO方式适用于连接数目多且连接比较长(重操作)的架构,JDK7 开始支持
AIO还不够成熟,暂时不介绍
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值