SAEA.RPC 26.11.24.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package SAEA.RPC --version 26.11.24.1
                    
NuGet\Install-Package SAEA.RPC -Version 26.11.24.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SAEA.RPC" Version="26.11.24.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SAEA.RPC" Version="26.11.24.1" />
                    
Directory.Packages.props
<PackageReference Include="SAEA.RPC" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SAEA.RPC --version 26.11.24.1
                    
#r "nuget: SAEA.RPC, 26.11.24.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SAEA.RPC@26.11.24.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SAEA.RPC&version=26.11.24.1
                    
Install as a Cake Addin
#tool nuget:?package=SAEA.RPC&version=26.11.24.1
                    
Install as a Cake Tool

SAEA.RPC - 高性能远程过程调用框架 🚀

NuGet version License

English Version | 中文版

基于 SAEA.Sockets 异步 IO 的高性能 RPC 框架,二进制传输,性能远超 HTTP/JSON RPC。

⚡ 零拷贝内核:构建于 SAEA.Sockets 的 Span/Memory 零拷贝收发与内存池通道之上,显著降低分配与 GC。

快速导航 🧭

章节 内容
⚡ 30秒快速开始 最简单的上手示例
🎯 核心特性 框架的主要功能
📐 架构设计 组件关系与调用流程
💡 应用场景 何时选择 SAEA.RPC
📊 性能对比 与 HTTP RPC 对比
❓ 常见问题 FAQ 快速解答
🔧 核心类 主要类一览
📝 使用示例 详细代码示例

30秒快速开始 ⚡

最快上手方式,只需3步即可运行 RPC 服务:

Step 1: 安装 NuGet 包

dotnet add package SAEA.RPC

Step 2: 创建 RPC 服务端(仅需5行代码)

using SAEA.RPC;

[RPCService]
public class HelloService
{
    public string SayHello(string name) => $"Hello, {name}!";
}

var provider = new ServiceProvider(port: 39654);
provider.Start();

Step 3: 创建 RPC 客户端调用

var proxy = new RPCServiceProxy("rpc://127.0.0.1:39654");
var result = proxy.HelloService.SayHello("SAEA");
Console.WriteLine(result);  // 输出: Hello, SAEA!

就这么简单! 🎉 你已经实现了一个高性能的 RPC 远程调用系统。


核心特性 🎯

特性 说明 优势
🔥 极简使用 [RPCService] 标记即可 零配置,几行代码完成服务搭建
⚡ 跨平台异步 IO 基于 SAEA.Sockets 面向高并发,低延迟响应
📦 二进制序列化 Protobuf 高效传输 数据量小,序列化快
🔌 动态代理 自动生成客户端代码 像调用本地方法一样调用远程服务
🛡️ AOP 过滤器 ActionFilterAtrribute 日志、权限、缓存统一拦截
📡 服务端推送 Notice<T> 推送消息 支持发布订阅模式
⚖️ 连接复用 ConsumerMultiplexer 同一服务地址按 links 复用连接
🔗 连接池 长连接复用 减少连接开销,提升性能

架构设计 📐

组件架构图

┌─────────────────────────────────────────────────────────────┐
│                     SAEA.RPC 架构                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌─────────────────────┐      ┌─────────────────────┐      │
│  │    Client 端        │      │    Server 端        │      │
│  │                     │      │                     │      │
│  │  ┌───────────────┐  │      │  ┌───────────────┐  │      │
│  │  │RPCServiceProxy│  │      │  │ ServiceProvider│  │      │
│  │  │  (动态代理)    │  │      │  │  (服务提供者)  │  │      │
│  │  └───────┬───────┘  │      │  └───────┬───────┘  │      │
│  │          │          │      │          │          │      │
│  │  ┌───────▼───────┐  │      │  ┌───────▼───────┐  │      │
│  │  │ServiceConsumer│  │      │  │ [RPCService]  │  │      │
│  │  │  (消费者)     │  │      │  │  (服务类)     │  │      │
│  │  └───────┬───────┘  │      │  └───────┬───────┘  │      │
│  │          │          │      │          │          │      │
│  │  ┌───────▼───────┐  │      │  ┌───────▼───────┐  │      │
│  │  │ConsumerMulti- │  │      │  │ RPCMapping    │  │      │
│  │  │ plexer(连接池) │  │      │  │ (服务映射)    │  │      │
│  │  └───────┬───────┘  │      │  └───────┬───────┘  │      │
│  └──────────┼──────────┘      └──────────┼──────────┘      │
│             │                            │                  │
│             └────────────┬───────────────┘                  │
│                          │                                  │
│                 ┌────────▼────────┐                        │
│                 │  SAEA.Sockets   │                        │
│                 │    (异步 IO)    │                        │
│                 └─────────────────┘                        │
│                                                             │
└─────────────────────────────────────────────────────────────┘

