您现在的位置是:首页 > 文章详情

🔥 smart-socket 新版发布,柔性内存池方案

日期:2024-12-02点击:159

1、smart-socket 简介

smart-socket 是一款增强了原生 JDK 实现的 AIO 通信框架。

换言之,smart-socket 100% 遵循 JDK 对于 AIO 接口规范的定义,只是重新提供了一套代码实现。

使其有着相较 JDK 官方 AIO :

  • 更高的通信性能

  • 更少的资源开销

  • 更稳定的运行保障。

产品特色:

  • 极简:于2017年开源至今发布了数十次版本,核心代码量始终控制在2500行以内,总代码量不足5000行。

  • 易用:5分钟上手(前提:未曾遭受网上错误编解码知识的毒害)

  • 高性能:以算法之力充分驱动硬件算力之势,基于smart-socket的服务在通信性能方面可轻松超过其他计算机语言开发的程序,包括且不限于:C/C++、Golang、Rust、Erlang。

为什么开发 smart-socket?

  • AIO 是一个面向开发人员更友好的设计理念,值得被更多人应用。

  • 原生 JDK 提供的实现存在性能问题,其线程模型限制了 IO 调度效率。

  • 原生 JDK 提供的实现存在资源开销问题,连接越多内存需求越高,难以在低规格服务器中支撑百万级长连接。

  • 原生 JDK 提供的实现存在稳定性问题,Mac 系统下进行压测存在不明原因的死机现象。

  • 我们需要一款比 Netty 更容易上手的通信框架。

2、更新说明

本次 smart-socket 为内存池增加了柔性伸缩的方案,能够在性能几乎无损的情况下,使得应用的内存开销大幅缩减。

在此前的 smart-socket 内存池方案中,会根据服务的预估并发量,提前分配合适的内存空间。如此虽然使通信性能持续维持在最佳状态,但由于存在固定的内存开销,使得流量低谷期的内存池利用率不高。

以下图为例,进行压力测试时 QPS 将近 800W/s,IO流量达到 500MB/s,为其分配的内存池容量从服务启动之后就一直维持在固定水位。

而当采用柔性内存池方案时,随着流量波峰的来临,内存会根据实际需求进行扩容。当压测结束,内存会层阶梯状逐步回收(下图)。

3、我们的用户

smart-socket 开源以来深受广大用户的青睐。

目前 smart-socket 在 Gitee 上已收获 4K+ Star,Fork 数超 1K;仓库访问量 12+ 万,源码下载量超4万。在此衷心感谢各位朋友对 smart-socket 的支持和信任。

篇幅有限,仅展示部分...

4、快速上手

4.1 引入Maven依赖

<dependencies>
    <dependency>
        <groupId>io.github.smartboot.socket</groupId>
        <artifactId>aio-core</artifactId>
        <version>${version}</version>
    </dependency>
</dependencies>

4.2 定义协议

这里提供的示例是一种简单的字符串通信协议,仅作效果演示。实际场景中还需根据通信双方约定的协议实现编解码算法。

import org.smartboot.socket.Protocol;
import org.smartboot.socket.transport.AioSession;

import java.nio.ByteBuffer;

publicclass StringProtocol implements Protocol<String> {

    @Override
    public String decode(ByteBuffer readBuffer, AioSession session) {
        int remaining = readBuffer.remaining();
        if (remaining < Integer.BYTES) {
            returnnull;
        }
        readBuffer.mark();
        int length = readBuffer.getInt();
        if (length > readBuffer.remaining()) {
            readBuffer.reset();
            returnnull;
        }
        byte[] b = newbyte[length];
        readBuffer.get(b);
        readBuffer.mark();
        returnnew String(b);
    }
}

4.3 启动服务端

服务端通过System.out打印客户端传输过来的字符串内容,并将该内容原样传回至客户端。

import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.transport.AioQuickServer;
import org.smartboot.socket.transport.WriteBuffer;

import java.io.IOException;

publicclass StringServer {

    public static void main(String[] args) throws IOException {
        MessageProcessor<String> processor = (session, msg) -> {
            System.out.println("receive from client: " + msg);
            WriteBuffer outputStream = session.writeBuffer();
            try {
                byte[] bytes = msg.getBytes();
                outputStream.writeInt(bytes.length);
                outputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        };

        AioQuickServer server = new AioQuickServer(8888, new StringProtocol(), processor);
        server.start();
    }
}

try-catch中先后调用writeIntwrite是一种协议编码手法,也是从事通信开发必须要理解和掌握的技能。

4.4 启动客户端

客户端与服务端建立TCP连接后,便向其发送hello smart-socket,当收到服务端的响应消息时,通过MessageProcessor的实现类进行控制台打印。

import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.transport.AioQuickClient;
import org.smartboot.socket.transport.AioSession;
import org.smartboot.socket.transport.WriteBuffer;

import java.io.IOException;

publicclass StringClient {

    public static void main(String[] args) throws IOException {
        MessageProcessor<String> processor = (session, msg) -> System.out.println("receive from server: " + msg);
        AioQuickClient client = new AioQuickClient("localhost", 8888, new StringProtocol(), processor);
        AioSession session = client.start();
        WriteBuffer writeBuffer = session.writeBuffer();
        byte[] data = "hello smart-socket".getBytes();
        writeBuffer.writeInt(data.length);
        writeBuffer.write(data);
        writeBuffer.flush();
    }
}

5、关于组织

smartboot开源组织,一个容易被误认为是在“重复造轮子”的低调组织。曾获得 2020 年度 OSC 中国开源项目「优秀 Gitee 组织 」荣誉。

该组织内的明星项目包括:

  • smart-socket
    历时5年精炼出2千多行代码,轻松实现百万级长连接的 AIO 通信框架。

  • smart-http
    基于 smart-socket 实现的 HTTP/1.1 web服务。

  • smart-servlet
    基于 smart-http 实现的 Servlet 4.0 容器服务。

  • smart-mqtt
    基于 smart-socket 实现的 MQTT 3.1.1/5.0 Broker&Client 服务。

  • smart-flow
    一款具备可观测性的轻量级业务编排框架。

组织地址:https://smartboot.tech/
代码仓库:https://gitee.com/smartboot

原文链接:https://www.oschina.net/news/322902/smart-socket-updated
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章