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

🔥AIO 通信神器:开启物联网时代新篇章!

日期:2023-12-20点击:84

1、smart-socket 简介

smart-socket,一款面向万物互联的 Java 通信框架。

产品特色:

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

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

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

推荐理由:

  1. 国产化,源码 100% 自研,信创首选产品。

  2. 轻量化,生成 jar 包仅 62KB;单机百万长连接内存开销仅4GB。

  3. 插件化,多款实用插件辅助高效通信编程。

  4. 人性化,低至分钟级的学习成本。

舒适的编程体验;高效的运行表现;极少的资源开销。

smart-socket 在多方面都遥遥领先 netty,除了知名度。

2、 版本更新​​​​​​

这个版本作了一些涉及 TCP和 UDP 的通信稳定性优化,算是把早年特地留下的一个坑给填补上了。

当初为了性能考虑,对WriteBuffer#flush作了无锁化设计。这带来的弊端是致使该接口线程不安全,在断开网络连接时有极小概率触发空指针。我自己这么多年总共也有遇到不超过5次。

但是,Solon作者,那个要单挑 Spring 的男人。不知道做了什么神仙操作,竟然能精准踩中这颗雷,并以“用户就是上帝”之势强烈要求我处理这个问题。

虽然内心是拒绝的,但为了能过个安稳年,不得不耗费几晚思考接口的重构。所幸最终呈现的效果还是令我满意的,即修复了问题,又没让性能受到丝毫损伤。

至于解决方案的具体细节,我就不在这里展开了。smart-socket 的微内核模块总共只有两千行代码,十分钟就能浏览完我八年的成果,所以有兴趣的人可以自行查阅。

3、快速上手

3.1 引入 Maven 依赖

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

3.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); } } 

3.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 是一种协议编码手法,也是从事通信开发必须要理解和掌握的技能。

3.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(); } } 

4、我们的用户

smart-socket 开源以来深受广大用户的青睐。其中有世界 500 强的知名企业;也有 Solon 之父(江湖人称单挑 Spring 的男人);还有无数家从事物联网领域的公司。

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

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


5、关于组织

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

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

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

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

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

  • 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/271822/smart-socket-updated
关注公众号

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

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

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

文章评论

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

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章