RPC 调用流程图

客户端调用流程:

Client                        Server
  │                             │
  │  proxy.Method(args)         │
  │         │                   │
  │    ┌────▼────┐              │
  │    │ 动态代理 │              │
  │    │生成调用  │              │
  │    └────┬────┘              │
  │         │                   │
  │    ┌────▼────┐              │
  │    │Protobuf │              │
  │    │ 序列化  │              │
  │    └────┬────┘              │
  │         │                   │
  │    ┌────▼────┐   TCP/异步  ┌▼────────┐
  │    │发送请求 ├──────────────►接收请求 │
  │    └─────────┘              └────┬────┘
  │                                  │
  │                            ┌─────▼─────┐
  │                            │ RPCMapping│
  │                            │ 查找服务  │
  │                            └─────┬─────┘
  │                                  │
  │                            ┌─────▼─────┐
  │                            │ 调用方法  │
  │                            │执行逻辑   │
  │                            └─────┬─────┘
  │                                  │
  │                            ┌─────▼─────┐
  │                            │Protobuf   │
  │                            │ 序列化    │
  │                            └─────┬─────┘
  │                                  │
  │         ┌────────────────────────┘
  │    ┌────▼────┐              ┌────▼────┐
  │    │接收响应 ◄──────────────┤发送响应 │
  │    └────┬────┘              └─────────┘
  │         │
  │    ┌────▼────┐
  │    │Protobuf │
  │    │ 反序列化│
  │    └────┬────┘
  │         │
  │    ┌────▼────┐
  │    │返回结果 │
  │    └─────────┘
  │                             │

应用场景 💡

✅ 适合使用 SAEA.RPC 的场景

场景 描述 推荐理由
🏢 微服务通信 服务间高性能调用 二进制传输,低延迟,高吞吐
🎮 游戏后端 游戏服务器间通信 异步 IO 支撑高并发,实时性强
🤖 分布式系统 节点间协调通信 长连接复用,减少握手开销
📊 实时数据处理 数据管道服务 高效序列化,处理速度快
🔧 内部 API 内部服务调用 相比 HTTP REST,性能提升显著
💾 数据访问层 数据服务代理 支持复杂对象传输

❌ 不适合的场景

场景 推荐替代方案
浏览器客户端调用 使用 SAEA.WebSocket 或 REST API
跨语言调用 使用 gRPC 或 HTTP API
公开外部 API 使用 SAEA.Http 或 ASP.NET WebAPI
简单配置服务 使用配置中心

性能对比 📊

与 HTTP RPC 对比

指标 SAEA.RPC HTTP/JSON RPC 优势
序列化方式 Protobuf 二进制 JSON 文本 体积更小
传输协议 TCP 长连接 HTTP 短连接 减少握手开销
并发模型 异步 IO 阻塞/异步 CPU 利用率高
延迟 低(长连接 + 二进制) 较高(短连接 + 文本解析) 减少握手与解析开销
吞吐量 高 较低 更高吞吐
连接复用 支持 需 Keep-Alive 原生支持

📌 上表为定性比较,未发布跨框架基准数据;请针对自身负载实测。

序列化性能对比

格式 相对数据大小 序列化速度 反序列化速度
Protobuf 小 ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐⭐
JSON 中 ⭐⭐⭐ ⭐⭐⭐
XML 大 ⭐⭐ ⭐⭐

