分佈式架構之 Smart-Socket

一、Smart-Socket 是什麼?

Smart-Socket 是一款國產開源的 Java AIO 通信框架,支持 TCP、UDP、SSL/TLS 。

作爲一款極簡、易用、高性能的通信框架,現已廣泛運用於物聯網、證券、電力、電商等諸多領域。

二、Smart-Socket 的優勢有哪些?

三、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