首页 文章 精选 留言 我的

精选列表

搜索[Rust编程],共10000篇文章
优秀的个人博客,低调大师

“最火 AI 编程软件” Cursor 备受风投公司青睐,公司估值超过 180 亿美元

据彭博援引知情人士报道,近几周来,投资者已与 Cursor 开发商 Anysphere 接洽,商讨一项融资协议,该协议将使这家初创公司的估值达到 180 至 200 亿美元。该提议是在这家 AI 初创公司年收入超过 5 亿美元后不久提出的。 知情人士透露,该公司并未主动寻求新一轮融资,而是投资者主动接洽。 Anysphere 首席执行官 Michael Truell 此前透露,超过半数财富 500 强企业使用 Cursor,日活用户超过 100 万人。OpenAI、Spotify、美国职业棒球大联盟和 Instacart 等知名公司均为其用户。 这家成立于 2023 年的公司年化收入已突破 5 亿美元,被硅谷投资者誉为 “史上收入增长最快的初创公司”。虽然公司目前并不缺乏现金,但考虑到有利的融资条件,Anysphere 可能会选择增加更多资本储备。

优秀的个人博客,低调大师

Kmesh v1.1.0 发布,基于 eBPF 和可编程内核的无 Sidecar 服务网格

Kmesh v1.1.0 版本已正式发布。 在 v1.0.0 的基础上,此版本对 Kmesh 的架构、可观察性和生态系统集成进行了重大改进。Kmesh 官方网站经过了全面的重新设计,提供了直观的界面和精简的文档,以增强用户和开发者的体验。此外,我们还重构了 DNS 模块并添加了长连接指标,从而能够更深入地洞察更多流量模式。 在 Kernel-Native 模式下,我们减少了对内核的侵入式修改。另外,我们使用全局变量替换 BPF 配置映射,以简化底层复杂性。与 Istio 1.25 的兼容性也经过了严格的验证,确保与该版本的 Istio 实现无缝互操作。值得注意的是,长期以来一直存在的 TestKmeshRestart E2E 测试用例不稳定问题,通过对底层 BPF 程序的长期调查和重构,已得到解决,标志着运行时可靠性的飞跃。 Kmesh v1.1.0 版本主要特性 网站全新改版 Kmesh 官方网站经过了彻底的重新设计,提供了更直观的用户体验,改进了文档,重新组织了内容层次结构,并简化了导航。在处理上一次迭代中的反馈时,我们专注于可以提升用户体验的关键领域。之前的界面存在一些可用性问题,偶尔会导致查找比较困难。我们的博客模块尤其需要关注,因为它的内容组织和视觉层次结构已经影响了内容的可发现性和可读性。从工程角度来看,我们认识到可以通过更好的组件组织和更系统的样式方法来改进代码结构,因为现有的实现随着时间的推移已经变得越来越复杂,难以维护。 为了解决这些问题,我们转向了 React 和 Docusaurus,这是一个对开发人员更加友好的现代文档框架。这使我们能够创建模块化组件,并通过可重用性消除冗余代码。 Docusaurus 提供专为文档和博客设计的内置导航系统,以及版本控制的文档功能。我们实现了文档的多语言支持,添加了高级搜索功能,并彻底重构了内容结构。这些举措显著提升了用户体验,使 Kmesh 网站对所有用户来说都更易于访问,也更具价值。 长连接指标 在此版本之前,Kmesh 仅在 TCP 连接终止和建立期间提供访问日志,其中包含有关连接的详细信息,例如发送和接收的字节数、数据包丢失、RTT 和重传次数。Kmesh 还提供特定于工作负载和服务的指标,例如发送和接收的字节数、丢失的数据包、最小 RTT 以及 Pod 打开和关闭的总连接数。这些指标仅在连接关闭后更新。 在此版本中,我们实现了 TCP 长连接的访问日志和指标,并开发了一种持续的监控和报告机制,可在长连接整个生命周期内捕获详细的实时数据。访问日志会定期报告,其中包含报告时间、连接建立时间、发送字节数、接收字节数、丢包率、RTT、重传次数和状态等信息。长连接还会定期报告发送字节数、接收字节数、丢包率和重传次数等指标。 DNS 重构 当前的 DNS 进程包含 CDS 刷新进程。因此,DNS 与内核原生模式深度耦合,无法在双引擎模式下使用。 在 1.1 版本中,我们重构了 Kmesh 的 DNS 模块。DNS 中循环遍历刷新队列的数据不再是一个包含 CDS 的结构,而是变成了一个域名,因此 DNS 模块不再关心 Kmesh 模式,只提供待解析的主机名。 BPF 配置映射优化 Kmesh 已删除专用的 kmesh_config_map BPF map,该map之前存储了全局运行时配置,例如 BPF 日志记录级别和监控开关。现在,这些设置通过全局变量进行管理。利用全局变量可以简化 BPF 配置管理,从而提高运行时效率和可维护性。 优化内核原生模式,减少对内核的侵入式修改 内核原生模式需要大量侵入式内核重构才能实现基于 HTTP 的流量控制。其中一些修改可能会对内核产生重大影响,这使得内核原生模式难以在实际产品中部署和使用。 为了解决这个问题,我们同步修改了内核原生模式下的内核以及相关的 ko 和 eBPF。通过本次版本的优化,在内核 5.10 中,内核修改限制为四个,在内核 6.6 中,内核修改减少为只有一个。最后一个修改将尽可能地被消除,最终目标是在原生版本 6.6 及以上版本上运行内核原生模式。 Istio 1.25 兼容性验证 Kmesh 已验证与 Istio 1.25 的兼容性,并在 CI 中添加了相应的端到端测试。Kmesh 社区负责在 CI 中对三个 Istio 版本进行验证,因此 Istio 1.22 的端到端测试已从 CI 中移除。 关键 Bug 修复 1. kmeshctl 安装waypoint错误: https://github.com/kmesh-net/kmesh/issues/1287 2. TestKmeshRestart flaky问题: https://github.com/kmesh-net/kmesh/issues/1192 Kmesh v1.1.0 发布说明:https://github.com/kmesh-net/kmesh/releases/tag/v1.1.0