💡 提示: SAEA.RPC 使用 Protobuf 二进制序列化,数据体积小、速度快,是高性能 RPC 的最佳选择。


常见问题 ❓

Q1: SAEA.RPC 与 gRPC 有什么区别?

A: | 特性 | SAEA.RPC | gRPC | |------|----------|------| | 学习曲线 | 简单 | 中等 | | 跨语言 | 仅 .NET | 多语言支持 | | 传输协议 | 自定义 TCP | HTTP/2 | | 适用场景 | .NET 内部通信 | 跨语言微服务 |

推荐在纯 .NET 环境下使用 SAEA.RPC,更简单高效。

Q2: 如何实现服务注册与发现?

A: ConsumerMultiplexer 负责同一服务地址上的多连接复用(links 指定连接数,调用在连接间轮转);多地址由使用方选好地址后分别创建:

var urls = new[]
{
    "rpc://192.168.1.1:39654",
    "rpc://192.168.1.2:39654"
};
var index = 0;
string NextUrl() => urls[index++ % urls.Length];

// 取一个地址创建消费者;同一地址内部再按 links 复用连接
var proxy = new RPCServiceProxy(NextUrl(), links: 4);

也可结合 Consul、Nacos 等服务发现组件动态更新地址列表。

Q3: 如何处理超时和异常?

A: 传输、调用与超时异常统一汇入 ExceptionCollector:

ExceptionCollector.OnErr += (name, ex) =>
{
    Console.WriteLine($"[{name}] RPC 调用异常: {ex.Message}");
};

var proxy = new RPCServiceProxy("rpc://127.0.0.1:39654");

服务端共用同一个异常汇,无需单独订阅:

var provider = new ServiceProvider(port: 39654);
provider.Start();

需要按连接观察链上异常时,用 ConfigurePipeline 追加处理器。回调必须在 Start() 之前赋值,服务启动后再赋值会抛 InvalidOperationException:

var provider = new ServiceProvider(port: 39654);
provider.ConfigurePipeline = (id, pipeline) => pipeline.AddLast(new ExceptionLogHandler());
provider.Start();

消费者在构造时就建立连接,链启动后即冻结,构造完成后再赋值会抛 InvalidOperationException,因此装配回调必须作为构造参数传入:

var proxy = new RPCServiceProxy("rpc://127.0.0.1:39654", 4, 5, 10 * 1000,
    (id, pipeline) => pipeline.AddLast(new ExceptionLogHandler()));
/// <summary>
/// 链上异常观察器:打印后继续交给链尾收口
/// </summary>
public sealed class ExceptionLogHandler : InboundHandlerAdapter
{
    public override Task ChannelExceptionCaughtAsync(IChannelHandlerContext ctx, Exception ex)
    {
        Console.WriteLine($"{ctx.ChannelId} RPC 异常: {ex.Message}");
        return ctx.FireExceptionCaughtAsync(ex);
    }
}

Q4: 如何实现日志记录和权限验证?

A: 使用 AOP 过滤器 ActionFilterAtrribute:

using SAEA.RPC;
using SAEA.Sockets.Pipeline;

public class AuthAttribute : ActionFilterAtrribute
{
    // 返回 false 会中止本次调用并直接返回 null
    public override bool OnActionExecuting(IChannel channel, string serviceName, string methodName, object[] args)
    {
        return true; // 替换为你的权限校验
    }

    public override void OnActionExecuted(IChannel channel, string serviceName, string methodName, object[] args, object result)
    {
        // 权限校验在调用前完成,调用后无需额外处理
    }
}

[RPCService]
public class AdminService
{
    [Auth]
    public void DeleteUser(int id) { }
}

过滤器拿到的是连接视图 IChannel,只暴露 Id、IsActive、Attributes 与关闭入口,拿不到底层 socket;需要客户端地址时用 channel.GetAttribute(RpcChannelKeys.RemoteEndPoint) 读取,值为 System.Net.EndPoint,连接建立后写入、随连接关闭清空。

