From bee336ed5e2c4c31a87d5798eb16cec22385442a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=9D=E5=B0=98?= <237809796@qq.com> Date: Sun, 22 Sep 2024 09:48:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=EF=BC=9AZHub=20=E7=9A=84rpc?= =?UTF-8?q?=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/tutorial-extras/_category_.json | 3 ++- docs/tutorial-extras/rpc.md | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/tutorial-extras/_category_.json b/docs/tutorial-extras/_category_.json index 022405a..9812c39 100644 --- a/docs/tutorial-extras/_category_.json +++ b/docs/tutorial-extras/_category_.json @@ -2,6 +2,7 @@ "label": "其他组件", "position": 3, "link": { - "type": "generated-index" + "type": "generated-index", + "description": "分布式系统常用组件" } } diff --git a/docs/tutorial-extras/rpc.md b/docs/tutorial-extras/rpc.md index 733de13..ffb1e34 100644 --- a/docs/tutorial-extras/rpc.md +++ b/docs/tutorial-extras/rpc.md @@ -4,4 +4,28 @@ sidebar_position: 1 # RPC远程调用 -# \ No newline at end of file +## 什么是 RPC +> RPC 是一种通过网络将远程过程调用(Remote Procedure Call,RPC)封装成消息,并传送到远程服务器上的过程。 +> ![zhub-fun.png](https://img.1216.top/docs/zhub/rpc-flow.png) + +## 使用场景 +> 在分布式环境下,通过 RPC 可以在两个应用之间进行消息传递,实现远程调用。 + +## rpc的订阅-调用基础示例 +### 被调用端 +```java + // 订阅 rpc-b 事件, 参数类型为 String + zhub.rpcSubscribe("rpc-b", IType.STRING, r -> { + String str = r.getValue(); + System.out.println("接收到 b 事件:" + str); + return r.render("接收到 b 事件:" + str); + }); +``` +### 调用端 +```java + // 调用 rpc-b 事件, 参数类型为 String,返回类型为 String + RpcResult rpcResult = zhub.rpc("rpc-b", "hello rpc", IType.STRING); + String result = rpcResult.getResult(); + System.out.println("rpc result:" + result); +``` +