分佈式架構之 Smart-Socket
一、Smart-Socket 是什麼?
Smart-Socket 是一款國產開源的 Java AIO 通信框架,支持 TCP、UDP、SSL/TLS 。
作爲一款極簡、易用、高性能的通信框架,現已廣泛運用於物聯網、證券、電力、電商等諸多領域。
二、Smart-Socket 的優勢有哪些?
-
- 高性能、高併發、低延遲、低能耗。
-
- 代碼量極少,可讀性強。核心代碼不到 1500 行,工程結構、包層次清晰。
-
- 學習門檻低,二次開發只需實現 2 個接口(Protocol、MessageProcessor), 具備通信開發經驗的幾乎無學習成本。
-
- 良好的線程模型、內存模型設計,保障服務高效穩定的運行。
-
- 支持自定義插件,並已提供了豐富地插件,包括:SSL/TLS 通信插件、心跳插件、斷鏈重連插件、服務指標統計插件、黑名單插件、內存池監測插件。
三、Smart-Socket 的工程結構是怎樣的?
. → 項目倉庫主目錄
├── smart-socket-parent → 項目主模塊
│ │
│ └── pom.xml
│
├── aio-core → 項目基礎子模塊,僅包含 TCP 的 Server、Client 通信服務,以及內存池。
│
├── aio-pro → 項目高級子模塊,提供便於開發所需的高級封裝,同時附帶 UDP 通信能力。
│
├── aio-example → 存放 smart-socket 的使用示例,學習過程中可供參考。
│
└── pom.xml
四、關於 Smart-Socket 相關的信息有哪些?
Smart-Socket 官方網站:
https://smartboot.gitee.io/book/smart-socket/
Smart-Socket 源代碼:
https://github.com/smartboot/smart-socket
https://gitee.com/smartboot/smart-socket
Gitee GVP 項目:
開源活躍情況:
一些企業應用案例:
五、YC-Framework 如何支持 Smart-Socket?
1. 引入 Maven 依賴
<dependency>
<groupId>com.yc.framework</groupId>
<artifactId>yc-common-smart-socket</artifactId>
</dependency>
2. 樣例代碼
(1) 客戶端
public class YcSmartSocketClient {
public static void main(String[] args) throws IOException {
MessageProcessor<String> processor = new MessageProcessor<String>() {
@Override
public void process(AioSession session, String msg) {
System.out.println("receive from server: " + msg);
}
};
AioQuickClient client = new AioQuickClient("localhost", 8888, new YcSmartSocketProtocol(), processor);
AioSession session = client.start();
WriteBuffer writeBuffer = session.writeBuffer();
byte[] data = "hello smart-socket".getBytes();
writeBuffer.writeInt(data.length);
writeBuffer.write(data);
writeBuffer.flush();
}
}
(2) 服務端
public class YcSmartSocketServer {
public static void main(String[] args) throws IOException {
MessageProcessor<String> processor = new MessageProcessor<String>() {
@Override
public void process(AioSession session, String 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 YcSmartSocketProtocol(), processor);
server.start();
}
}
相關示例參考如下:
https://github.com/developers-youcong/yc-framework/tree/main/yc-example/yc-example-smart-socket
以上源代碼均已開源,開源不易,如果對你有幫助,不妨給個 star!!!
YC-Framework 官網:
https://framework.youcongtech.com/
YC-Framework Github 源代碼:
https://github.com/developers-youcong/yc-framework
YC-Framework Gitee 源代碼:
https://gitee.com/developers-youcong/yc-framework
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/mjDskX1igs3zKlIf75ycMA