smart-socket:一套更“轻”的 Java AIO 通信框架,上手只要 2 个接口

20 阅读2分钟

smart-socket:一套更“轻”的 Java AIO 通信框架,上手只要 2 个接口

简介

smart-socket 是 smartboot 组织开源的 Java AIO(Asynchronous I/O,异步 IO)通信框架:项目自述为“100% 遵循 JDK AIO 接口规范,但重新实现一套代码”,以获得更高通信性能、更少资源开销与更稳定运行;使用上主打极简(核心只需实现 Protocol + MessageProcessor 两个接口),并提供 SSL/TLS、心跳、断链重连、黑名单、监控等插件能力(见项目 README 插件清单)。

推荐理由

  • 定位清晰:面向 TCP/长连接/自定义协议的通信层(AIO),更接近“写协议服务”的需求,而不是 Web 框架
  • 上手路径短:官方强调二次开发只需实现 ProtocolMessageProcessor 两个接口(README)
  • 插件化:官方列出内置插件(TLS/SSL、心跳、断链重连、监控、黑名单、内存池监测等,README)

适合/不适合

  • 适合:你要做自定义二进制协议/物联网网关/长连接服务;团队想在“比 Netty 更轻的学习成本”下落地通信层;能接受自己把协议、粘包拆包、异常与监控补齐
  • 不适合:你要现成的 HTTP/WebSocket 全家桶或成熟中间件生态

替代对比表

项目定位上手成本部署方式生态/集成许可典型坑
smart-socketJava AIO 通信框架(自定义协议/长连接)低(2 个接口起步)Maven插件:TLS/心跳/重连/监控/黑名单等(README)Apache-2.0需要自己设计协议与拆包;版本选择需核对文档与 tag;长连接治理要配合心跳/超时/限流
NettyJava NIO 通信框架/网络基础设施中-高Maven生态极强(大量框架/组件依赖)Apache-2.0概念多(Pipeline/ByteBuf);内存与背压处理容易踩坑
Apache MINAJava 网络应用框架(NIO 为主)Maven生态中等Apache-2.0(待核实:以其官方仓库 LICENSE 为准)维护活跃度与版本节奏需你核对(看最近 tag/commit/issue)

快速上手

前置:JDK 8+(官方文档页写法),Maven。

1)引入依赖

  • GitHub tag 最新:v1.8.2
<dependency>
  <groupId>io.github.smartboot.socket</groupId>
  <artifactId>aio-core</artifactId>
  <version>1.8.2</version>
</dependency>

2)示例:长度字段 + 字符串回显

StringProtocol(长度字段拆包):

import java.nio.ByteBuffer;
import org.smartboot.socket.Protocol;
import org.smartboot.socket.transport.AioSession;

public class StringProtocol implements Protocol<String> {
  @Override
  public String decode(ByteBuffer readBuffer, AioSession session) {
    if (readBuffer.remaining() < Integer.BYTES) return null;
    readBuffer.mark();
    int length = readBuffer.getInt();
    if (length > readBuffer.remaining()) {
      readBuffer.reset();
      return null;
    }
    byte[] b = new byte[length];
    readBuffer.get(b);
    return new String(b);
  }
}

Server:

import java.io.IOException;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.transport.AioQuickServer;
import org.smartboot.socket.transport.WriteBuffer;

public class StringServer {
  public static void main(String[] args) throws IOException {
    MessageProcessor<String> processor = (session, msg) -> {
      WriteBuffer out = session.writeBuffer();
      byte[] bytes = msg.getBytes();
      try {
        out.writeInt(bytes.length);
        out.write(bytes);
      } catch (IOException e) {
        e.printStackTrace();
      }
    };

    new AioQuickServer(8888, new StringProtocol(), processor).start();
    System.out.println("listen :8888");
  }
}

Client:

import java.io.IOException;
import org.smartboot.socket.MessageProcessor;
import org.smartboot.socket.transport.AioQuickClient;
import org.smartboot.socket.transport.AioSession;
import org.smartboot.socket.transport.WriteBuffer;

public class StringClient {
  public static void main(String[] args) throws IOException {
    MessageProcessor<String> processor = (session, msg) -> System.out.println("resp: " + msg);
    AioSession session = new AioQuickClient("127.0.0.1", 8888, new StringProtocol(), processor).start();

    WriteBuffer out = session.writeBuffer();
    byte[] data = "hello smart-socket".getBytes();
    out.writeInt(data.length);
    out.write(data);
    out.flush();
  }
}

开源地址

  • 官网/文档:https://smartboot.tech/smart-socket/
  • GitHub:https://github.com/smartboot/smart-socket
  • Gitee:https://gitee.com/smartboot/smart-socket