优秀的个人博客,低调大师

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

1、阻塞 阻塞模式下,相关方法都会导致线程暂停 ServerSocketChannel.accept 会在没有连接建立时让线程暂停 SocketChannel.read 会在通道中没有数据可读时让线程暂停 阻塞的表现其实就是线程暂停了,暂停期间不会占用 cpu,但线程相当于闲置 单线程下,阻塞方法之间相互影响,几乎不能正常工作,需要多线程支持 但多线程下,有新的问题,体现在以下方面 32 位 jvm 一个线程 320k,64 位 jvm 一个线程 1024k,如果连接数过多,必然导致 OOM,并且线程太多,反而会因为频繁上下文切换导致性能降低 可以采用线程池技术来减少线程数和线程上下文切换,但治标不治本,如果有很多连接建立,但长时间 inactive,会阻塞线程池中所有线程,因此不适合长连接,只适合短连接 服务端代码 public class Server { public static void main(String[] args) { // 创建缓冲区 ByteBuffer buffer = ByteBuffer.allocate(16); // 获得服务器通道 try(ServerSocketChannel server = ServerSocketChannel.open()) { // 为服务器通道绑定端口 server.bind(new InetSocketAddress(8080)); // 用户存放连接的集合 ArrayList<SocketChannel> channels = new ArrayList<>(); // 循环接收连接 while (true) { System.out.println("before connecting..."); // 没有连接时,会阻塞线程 SocketChannel socketChannel = server.accept(); System.out.println("after connecting..."); channels.add(socketChannel); // 循环遍历集合中的连接 for(SocketChannel channel : channels) { System.out.println("before reading"); // 处理通道中的数据 // 当通道中没有数据可读时,会阻塞线程 channel.read(buffer); buffer.flip(); ByteBufferUtil.debugRead(buffer); buffer.clear(); System.out.println("after reading"); } } } catch (IOException e) { e.printStackTrace(); } } } 客户端代码 public class Client { public static void main(String[] args) { try (SocketChannel socketChannel = SocketChannel.open()) { // 建立连接 socketChannel.connect(new InetSocketAddress("localhost", 8080)); System.out.println("waiting..."); } catch (IOException e) { e.printStackTrace(); } } } 运行结果 客户端 - 服务器建立连接前:服务器端因 accept 阻塞 客户端 - 服务器建立连接后,客户端发送消息前:服务器端因通道为空被阻塞 客户端发送数据后,服务器处理通道中的数据。再次进入循环时,再次被 accept 阻塞 之前的客户端再次发送消息,服务器端因为被 accept 阻塞,无法处理之前客户端发送到通道中的信息 2、非阻塞 可以通过 ServerSocketChannel 的 configureBlocking (false) 方法将 获得连接设置为非阻塞的。此时若没有连接,accept 会返回 null 可以通过 SocketChannel 的 configureBlocking (false) 方法将从通道中 读取数据设置为非阻塞的。若此时通道中没有数据可读,read 会返回 - 1 服务器代码如下 public class Server { public static void main(String[] args) { // 创建缓冲区 ByteBuffer buffer = ByteBuffer.allocate(16); // 获得服务器通道 try(ServerSocketChannel server = ServerSocketChannel.open()) { // 设置为非阻塞模式,没有连接时返回null,不会阻塞线程 server.configureBlocking(false); // 为服务器通道绑定端口 server.bind(new InetSocketAddress(8080)); // 用户存放连接的集合 ArrayList<SocketChannel> channels = new ArrayList<>(); // 循环接收连接 while (true) { SocketChannel socketChannel = server.accept(); // 通道不为空时才将连接放入到集合中 if (socketChannel != null) { System.out.println("after connecting..."); channels.add(socketChannel); } // 循环遍历集合中的连接 for(SocketChannel channel : channels) { // 处理通道中的数据 // 设置为非阻塞模式,若通道中没有数据,会返回0,不会阻塞线程 channel.configureBlocking(false); int read = channel.read(buffer); if(read > 0) { buffer.flip(); ByteBufferUtil.debugRead(buffer); buffer.clear(); System.out.println("after reading"); } } } } catch (IOException e) { e.printStackTrace(); } } } 这样写存在一个问题,因为设置为了非阻塞,会一直执行 while (true) 中的代码,CPU 一直处于忙碌状态,会使得性能变低,所以实际情况中不使用这种方法处理请求 3、Selector 多路复用 单线程可以配合 Selector 完成对多个 Channel 可读写事件的监控,这称之为多路复用 多路复用仅针对网络 IO,普通文件 IO 无法利用多路复用 如果不用 Selector 的非阻塞模式,线程大部分时间都在做无用功,而 Selector 能够保证 有可连接事件时才去连接 有可读事件才去读取 有可写事件才去写入 限于网络传输能力,Channel 未必时时可写,一旦 Channel 可写,会触发 Selector 的可写事件 4、使用及 Accpet 事件 要使用 Selector 实现多路复用,服务端代码如下改进 public class SelectServer { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.allocate(16); // 获得服务器通道 try(ServerSocketChannel server = ServerSocketChannel.open()) { server.bind(new InetSocketAddress(8080)); // 创建选择器 Selector selector = Selector.open(); // 通道必须设置为非阻塞模式 server.configureBlocking(false); // 将通道注册到选择器中,并设置感兴趣的事件 server.register(selector, SelectionKey.OP_ACCEPT); while (true) { // 若没有事件就绪,线程会被阻塞,反之不会被阻塞。从而避免了CPU空转 // 返回值为就绪的事件个数 int ready = selector.select(); System.out.println("selector ready counts : " + ready); // 获取所有事件 Set<SelectionKey> selectionKeys = selector.selectedKeys(); // 使用迭代器遍历事件 Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); // 判断key的类型 if(key.isAcceptable()) { // 获得key对应的channel ServerSocketChannel channel = (ServerSocketChannel) key.channel(); System.out.println("before accepting..."); // 获取连接并处理,而且是必须处理,否则需要取消 SocketChannel socketChannel = channel.accept(); System.out.println("after accepting..."); // 处理完毕后移除 iterator.remove(); } } } } catch (IOException e) { e.printStackTrace(); } } } 步骤解析 获得选择器 Selector Selector selector = Selector.open(); 将通道设置为非阻塞模式,并注册到选择器中,并设置感兴趣的事件 channel 必须工作在非阻塞模式 FileChannel 没有非阻塞模式,因此不能配合 selector 一起使用 绑定的事件类型可以有 connect - 客户端连接成功时触发 accept - 服务器端成功接受连接时触发 read - 数据可读入时触发,有因为接收能力弱,数据暂不能读入的情况 write - 数据可写出时触发,有因为发送能力弱,数据暂不能写出的情况 // 通道必须设置为非阻塞模式 server.configureBlocking(false); // 将通道注册到选择器中,并设置感兴趣的实践 server.register(selector, SelectionKey.OP_ACCEPT); 通过 Selector 监听事件,并获得就绪的通道个数,若没有通道就绪,线程会被阻塞 阻塞直到绑定事件发生 int count = selector.select(); 阻塞直到绑定事件发生,或是超时(时间单位为 ms) int count = selector.select(long timeout); 不会阻塞,也就是不管有没有事件,立刻返回,自己根据返回值检查是否有事件 int count = selector.selectNow(); 获取就绪事件并得到对应的通道,然后进行处理 // 获取所有事件 Set<SelectionKey> selectionKeys = selector.selectedKeys(); // 使用迭代器遍历事件 Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); // 判断key的类型,此处为Accept类型 if(key.isAcceptable()) { // 获得key对应的channel ServerSocketChannel channel = (ServerSocketChannel) key.channel(); // 获取连接并处理,而且是必须处理,否则需要取消 SocketChannel socketChannel = channel.accept(); // 处理完毕后移除 iterator.remove(); } } 事件发生后能否不处理 事件发生后,要么处理,要么取消(cancel),不能什么都不做,否则下次该事件仍会触发,这是因为 nio 底层使用的是水平触发 5、Read 事件 在 Accept 事件中,若有客户端与服务器端建立了连接,需要将其对应的 SocketChannel 设置为非阻塞,并注册到选择其中 添加 Read 事件,触发后进行读取操作 添加 Read 事件,触发后进行读取操作 public class SelectServer { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.allocate(16); // 获得服务器通道 try(ServerSocketChannel server = ServerSocketChannel.open()) { server.bind(new InetSocketAddress(8080)); // 创建选择器 Selector selector = Selector.open(); // 通道必须设置为非阻塞模式 server.configureBlocking(false); // 将通道注册到选择器中,并设置感兴趣的实践 server.register(selector, SelectionKey.OP_ACCEPT); // 为serverKey设置感兴趣的事件 while (true) { // 若没有事件就绪,线程会被阻塞,反之不会被阻塞。从而避免了CPU空转 // 返回值为就绪的事件个数 int ready = selector.select(); System.out.println("selector ready counts : " + ready); // 获取所有事件 Set<SelectionKey> selectionKeys = selector.selectedKeys(); // 使用迭代器遍历事件 Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); // 判断key的类型 if(key.isAcceptable()) { // 获得key对应的channel ServerSocketChannel channel = (ServerSocketChannel) key.channel(); System.out.println("before accepting..."); // 获取连接 SocketChannel socketChannel = channel.accept(); System.out.println("after accepting..."); // 设置为非阻塞模式,同时将连接的通道也注册到选择其中 socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); // 处理完毕后移除 iterator.remove(); } else if (key.isReadable()) { SocketChannel channel = (SocketChannel) key.channel(); System.out.println("before reading..."); channel.read(buffer); System.out.println("after reading..."); buffer.flip(); ByteBufferUtil.debugRead(buffer); buffer.clear(); // 处理完毕后移除 iterator.remove(); } } } } catch (IOException e) { e.printStackTrace(); } } } 删除事件 当处理完一个事件后,一定要调用迭代器的 remove 方法移除对应事件,否则会出现错误。原因如下 以我们上面的 Read 事件 的代码为例 当调用了 server.register (selector, SelectionKey.OP_ACCEPT) 后,Selector 中维护了一个集合,用于存放 SelectionKey 以及其对应的通道 // WindowsSelectorImpl 中的 SelectionKeyImpl数组 private SelectionKeyImpl[] channelArray = new SelectionKeyImpl[8]; public class SelectionKeyImpl extends AbstractSelectionKey { // Key对应的通道 final SelChImpl channel; ... } 当选择器中的通道对应的事件发生后,selecionKey 会被放到另一个集合中,但是 selecionKey 不会自动移除,所以需要我们在处理完一个事件后,通过迭代器手动移除其中的 selecionKey。否则会导致已被处理过的事件再次被处理,就会引发错误 断开处理 当客户端与服务器之间的连接断开时,会给服务器端发送一个读事件,对异常断开和正常断开需要加以不同的方式进行处理 正常断开 正常断开时,服务器端的 channel.read (buffer) 方法的返回值为 - 1,所以当结束到返回值为 - 1 时,需要调用 key 的 cancel 方法取消此事件,并在取消后移除该事件 int read = channel.read(buffer); // 断开连接时,客户端会向服务器发送一个写事件,此时read的返回值为-1 if(read == -1) { // 取消该事件的处理 key.cancel(); channel.close(); } else { ... } // 取消或者处理,都需要移除key iterator.remove(); 异常断开 异常断开时,会抛出 IOException 异常, 在 try-catch 的 catch 块中捕获异常并调用 key 的 cancel 方法即可 消息边界 不处理消息边界存在的问题 将缓冲区的大小设置为 4 个字节,发送 2 个汉字(你好),通过 decode 解码并打印时,会出现乱码 ByteBuffer buffer = ByteBuffer.allocate(4); // 解码并打印 System.out.println(StandardCharsets.UTF_8.decode(buffer)); 你� �� 这是因为 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 获取到对应通道的附件 public final SelectionKey register(Selector sel, int ops, Object att) 可通过 SelectionKey 的 attachment () 方法获得附件 ByteBuffer buffer = (ByteBuffer) key.attachment(); 我们需要在 Accept 事件发生后,将通道注册到 Selector 中时,对每个通道添加一个 ByteBuffer 附件,让每个通道发生读事件时都使用自己的通道,避免与其他通道发生冲突而导致问题 // 设置为非阻塞模式,同时将连接的通道也注册到选择其中,同时设置附件 socketChannel.configureBlocking(false); ByteBuffer buffer = ByteBuffer.allocate(16); // 添加通道对应的Buffer附件 socketChannel.register(selector, SelectionKey.OP_READ, buffer); 当 Channel 中的数据大于缓冲区时,需要对缓冲区进行扩容操作。此代码中的扩容的判定方法: Channel 调用 compact 方法后,的 position 与 limit 相等,说明缓冲区中的数据并未被读取(容量太小),此时创建新的缓冲区,其大小扩大为两倍。同时还要将旧缓冲区中的数据拷贝到新的缓冲区中,同时调用 SelectionKey 的 attach 方法将新的缓冲区作为新的附件放入 SelectionKey 中 // 如果缓冲区太小,就进行扩容 if (buffer.position() == buffer.limit()) { ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity()*2); // 将旧buffer中的内容放入新的buffer中 ewBuffer.put(buffer); // 将新buffer作为附件放到key中 key.attach(newBuffer); } 改造后的服务器代码如下 public class SelectServer { public static void main(String[] args) { // 获得服务器通道 try(ServerSocketChannel server = ServerSocketChannel.open()) { server.bind(new InetSocketAddress(8080)); // 创建选择器 Selector selector = Selector.open(); // 通道必须设置为非阻塞模式 server.configureBlocking(false); // 将通道注册到选择器中,并设置感兴趣的事件 server.register(selector, SelectionKey.OP_ACCEPT); // 为serverKey设置感兴趣的事件 while (true) { // 若没有事件就绪,线程会被阻塞,反之不会被阻塞。从而避免了CPU空转 // 返回值为就绪的事件个数 int ready = selector.select(); System.out.println("selector ready counts : " + ready); // 获取所有事件 Set<SelectionKey> selectionKeys = selector.selectedKeys(); // 使用迭代器遍历事件 Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); // 判断key的类型 if(key.isAcceptable()) { // 获得key对应的channel ServerSocketChannel channel = (ServerSocketChannel) key.channel(); System.out.println("before accepting..."); // 获取连接 SocketChannel socketChannel = channel.accept(); System.out.println("after accepting..."); // 设置为非阻塞模式,同时将连接的通道也注册到选择其中,同时设置附件 socketChannel.configureBlocking(false); ByteBuffer buffer = ByteBuffer.allocate(16); socketChannel.register(selector, SelectionKey.OP_READ, buffer); } else if (key.isReadable()) { SocketChannel channel = (SocketChannel) key.channel(); System.out.println("before reading..."); // 通过key获得附件(buffer) ByteBuffer buffer = (ByteBuffer) key.attachment(); int read = channel.read(buffer); if(read == -1) { key.cancel(); channel.close(); } else { // 通过分隔符来分隔buffer中的数据 split(buffer); // 如果缓冲区太小,就进行扩容 if (buffer.position() == buffer.limit()) { ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity()*2); // 将旧buffer中的内容放入新的buffer中 buffer.flip(); newBuffer.put(buffer); // 将新buffer放到key中作为附件 key.attach(newBuffer); } } System.out.println("after reading..."); } } } } catch (IOException e) { e.printStackTrace(); } } private static void split(ByteBuffer buffer) { buffer.flip(); for(int i = 0; i < buffer.limit(); i++) { // 遍历寻找分隔符 // get(i)不会移动position if (buffer.get(i) == '\n') { // 缓冲区长度 int length = i+1-buffer.position(); ByteBuffer target = ByteBuffer.allocate(length); // 将前面的内容写入target缓冲区 for(int j = 0; j < length; j++) { // 将buffer中的数据写入target中 target.put(buffer.get()); } // 打印结果 ByteBufferUtil.debugAll(target); } } // 切换为写模式,但是缓冲区可能未读完,这里需要使用compact buffer.compact(); } } 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 中 int write = socket.write(buffer); // 通道中可能无法放入缓冲区中的所有数据 if (buffer.hasRemaining()) { // 注册到Selector中,关注可写事件,并将buffer添加到key的附件中 socket.configureBlocking(false); socket.register(selector, SelectionKey.OP_WRITE, buffer); } 添加写事件的相关操作 key.isWritable(),对 Buffer 再次进行写操作 每次写后需要判断 Buffer 中是否还有数据(是否写完)。若写完,需要移除 SelecionKey 中的 Buffer 附件,避免其占用过多内存,同时还需移除对写事件的关注 SocketChannel socket = (SocketChannel) key.channel(); // 获得buffer ByteBuffer buffer = (ByteBuffer) key.attachment(); // 执行写操作 int write = socket.write(buffer); System.out.println(write); // 如果已经完成了写操作,需要移除key中的附件,同时不再对写事件感兴趣 if (!buffer.hasRemaining()) { key.attach(null); key.interestOps(0); } 整体代码如下 public class WriteServer { public static void main(String[] args) { try(ServerSocketChannel server = ServerSocketChannel.open()) { server.bind(new InetSocketAddress(8080)); server.configureBlocking(false); Selector selector = Selector.open(); server.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); // 处理后就移除事件 iterator.remove(); if (key.isAcceptable()) { // 获得客户端的通道 SocketChannel socket = server.accept(); // 写入数据 StringBuilder builder = new StringBuilder(); for(int i = 0; i < 500000000; i++) { builder.append("a"); } ByteBuffer buffer = StandardCharsets.UTF_8.encode(builder.toString()); // 先执行一次Buffer->Channel的写入,如果未写完,就添加一个可写事件 int write = socket.write(buffer); System.out.println(write); // 通道中可能无法放入缓冲区中的所有数据 if (buffer.hasRemaining()) { // 注册到Selector中,关注可写事件,并将buffer添加到key的附件中 socket.configureBlocking(false); socket.register(selector, SelectionKey.OP_WRITE, buffer); } } else if (key.isWritable()) { SocketChannel socket = (SocketChannel) key.channel(); // 获得buffer ByteBuffer buffer = (ByteBuffer) key.attachment(); // 执行写操作 int write = socket.write(buffer); System.out.println(write); // 如果已经完成了写操作,需要移除key中的附件,同时不再对写事件感兴趣 if (!buffer.hasRemaining()) { key.attach(null); key.interestOps(0); } } } } } catch (IOException e) { e.printStackTrace(); } } } 7、优化 多线程优化 充分利用多核 CPU,分两组选择器 单线程配一个选择器(Boss),专门处理 accept 事件 创建 cpu 核心数的线程(Worker),每个线程配一个选择器,轮流处理 read 事件 实现思路 创建一个负责处理 Accept 事件的 Boss 线程,与多个负责处理 Read 事件的 Worker 线程 Boss 线程执行的操作 接受并处理 Accepet 事件,当 Accept 事件发生后,调用 Worker 的 register (SocketChannel socket) 方法,让 Worker 去处理 Read 事件,其中需要根据标识 robin 去判断将任务分配给哪个 Worker // 创建固定数量的Worker Worker[] workers = new Worker[4]; // 用于负载均衡的原子整数 AtomicInteger robin = new AtomicInteger(0); // 负载均衡,轮询分配Worker workers[robin.getAndIncrement()% workers.length].register(socket); register (SocketChannel socket) 方法会通过同步队列完成 Boss 线程与 Worker 线程之间的通信,让 SocketChannel 的注册任务被 Worker 线程执行。添加任务后需要调用 selector.wakeup () 来唤醒被阻塞的 Selector public void register(final SocketChannel socket) throws IOException { // 只启动一次 if (!started) { // 初始化操作 } // 向同步队列中添加SocketChannel的注册事件 // 在Worker线程中执行注册事件 queue.add(new Runnable() { @Override public void run() { try { socket.register(selector, SelectionKey.OP_READ); } catch (IOException e) { e.printStackTrace(); } } }); // 唤醒被阻塞的Selector // select类似LockSupport中的park,wakeup的原理类似LockSupport中的unpark selector.wakeup(); } Worker 线程执行的操作 从同步队列中获取注册任务,并处理 Read 事件 实现代码 public class ThreadsServer { public static void main(String[] args) { try (ServerSocketChannel server = ServerSocketChannel.open()) { // 当前线程为Boss线程 Thread.currentThread().setName("Boss"); server.bind(new InetSocketAddress(8080)); // 负责轮询Accept事件的Selector Selector boss = Selector.open(); server.configureBlocking(false); server.register(boss, SelectionKey.OP_ACCEPT); // 创建固定数量的Worker Worker[] workers = new Worker[4]; // 用于负载均衡的原子整数 AtomicInteger robin = new AtomicInteger(0); for(int i = 0; i < workers.length; i++) { workers[i] = new Worker("worker-"+i); } while (true) { boss.select(); Set<SelectionKey> selectionKeys = boss.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while (iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); // BossSelector负责Accept事件 if (key.isAcceptable()) { // 建立连接 SocketChannel socket = server.accept(); System.out.println("connected... "); socket.configureBlocking(false); // socket注册到Worker的Selector中 System.out.println("before read..."); // 负载均衡,轮询分配Worker workers[robin.getAndIncrement()% workers.length].register(socket); System.out.println("after read..."); } } } } catch (IOException e) { e.printStackTrace(); } } static class Worker implements Runnable { private Thread thread; private volatile Selector selector; private String name; private volatile boolean started = false; /** * 同步队列,用于Boss线程与Worker线程之间的通信 */ private ConcurrentLinkedQueue<Runnable> queue; public Worker(String name) { this.name = name; } public void register(final SocketChannel socket) throws IOException { // 只启动一次 if (!started) { thread = new Thread(this, name); selector = Selector.open(); queue = new ConcurrentLinkedQueue<>(); thread.start(); started = true; } // 向同步队列中添加SocketChannel的注册事件 // 在Worker线程中执行注册事件 queue.add(new Runnable() { @Override public void run() { try { socket.register(selector, SelectionKey.OP_READ); } catch (IOException e) { e.printStackTrace(); } } }); // 唤醒被阻塞的Selector // select类似LockSupport中的park,wakeup的原理类似LockSupport中的unpark selector.wakeup(); } @Override public void run() { while (true) { try { selector.select(); // 通过同步队列获得任务并运行 Runnable task = queue.poll(); if (task != null) { // 获得任务,执行注册操作 task.run(); } Set<SelectionKey> selectionKeys = selector.selectedKeys(); Iterator<SelectionKey> iterator = selectionKeys.iterator(); while(iterator.hasNext()) { SelectionKey key = iterator.next(); iterator.remove(); // Worker只负责Read事件 if (key.isReadable()) { // 简化处理,省略细节 SocketChannel socket = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(16); socket.read(buffer); buffer.flip(); ByteBufferUtil.debugAll(buffer); } } } catch (IOException e) { e.printStackTrace(); } } } } } 本文由传智教育博学谷教研团队发布。 如果本文对您有帮助,欢迎关注和点赞;如果您有任何建议也可留言评论或私信,您的支持是我坚持创作的动力。 转载请注明出处!

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码