折腾了我一周,原来Netty网络编程就是这么个破玩意儿!!! ...

十念  论坛元老 | 2022-12-29 16:12:12 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1025|帖子 1025|积分 3075

1、阻塞


  • 阻塞模式下,相关方法都会导致线程暂停

    • ServerSocketChannel.accept 会在没有连接建立时让线程暂停
    • SocketChannel.read 会在通道中没有数据可读时让线程暂停
    • 阻塞的表现其实就是线程暂停了,暂停期间不会占用 cpu,但线程相当于闲置

  • 单线程下,阻塞方法之间相互影响,几乎不能正常工作,需要多线程支持
  • 但多线程下,有新的问题,体现在以下方面

    • 32 位 jvm 一个线程 320k,64 位 jvm 一个线程 1024k,如果连接数过多,必然导致 OOM,并且线程太多,反而会因为频繁上下文切换导致性能降低
    • 可以采用线程池技术来减少线程数和线程上下文切换,但治标不治本,如果有很多连接建立,但长时间 inactive,会阻塞线程池中所有线程,因此不适合长连接,只适合短连接

服务端代码
  1. public class Server {
  2.     public static void main(String[] args) {
  3.         // 创建缓冲区
  4.         ByteBuffer buffer = ByteBuffer.allocate(16);
  5.         // 获得服务器通道
  6.         try(ServerSocketChannel server = ServerSocketChannel.open()) {
  7.             // 为服务器通道绑定端口
  8.             server.bind(new InetSocketAddress(8080));
  9.             // 用户存放连接的集合
  10.             ArrayList<SocketChannel> channels = new ArrayList<>();
  11.             // 循环接收连接
  12.             while (true) {
  13.                 System.out.println("before connecting...");
  14.                 // 没有连接时,会阻塞线程
  15.                 SocketChannel socketChannel = server.accept();
  16.                 System.out.println("after connecting...");
  17.                 channels.add(socketChannel);
  18.                 // 循环遍历集合中的连接
  19.                 for(SocketChannel channel : channels) {
  20.                     System.out.println("before reading");
  21.                     // 处理通道中的数据
  22.                     // 当通道中没有数据可读时,会阻塞线程
  23.                     channel.read(buffer);
  24.                     buffer.flip();
  25.                     ByteBufferUtil.debugRead(buffer);
  26.                     buffer.clear();
  27.                     System.out.println("after reading");
  28.                 }
  29.             }
  30.         } catch (IOException e) {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34. }
复制代码
客户端代码
  1. public class Client {
  2.     public static void main(String[] args) {
  3.         try (SocketChannel socketChannel = SocketChannel.open()) {
  4.             // 建立连接
  5.             socketChannel.connect(new InetSocketAddress("localhost", 8080));
  6.             System.out.println("waiting...");
  7.         } catch (IOException e) {
  8.             e.printStackTrace();
  9.         }
  10.     }
  11. }
复制代码
运行结果

  • 客户端 - 服务器建立连接前:服务器端因 accept 阻塞


  • 客户端 - 服务器建立连接后,客户端发送消息前:服务器端因通道为空被阻塞


  • 客户端发送数据后,服务器处理通道中的数据。再次进入循环时,再次被 accept 阻塞


  • 之前的客户端再次发送消息,服务器端因为被 accept 阻塞,无法处理之前客户端发送到通道中的信息

2、非阻塞


  • 可以通过 ServerSocketChannel 的 configureBlocking (false) 方法将 获得连接设置为非阻塞的。此时若没有连接,accept 会返回 null
  • 可以通过 SocketChannel 的 configureBlocking (false) 方法将从通道中 读取数据设置为非阻塞的。若此时通道中没有数据可读,read 会返回 - 1
服务器代码如下
  1. public class Server {
  2.     public static void main(String[] args) {
  3.         // 创建缓冲区
  4.         ByteBuffer buffer = ByteBuffer.allocate(16);
  5.         // 获得服务器通道
  6.         try(ServerSocketChannel server = ServerSocketChannel.open()) {
  7.             // 设置为非阻塞模式,没有连接时返回null,不会阻塞线程
  8.             server.configureBlocking(false);
  9.             // 为服务器通道绑定端口
  10.             server.bind(new InetSocketAddress(8080));
  11.             // 用户存放连接的集合
  12.             ArrayList<SocketChannel> channels = new ArrayList<>();
  13.             // 循环接收连接
  14.             while (true) {
  15.                
  16.               
  17.                 SocketChannel socketChannel = server.accept();
  18.                 // 通道不为空时才将连接放入到集合中
  19.                 if (socketChannel != null) {
  20.                     System.out.println("after connecting...");
  21.                     channels.add(socketChannel);
  22.                 }
  23.                 // 循环遍历集合中的连接
  24.                 for(SocketChannel channel : channels) {
  25.                     // 处理通道中的数据
  26.                     // 设置为非阻塞模式,若通道中没有数据,会返回0,不会阻塞线程
  27.                     channel.configureBlocking(false);
  28.                     int read = channel.read(buffer);
  29.                     if(read > 0) {
  30.                         buffer.flip();
  31.                         ByteBufferUtil.debugRead(buffer);
  32.                         buffer.clear();
  33.                         System.out.println("after reading");
  34.                     }
  35.                 }
  36.             }
  37.         } catch (IOException e) {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41. }
复制代码
这样写存在一个问题,因为设置为了非阻塞,会一直执行 while (true) 中的代码,CPU 一直处于忙碌状态,会使得性能变低,所以实际情况中不使用这种方法处理请求
3、Selector

多路复用

单线程可以配合 Selector 完成对多个 Channel 可读写事件的监控,这称之为多路复用

  • 多路复用仅针对网络 IO,普通文件 IO 无法利用多路复用
  • 如果不用 Selector 的非阻塞模式,线程大部分时间都在做无用功,而 Selector 能够保证

    • 有可连接事件时才去连接
    • 有可读事件才去读取
    • 有可写事件才去写入

      • 限于网络传输能力,Channel 未必时时可写,一旦 Channel 可写,会触发 Selector 的可写事件


4、使用及 Accpet 事件

要使用 Selector 实现多路复用,服务端代码如下改进
  1. public class SelectServer {
  2.     public static void main(String[] args) {
  3.         ByteBuffer buffer = ByteBuffer.allocate(16);
  4.         // 获得服务器通道
  5.         try(ServerSocketChannel server = ServerSocketChannel.open()) {
  6.             server.bind(new InetSocketAddress(8080));
  7.             // 创建选择器
  8.             Selector selector = Selector.open();
  9.             
  10.             // 通道必须设置为非阻塞模式
  11.             server.configureBlocking(false);
  12.             // 将通道注册到选择器中,并设置感兴趣的事件
  13.             server.register(selector, SelectionKey.OP_ACCEPT);
  14.             while (true) {
  15.                 // 若没有事件就绪,线程会被阻塞,反之不会被阻塞。从而避免了CPU空转
  16.                 // 返回值为就绪的事件个数
  17.                 int ready = selector.select();
  18.                 System.out.println("selector ready counts : " + ready);
  19.                
  20.                 // 获取所有事件
  21.                 Set<SelectionKey> selectionKeys = selector.selectedKeys();
  22.                
  23.                 // 使用迭代器遍历事件
  24.                 Iterator<SelectionKey> iterator = selectionKeys.iterator();
  25.                 while (iterator.hasNext()) {
  26.                     SelectionKey key = iterator.next();
  27.                     
  28.                     // 判断key的类型
  29.                     if(key.isAcceptable()) {
  30.                         // 获得key对应的channel
  31.                         ServerSocketChannel channel = (ServerSocketChannel) key.channel();
  32.                         System.out.println("before accepting...");
  33.                         
  34.                                         // 获取连接并处理,而且是必须处理,否则需要取消
  35.                         SocketChannel socketChannel = channel.accept();
  36.                         System.out.println("after accepting...");
  37.                         
  38.                         // 处理完毕后移除
  39.                         iterator.remove();
  40.                     }
  41.                 }
  42.             }
  43.         } catch (IOException e) {
  44.             e.printStackTrace();
  45.         }
  46.     }
  47. }
复制代码
步骤解析

  • 获得选择器 Selector
  1. Selector selector = Selector.open();
复制代码

  • 将通道设置为非阻塞模式,并注册到选择器中,并设置感兴趣的事件

    • channel 必须工作在非阻塞模式
    • FileChannel 没有非阻塞模式,因此不能配合 selector 一起使用
    • 绑定的事件类型可以有

      • connect - 客户端连接成功时触发


  • accept - 服务器端成功接受连接时触发

    • read - 数据可读入时触发,有因为接收能力弱,数据暂不能读入的情况

  • write - 数据可写出时触发,有因为发送能力弱,数据暂不能写出的情况
  1. // 通道必须设置为非阻塞模式
  2. server.configureBlocking(false);
  3. // 将通道注册到选择器中,并设置感兴趣的实践
  4. server.register(selector, SelectionKey.OP_ACCEPT);
复制代码

  • 通过 Selector 监听事件,并获得就绪的通道个数,若没有通道就绪,线程会被阻塞

    • 阻塞直到绑定事件发生
      1. int count = selector.select();
      复制代码
    • 阻塞直到绑定事件发生,或是超时(时间单位为 ms)
      1. int count = selector.select(long timeout);
      复制代码
    • 不会阻塞,也就是不管有没有事件,立刻返回,自己根据返回值检查是否有事件
      1. int count = selector.selectNow();
      复制代码

  • 获取就绪事件并得到对应的通道,然后进行处理
  1. // 获取所有事件
  2. Set<SelectionKey> selectionKeys = selector.selectedKeys();
  3.                
  4. // 使用迭代器遍历事件
  5. Iterator<SelectionKey> iterator = selectionKeys.iterator();
  6. while (iterator.hasNext()) {
  7.         SelectionKey key = iterator.next();
  8.                     
  9.         // 判断key的类型,此处为Accept类型
  10.         if(key.isAcceptable()) {
  11.         // 获得key对应的channel
  12.         ServerSocketChannel channel = (ServerSocketChannel) key.channel();
  13.         // 获取连接并处理,而且是必须处理,否则需要取消
  14.         SocketChannel socketChannel = channel.accept();
  15.         // 处理完毕后移除
  16.         iterator.remove();
  17.         }
  18. }
复制代码
事件发生后能否不处理
事件发生后,要么处理,要么取消(cancel),不能什么都不做,否则下次该事件仍会触发,这是因为 nio 底层使用的是水平触发
5、Read 事件


  • 在 Accept 事件中,若有客户端与服务器端建立了连接,需要将其对应的 SocketChannel 设置为非阻塞,并注册到选择其中
    添加 Read 事件,触发后进行读取操作
  • 添加 Read 事件,触发后进行读取操作
  1. public class SelectServer {
  2.     public static void main(String[] args) {
  3.         ByteBuffer buffer = ByteBuffer.allocate(16);
  4.         // 获得服务器通道
  5.         try(ServerSocketChannel server = ServerSocketChannel.open()) {
  6.             server.bind(new InetSocketAddress(8080));
  7.             // 创建选择器
  8.             Selector selector = Selector.open();
  9.             // 通道必须设置为非阻塞模式
  10.             server.configureBlocking(false);
  11.             // 将通道注册到选择器中,并设置感兴趣的实践
  12.             server.register(selector, SelectionKey.OP_ACCEPT);
  13.             // 为serverKey设置感兴趣的事件
  14.             while (true) {
  15.                 // 若没有事件就绪,线程会被阻塞,反之不会被阻塞。从而避免了CPU空转
  16.                 // 返回值为就绪的事件个数
  17.                 int ready = selector.select();
  18.                 System.out.println("selector ready counts : " + ready);
  19.                 // 获取所有事件
  20.                 Set<SelectionKey> selectionKeys = selector.selectedKeys();
  21.                 // 使用迭代器遍历事件
  22.                 Iterator<SelectionKey> iterator = selectionKeys.iterator();
  23.                 while (iterator.hasNext()) {
  24.                     SelectionKey key = iterator.next();
  25.                     // 判断key的类型
  26.                     if(key.isAcceptable()) {
  27.                         // 获得key对应的channel
  28.                         ServerSocketChannel channel = (ServerSocketChannel) key.channel();
  29.                         System.out.println("before accepting...");
  30.                         // 获取连接
  31.                         SocketChannel socketChannel = channel.accept();
  32.                         System.out.println("after accepting...");
  33.                         // 设置为非阻塞模式,同时将连接的通道也注册到选择其中
  34.                         socketChannel.configureBlocking(false);
  35.                         socketChannel.register(selector, SelectionKey.OP_READ);
  36.                         // 处理完毕后移除
  37.                         iterator.remove();
  38.                     } else if (key.isReadable()) {
  39.                         SocketChannel channel = (SocketChannel) key.channel();
  40.                         System.out.println("before reading...");
  41.                         channel.read(buffer);
  42.                         System.out.println("after reading...");
  43.                         buffer.flip();
  44.                         ByteBufferUtil.debugRead(buffer);
  45.                         buffer.clear();
  46.                         // 处理完毕后移除
  47.                         iterator.remove();
  48.                     }
  49.                 }
  50.             }
  51.         } catch (IOException e) {
  52.             e.printStackTrace();
  53.         }
  54.     }
  55. }
复制代码
删除事件
当处理完一个事件后,一定要调用迭代器的 remove 方法移除对应事件,否则会出现错误。原因如下
以我们上面的 Read 事件 的代码为例

  • 当调用了 server.register (selector, SelectionKey.OP_ACCEPT) 后,Selector 中维护了一个集合,用于存放 SelectionKey 以及其对应的通道
    1. // WindowsSelectorImpl 中的 SelectionKeyImpl数组
    2. private SelectionKeyImpl[] channelArray = new SelectionKeyImpl[8];
    复制代码
    1. public class SelectionKeyImpl extends AbstractSelectionKey {
    2.     // Key对应的通道
    3.     final SelChImpl channel;
    4.     ...
    5. }
    复制代码


  • 选择器中的通道对应的事件发生后,selecionKey 会被放到另一个集合中,但是 selecionKey 不会自动移除,所以需要我们在处理完一个事件后,通过迭代器手动移除其中的 selecionKey。否则会导致已被处理过的事件再次被处理,就会引发错误

断开处理

当客户端与服务器之间的连接断开时,会给服务器端发送一个读事件,对异常断开和正常断开需要加以不同的方式进行处理

  • 正常断开

    • 正常断开时,服务器端的 channel.read (buffer) 方法的返回值为 - 1,所以当结束到返回值为 - 1 时,需要调用 key 的 cancel 方法取消此事件,并在取消后移除该事件
      1. int read = channel.read(buffer);
      2. // 断开连接时,客户端会向服务器发送一个写事件,此时read的返回值为-1
      3. if(read == -1) {
      4.     // 取消该事件的处理
      5.         key.cancel();
      6.     channel.close();
      7. } else {
      8.     ...
      9. }
      10. // 取消或者处理,都需要移除key
      11. iterator.remove();
      复制代码

  • 异常断开

    • 异常断开时,会抛出 IOException 异常, 在 try-catch 的 catch 块中捕获异常并调用 key 的 cancel 方法即可

消息边界

不处理消息边界存在的问题
将缓冲区的大小设置为 4 个字节,发送 2 个汉字(你好),通过 decode 解码并打印时,会出现乱码
  1. ByteBuffer buffer = ByteBuffer.allocate(4);
  2. // 解码并打印
  3. System.out.println(StandardCharsets.UTF_8.decode(buffer));
  4. 你�
  5. ��
复制代码
这是因为 UTF-8 字符集下,1 个汉字占用 3 个字节,此时缓冲区大小为 4 个字节,一次读时间无法处理完通道中的所有数据,所以一共会触发两次读事件。这就导致 你好 的 好 字被拆分为了前半部分和后半部分发送,解码时就会出现问题
处理消息边界
传输的文本可能有以下三种情况

  • 文本大于缓冲区大小

    • 此时需要将缓冲区进行扩容

  • 发生半包现象
  • 发生粘包现象

解决思路大致有以下三种

  • 固定消息长度,数据包大小一样,服务器按预定长度读取,当发送的数据较少时,需要将数据进行填充,直到长度与消息规定长度一致。缺点是浪费带宽
  • 另一种思路是按分隔符拆分,缺点是效率低,需要一个一个字符地去匹配分隔符
  • TLV 格式,即 Type 类型、Length 长度、Value 数据
    (也就是在消息开头用一些空间存放后面数据的长度),如 HTTP 请求头中的 Content-Type 与 Content-Length
    。类型和长度已知的情况下,就可以方便获取消息大小,分配合适的 buffer,缺点是 buffer 需要提前分配,如果内容过大,则影响 server 吞吐量

    • Http 1.1 是 TLV 格式

  • Http 2.0 是 LTV 格式

下文的消息边界处理方式为第二种:按分隔符拆分
附件与扩容
Channel 的 register 方法还有第三个参数:附件,可以向其中放入一个 Object 类型的对象,该对象会与登记的 Channel 以及其对应的 SelectionKey 绑定,可以从 SelectionKey 获取到对应通道的附件
  1. public final SelectionKey register(Selector sel, int ops, Object att)
复制代码
可通过 SelectionKey 的 attachment () 方法获得附件
  1. ByteBuffer buffer = (ByteBuffer) key.attachment();
复制代码
我们需要在 Accept 事件发生后,将通道注册到 Selector 中时,对每个通道添加一个 ByteBuffer 附件,让每个通道发生读事件时都使用自己的通道,避免与其他通道发生冲突而导致问题
  1. // 设置为非阻塞模式,同时将连接的通道也注册到选择其中,同时设置附件
  2. socketChannel.configureBlocking(false);
  3. ByteBuffer buffer = ByteBuffer.allocate(16);
  4. // 添加通道对应的Buffer附件
  5. socketChannel.register(selector, SelectionKey.OP_READ, buffer);
复制代码
当 Channel 中的数据大于缓冲区时,需要对缓冲区进行扩容操作。此代码中的扩容的判定方法: Channel 调用 compact 方法后,的 position 与 limit 相等,说明缓冲区中的数据并未被读取(容量太小),此时创建新的缓冲区,其大小扩大为两倍。同时还要将旧缓冲区中的数据拷贝到新的缓冲区中,同时调用 SelectionKey 的 attach 方法将新的缓冲区作为新的附件放入 SelectionKey 中
  1. // 如果缓冲区太小,就进行扩容
  2. if (buffer.position() == buffer.limit()) {
  3.     ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity()*2);
  4.     // 将旧buffer中的内容放入新的buffer中
  5.     ewBuffer.put(buffer);
  6.     // 将新buffer作为附件放到key中
  7.     key.attach(newBuffer);
  8. }
复制代码
改造后的服务器代码如下
  1. public class SelectServer {
  2.     public static void main(String[] args) {
  3.         // 获得服务器通道
  4.         try(ServerSocketChannel server = ServerSocketChannel.open()) {
  5.             server.bind(new InetSocketAddress(8080));
  6.             // 创建选择器
  7.             Selector selector = Selector.open();
  8.             // 通道必须设置为非阻塞模式
  9.             server.configureBlocking(false);
  10.             // 将通道注册到选择器中,并设置感兴趣的事件
  11.             server.register(selector, SelectionKey.OP_ACCEPT);
  12.             // 为serverKey设置感兴趣的事件
  13.             while (true) {
  14.                 // 若没有事件就绪,线程会被阻塞,反之不会被阻塞。从而避免了CPU空转
  15.                 // 返回值为就绪的事件个数
  16.                 int ready = selector.select();
  17.                 System.out.println("selector ready counts : " + ready);
  18.                 // 获取所有事件
  19.                 Set<SelectionKey> selectionKeys = selector.selectedKeys();
  20.                 // 使用迭代器遍历事件
  21.                 Iterator<SelectionKey> iterator = selectionKeys.iterator();
  22.                
  23.                 while (iterator.hasNext()) {
  24.                     SelectionKey key = iterator.next();
  25.                     iterator.remove();
  26.                     // 判断key的类型
  27.                     if(key.isAcceptable()) {
  28.                         // 获得key对应的channel
  29.                         ServerSocketChannel channel = (ServerSocketChannel) key.channel();
  30.                         System.out.println("before accepting...");
  31.                         // 获取连接
  32.                         SocketChannel socketChannel = channel.accept();
  33.                         System.out.println("after accepting...");
  34.                         // 设置为非阻塞模式,同时将连接的通道也注册到选择其中,同时设置附件
  35.                         socketChannel.configureBlocking(false);
  36.                         ByteBuffer buffer = ByteBuffer.allocate(16);
  37.                         socketChannel.register(selector, SelectionKey.OP_READ, buffer);
  38.                         
  39.                         
  40.                     } else if (key.isReadable()) {
  41.                         SocketChannel channel = (SocketChannel) key.channel();
  42.                         System.out.println("before reading...");
  43.                         // 通过key获得附件(buffer)
  44.                         ByteBuffer buffer = (ByteBuffer) key.attachment();
  45.                         int read = channel.read(buffer);
  46.                         if(read == -1) {
  47.                             key.cancel();
  48.                             channel.close();
  49.                         } else {
  50.                             // 通过分隔符来分隔buffer中的数据
  51.                             split(buffer);
  52.                             // 如果缓冲区太小,就进行扩容
  53.                             if (buffer.position() == buffer.limit()) {
  54.                                 ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity()*2);
  55.                                 // 将旧buffer中的内容放入新的buffer中
  56.                                 buffer.flip();
  57.                                 newBuffer.put(buffer);
  58.                                 // 将新buffer放到key中作为附件
  59.                                 key.attach(newBuffer);
  60.                             }
  61.                         }
  62.                         System.out.println("after reading...");
  63.                        
  64.                        
  65.                     }
  66.                 }
  67.             }
  68.         } catch (IOException e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.     private static void split(ByteBuffer buffer) {
  73.         buffer.flip();
  74.         for(int i = 0; i < buffer.limit(); i++) {
  75.             // 遍历寻找分隔符
  76.             // get(i)不会移动position
  77.             if (buffer.get(i) == '\n') {
  78.                 // 缓冲区长度
  79.                 int length = i+1-buffer.position();
  80.                 ByteBuffer target = ByteBuffer.allocate(length);
  81.                 // 将前面的内容写入target缓冲区
  82.                 for(int j = 0; j < length; j++) {
  83.                     // 将buffer中的数据写入target中
  84.                     target.put(buffer.get());
  85.                 }
  86.                 // 打印结果
  87.                 ByteBufferUtil.debugAll(target);
  88.             }
  89.         }
  90.         // 切换为写模式,但是缓冲区可能未读完,这里需要使用compact
  91.         buffer.compact();
  92.     }
  93. }
复制代码
ByteBuffer 的大小分配


  • 每个 channel 都需要记录可能被切分的消息,因为 ByteBuffer 不能被多个 channel 共同使用,因此需要为每个 channel 维护一个独立的 ByteBuffer
  • ByteBuffer 不能太大,比如一个 ByteBuffer 1Mb 的话,要支持百万连接就要 1Tb 内存,因此需要设计大小可变的 ByteBuffer
  • 分配思路可以参考

    • 一种思路是首先分配一个较小的 buffer,例如 4k,如果发现数据不够,再分配 8k 的 buffer,将 4k buffer 内容拷贝至 8k buffer,优点是消息连续容易处理,缺点是数据拷贝耗费性能
    • 另一种思路是用多个数组组成 buffer,一个数组不够,把多出来的内容写入新的数组,与前面的区别是消息存储不连续解析复杂,优点是避免了拷贝引起的性能损耗

6、Write 事件

服务器通过 Buffer 向通道中写入数据时,可能因为通道容量小于 Buffer 中的数据大小,导致无法一次性将 Buffer 中的数据全部写入到 Channel 中,这时便需要分多次写入,具体步骤如下

  • 执行一次写操作,向将 buffer 中的内容写入到 SocketChannel 中,然后判断 Buffer 中是否还有数据
  • 若 Buffer 中还有数据,则需要将 SockerChannel 注册到 Seletor 中,并关注写事件,同时将未写完的 Buffer 作为附件一起放入到 SelectionKey 中
  1. int write = socket.write(buffer);
  2. // 通道中可能无法放入缓冲区中的所有数据
  3. if (buffer.hasRemaining()) {
  4.     // 注册到Selector中,关注可写事件,并将buffer添加到key的附件中
  5.     socket.configureBlocking(false);
  6.     socket.register(selector, SelectionKey.OP_WRITE, buffer);
  7. }
复制代码

  • 添加写事件的相关操作 key.isWritable(),对 Buffer 再次进行写操作

    • 每次写后需要判断 Buffer 中是否还有数据(是否写完)。若写完,需要移除 SelecionKey 中的 Buffer 附件,避免其占用过多内存,同时还需移除对写事件的关注

  1. SocketChannel socket = (SocketChannel) key.channel();
  2. // 获得buffer
  3. ByteBuffer buffer = (ByteBuffer) key.attachment();
  4. // 执行写操作
  5. int write = socket.write(buffer);
  6. System.out.println(write);
  7. // 如果已经完成了写操作,需要移除key中的附件,同时不再对写事件感兴趣
  8. if (!buffer.hasRemaining()) {
  9.     key.attach(null);
  10.     key.interestOps(0);
  11. }
复制代码
整体代码如下
  1. public class WriteServer {
  2.     public static void main(String[] args) {
  3.         try(ServerSocketChannel server = ServerSocketChannel.open()) {
  4.             server.bind(new InetSocketAddress(8080));
  5.             server.configureBlocking(false);
  6.             Selector selector = Selector.open();
  7.             server.register(selector, SelectionKey.OP_ACCEPT);
  8.             while (true) {
  9.                 selector.select();
  10.                 Set<SelectionKey> selectionKeys = selector.selectedKeys();
  11.                 Iterator<SelectionKey> iterator = selectionKeys.iterator();
  12.                 while (iterator.hasNext()) {
  13.                     SelectionKey key = iterator.next();
  14.                     // 处理后就移除事件
  15.                     iterator.remove();
  16.                     if (key.isAcceptable()) {
  17.                         // 获得客户端的通道
  18.                         SocketChannel socket = server.accept();
  19.                         // 写入数据
  20.                         StringBuilder builder = new StringBuilder();
  21.                         for(int i = 0; i < 500000000; i++) {
  22.                             builder.append("a");
  23.                         }
  24.                         ByteBuffer buffer = StandardCharsets.UTF_8.encode(builder.toString());
  25.                         // 先执行一次Buffer->Channel的写入,如果未写完,就添加一个可写事件
  26.                         int write = socket.write(buffer);
  27.                         System.out.println(write);
  28.                         // 通道中可能无法放入缓冲区中的所有数据
  29.                         if (buffer.hasRemaining()) {
  30.                             // 注册到Selector中,关注可写事件,并将buffer添加到key的附件中
  31.                             socket.configureBlocking(false);
  32.                             socket.register(selector, SelectionKey.OP_WRITE, buffer);
  33.                         }
  34.                     } else if (key.isWritable()) {
  35.                         SocketChannel socket = (SocketChannel) key.channel();
  36.                         // 获得buffer
  37.                         ByteBuffer buffer = (ByteBuffer) key.attachment();
  38.                         // 执行写操作
  39.                         int write = socket.write(buffer);
  40.                         System.out.println(write);
  41.                         // 如果已经完成了写操作,需要移除key中的附件,同时不再对写事件感兴趣
  42.                         if (!buffer.hasRemaining()) {
  43.                             key.attach(null);
  44.                             key.interestOps(0);
  45.                         }
  46.                     }
  47.                 }
  48.             }
  49.         } catch (IOException e) {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53. }
复制代码
7、优化

多线程优化

充分利用多核 CPU,分两组选择器

  • 单线程配一个选择器(Boss),专门处理 accept 事件
  • 创建 cpu 核心数的线程(Worker),每个线程配一个选择器,轮流处理 read 事件
实现思路


  • 创建一个负责处理 Accept 事件的 Boss 线程,与多个负责处理 Read 事件的 Worker 线程
  • Boss 线程执行的操作

    • 接受并处理 Accepet 事件,当 Accept 事件发生后,调用 Worker 的 register (SocketChannel socket) 方法,让 Worker 去处理 Read 事件,其中需要根据标识 robin 去判断将任务分配给哪个 Worker
      1. // 创建固定数量的Worker
      2. Worker[] workers = new Worker[4];
      3. // 用于负载均衡的原子整数
      4. AtomicInteger robin = new AtomicInteger(0);
      5. // 负载均衡,轮询分配Worker
      6. workers[robin.getAndIncrement()% workers.length].register(socket);
      复制代码
    • register (SocketChannel socket) 方法会通过同步队列完成 Boss 线程与 Worker 线程之间的通信,让 SocketChannel 的注册任务被 Worker 线程执行。添加任务后需要调用 selector.wakeup () 来唤醒被阻塞的 Selector
      1. public void register(final SocketChannel socket) throws IOException {
      2.     // 只启动一次
      3.     if (!started) {
      4.        // 初始化操作
      5.     }
      6.     // 向同步队列中添加SocketChannel的注册事件
      7.     // 在Worker线程中执行注册事件
      8.     queue.add(new Runnable() {
      9.         @Override
      10.         public void run() {
      11.             try {
      12.                 socket.register(selector, SelectionKey.OP_READ);
      13.             } catch (IOException e) {
      14.                 e.printStackTrace();
      15.             }
      16.         }
      17.     });
      18.     // 唤醒被阻塞的Selector
      19.     // select类似LockSupport中的park,wakeup的原理类似LockSupport中的unpark
      20.     selector.wakeup();
      21. }
      复制代码

  • Worker 线程执行的操作

    • 从同步队列中获取注册任务,并处理 Read 事件

实现代码
  1. public class ThreadsServer {
  2.     public static void main(String[] args) {
  3.         try (ServerSocketChannel server = ServerSocketChannel.open()) {
  4.             // 当前线程为Boss线程
  5.             Thread.currentThread().setName("Boss");
  6.             server.bind(new InetSocketAddress(8080));
  7.             // 负责轮询Accept事件的Selector
  8.             Selector boss = Selector.open();
  9.             server.configureBlocking(false);
  10.             server.register(boss, SelectionKey.OP_ACCEPT);
  11.             // 创建固定数量的Worker
  12.             Worker[] workers = new Worker[4];
  13.             // 用于负载均衡的原子整数
  14.             AtomicInteger robin = new AtomicInteger(0);
  15.             for(int i = 0; i < workers.length; i++) {
  16.                 workers[i] = new Worker("worker-"+i);
  17.             }
  18.             while (true) {
  19.                 boss.select();
  20.                 Set<SelectionKey> selectionKeys = boss.selectedKeys();
  21.                 Iterator<SelectionKey> iterator = selectionKeys.iterator();
  22.                 while (iterator.hasNext()) {
  23.                     SelectionKey key = iterator.next();
  24.                     iterator.remove();
  25.                     // BossSelector负责Accept事件
  26.                     if (key.isAcceptable()) {
  27.                         // 建立连接
  28.                         SocketChannel socket = server.accept();
  29.                         System.out.println("connected... ");
  30.                         socket.configureBlocking(false);
  31.                         // socket注册到Worker的Selector中
  32.                         System.out.println("before read...");
  33.                         // 负载均衡,轮询分配Worker
  34.                         workers[robin.getAndIncrement()% workers.length].register(socket);
  35.                         System.out.println("after read...");
  36.                     }
  37.                 }
  38.             }
  39.         } catch (IOException e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.     static class Worker implements Runnable {
  44.         private Thread thread;
  45.         private volatile Selector selector;
  46.         private String name;
  47.         private volatile boolean started = false;
  48.         /**
  49.          * 同步队列,用于Boss线程与Worker线程之间的通信
  50.          */
  51.         private ConcurrentLinkedQueue<Runnable> queue;
  52.         public Worker(String name) {
  53.             this.name = name;
  54.         }
  55.         public void register(final SocketChannel socket) throws IOException {
  56.             // 只启动一次
  57.             if (!started) {
  58.                 thread = new Thread(this, name);
  59.                 selector = Selector.open();
  60.                 queue = new ConcurrentLinkedQueue<>();
  61.                 thread.start();
  62.                 started = true;
  63.             }
  64.             
  65.             // 向同步队列中添加SocketChannel的注册事件
  66.             // 在Worker线程中执行注册事件
  67.             queue.add(new Runnable() {
  68.                 @Override
  69.                 public void run() {
  70.                     try {
  71.                         socket.register(selector, SelectionKey.OP_READ);
  72.                     } catch (IOException e) {
  73.                         e.printStackTrace();
  74.                     }
  75.                 }
  76.             });
  77.             // 唤醒被阻塞的Selector
  78.             // select类似LockSupport中的park,wakeup的原理类似LockSupport中的unpark
  79.             selector.wakeup();
  80.         }
  81.         @Override
  82.         public void run() {
  83.             while (true) {
  84.                 try {
  85.                     selector.select();
  86.                     // 通过同步队列获得任务并运行
  87.                     Runnable task = queue.poll();
  88.                     if (task != null) {
  89.                         // 获得任务,执行注册操作
  90.                         task.run();
  91.                     }
  92.                     Set<SelectionKey> selectionKeys = selector.selectedKeys();
  93.                     Iterator<SelectionKey> iterator = selectionKeys.iterator();
  94.                     while(iterator.hasNext()) {
  95.                         SelectionKey key = iterator.next();
  96.                         iterator.remove();
  97.                         // Worker只负责Read事件
  98.                         if (key.isReadable()) {
  99.                             // 简化处理,省略细节
  100.                             SocketChannel socket = (SocketChannel) key.channel();
  101.                             ByteBuffer buffer = ByteBuffer.allocate(16);
  102.                             socket.read(buffer);
  103.                             buffer.flip();
  104.                             ByteBufferUtil.debugAll(buffer);
  105.                         }
  106.                     }
  107.                 } catch (IOException e) {
  108.                     e.printStackTrace();
  109.                 }
  110.             }
  111.         }
  112.     }
  113. }
复制代码
本文由传智教育博学谷教研团队发布。
如果本文对您有帮助,欢迎关注和点赞;如果您有任何建议也可留言评论或私信,您的支持是我坚持创作的动力。
转载请注明出处!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

十念

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表