Q5: 支持异步方法吗?

A: 支持 async/await:

[RPCService]
public class UserService
{
    public async Task<User> GetUserAsync(int id)
    {
        return await _repository.FindAsync(id);
    }
}

Q6: 如何隐藏某些方法不对外暴露?

A: 使用 [NoRpc] 特性:

[RPCService]
public class UserService
{
    public string Login(string name) => "OK";  // 暴露
    
    [NoRpc]
    public void InternalMethod() { }  // 不暴露
}

Q7: 如何实现服务端推送?

A: 消费者先 RegistReceiveNotice() 登记,再用 OnNoticed 接收;服务端调用 Notice<T> 广播给所有已登记的连接:

using SAEA.Common.Serialization;

// 服务端推送(广播给所有已登记的消费者)
await provider.Notice("系统更新");

// 客户端订阅
var proxy = new RPCServiceProxy("rpc://127.0.0.1:39654");
proxy.OnNoticed += (serializeData) =>
{
    Console.WriteLine($"收到推送: {SAEASerialize.Deserialize<string>(serializeData)}");
};
proxy.RegistReceiveNotice();

OnNoticed 是领域通知事件,参数是推送载荷的序列化字节;需要按连接观察原始帧时改用处理器链。


核心类 🔧

类名 说明
ServiceProvider RPC 服务提供者主类,负责启动和管理服务
ServiceConsumer RPC 消费者主类,负责连接和调用
RPCServiceProxy 动态生成的客户端代理基类
RPCServiceAttribute 标记类为 RPC 服务
NoRpcAttribute 标记方法不对外开放
ActionFilterAtrribute AOP 过滤器抽象基类(类名拼写沿用现有 API)
RpcChannelKeys 通道属性键,RemoteEndPoint 对应过滤器可读的客户端地址
RPCMapping 服务映射缓存表
ConsumerMultiplexer 同一服务地址上的多连接复用管理器(links 决定连接数)
Notice<T> 服务端推送消息类型

使用示例 📝

完整服务端示例

using SAEA.RPC;
using SAEA.RPC.Model;
using SAEA.RPC.Provider;
using SAEA.Sockets.Pipeline;

// 定义用户实体
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreateTime { get; set; }
}

// 定义日志过滤器
public class LogAttribute : ActionFilterAtrribute
{
    public override bool OnActionExecuting(IChannel channel, string serviceName, string methodName, object[] args)
    {
        Console.WriteLine($"[{DateTime.Now}] 调用: {serviceName}.{methodName}");
        return true;
    }

    public override void OnActionExecuted(IChannel channel, string serviceName, string methodName, object[] args, object result)
    {
        Console.WriteLine($"[完成] 返回: {result}");
    }
}

// 定义 RPC 服务
[RPCService]
public class UserService
{
    private List<User> _users = new List<User>();

    [Log]
    public string Register(string name)
    {
        var user = new User 
        { 
            Id = _users.Count + 1, 
            Name = name,
            CreateTime = DateTime.Now 
        };
        _users.Add(user);
        return $"用户 {name} 注册成功,ID: {user.Id}";
    }

    public User GetUser(int id)
    {
        return _users.FirstOrDefault(u => u.Id == id);
    }

    public List<User> GetAllUsers()
    {
        return _users;
    }

    [NoRpc]
    public void InternalMethod()
    {
        // 此方法不会暴露为 RPC 服务
    }
}

// 启动服务(异常统一汇入 ExceptionCollector,可按需在此之前订阅 ExceptionCollector.OnErr)
var provider = new ServiceProvider(port: 39654);
provider.Start();

Console.WriteLine("RPC 服务已启动,端口: 39654");
Console.ReadKey();

完整客户端示例

using SAEA.RPC;

// 连接 RPC 服务(构造时即建立连接;异常统一汇入 ExceptionCollector)
var url = "rpc://127.0.0.1:39654";
var proxy = new RPCServiceProxy(url);

// 调用远程方法 - 注册用户
var result = proxy.UserService.Register("张三");
Console.WriteLine(result);  // 输出: 用户 张三 注册成功,ID: 1

