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

🎉smart-socket v1.5.36 发布,物联时代的弄潮儿

日期:2023-10-27点击:485

1、smart-socket 简介

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

产品特色:

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

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

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

推荐理由:

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

  2. 轻量化,生成 jar 包仅 62KB。

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

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

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

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

2、 版本更新

  1. 拆解底层 write 线程模型。

  2. 提升进程运行期间动态启停通信服务的资源管理可靠性。

  3. 新增 IdleStatePlugin 插件

  4. 内置线程调整为守护线程,实现优雅停机。

  5. 新增示例代码

这个版本主要带来了 IdleStatePlugin 插件,这是一款对通信链路闲置状态检测的插件。适用于弱网环境下,解决通信双方因无法感知链路异常,导致通信质量受损等问题。

按照 smart-socket 一贯的设计风格,此插件依旧开箱即用,用户可根据自身需求选择以下合适的集成模式。

 // 读、写通道闲置时长超过5秒,则断开连接 processor.addPlugin(new IdleStatePlugin<>(5000)); // 读通道闲置时长超过5秒,则断开连接 processor.addPlugin(new IdleStatePlugin<>(5000,true,false)); // 写通道闲置时长超过5秒,则断开连接 processor.addPlugin(new IdleStatePlugin<>(5000,false,true));

Maven

 <!-- JDK1.8 --> <dependencies> <dependency> <groupId>org.smartboot.socket</groupId> <artifactId>aio-core</artifactId> <version>1.5.36</version> </dependency> </dependencies> 

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、我们的用户

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


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/263621/smart-socket-1-5-36-released
关注公众号

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

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

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

文章评论

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

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章