// 调用远程方法 - 获取用户
var user = proxy.UserService.GetUser(1);
Console.WriteLine($"用户信息: {user.Name}, 注册时间: {user.CreateTime}");

// 调用远程方法 - 获取所有用户
var users = proxy.UserService.GetAllUsers();
Console.WriteLine($"总用户数: {users.Count}");

多服务地址负载均衡

RPCServiceProxy 一个实例绑定一个服务地址;同一地址内部的连接复用由 links 控制(ConsumerMultiplexer 轮转使用这些连接)。多地址需要由使用方自己选路:

using SAEA.RPC;

// 配置多个服务地址,调用方自行轮转
var urls = new[]
{
    "rpc://192.168.1.1:39654",
    "rpc://192.168.1.2:39654",
    "rpc://192.168.1.3:39654"
};
var index = 0;
string NextUrl() => urls[index++ % urls.Length];

var proxy = new RPCServiceProxy(NextUrl(), links: 4);

for (int i = 0; i < 100; i++)
{
    var result = proxy.UserService.Register($"User_{i}");
    Console.WriteLine(result);
}

服务端推送示例

using SAEA.Common.Serialization;
using SAEA.RPC;

// 服务端
var provider = new ServiceProvider(port: 39654);
provider.Start();

// 定时推送消息(广播给所有已登记的消费者)
var timer = new Timer(_ =>
{
    provider.Notice($"系统公告 当前时间: {DateTime.Now}");
}, null, 0, 5000);

// 客户端订阅
var proxy = new RPCServiceProxy("rpc://127.0.0.1:39654");
proxy.OnNoticed += (serializeData) =>
{
    Console.WriteLine($"收到推送: {SAEASerialize.Deserialize<string>(serializeData)}");
};
proxy.RegistReceiveNotice();

传输协议

RPC 消息格式:

| Type(1字节) | Total(4字节) | SeqNo(8字节) | SLen(4字节) | ServiceName | MLen(4字节) | MethodName | Data |
字段 大小 说明
Type 1 字节 消息类型
Total 4 字节 总长度
SeqNo 8 字节 序列号(请求响应匹配)
SLen 4 字节 服务名长度
ServiceName N 字节 服务名称
MLen 4 字节 方法名长度
MethodName N 字节 方法名称
Data N 字节 Protobuf 序列化参数

消息类型:

  • Ping/Pong - 心跳保活
  • Request/Response - 请求响应
  • Notice - 服务端推送
  • Error - 错误消息
  • Close - 关闭连接

依赖项

包名 版本 说明
SAEA.Sockets 26.9.21.1 Socket 通信框架
SAEA.Common 26.9.21.1 序列化工具

更多资源


许可证

Apache License 2.0

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SAEA.RPC:

Package Downloads
MicroServiceGateway.Client

MicroServiceGateway.Client

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.11.25.1 0 11/25/2026
26.11.24.1 54 11/23/2026
26.11.23.1 55 11/23/2026
26.11.16.1 117 11/16/2026
26.10.8.1 191 10/8/2026
26.4.23.1 306 4/23/2026
26.4.22.1 164 4/21/2026
7.26.4.21 143 4/20/2026
7.26.4.20 141 4/20/2026
7.26.2.2 182 2/2/2026
7.26.1.27 170 1/27/2026
7.26.1.25 167 1/25/2026
7.25.2.19 307 2/19/2025
7.25.2.18 268 2/18/2025
7.25.2.13 250 2/13/2025
7.25.2.11 267 2/11/2025
7.24.8.4 256 8/4/2024
7.23.9.24 301 9/24/2023
7.0.3.19 442 3/19/2023
7.0.0.3 727 2/27/2022
Loading failed

This is a SAEA. Sockets-based RPC component, portable and efficient, far superior performance to similar products, easy to use, a few lines of code can be implemented.
这是一个基于SAEA.Sockets的rpc组件,轻便高效,性能远超同类产品,简单易用,几行代码就能实现
https://github.com/yswenli/SAEA