diff --git a/skills/rpc/README.md b/skills/rpc/README.md new file mode 100644 index 0000000..0129e11 --- /dev/null +++ b/skills/rpc/README.md @@ -0,0 +1,37 @@ +# RPC + ++ Go语言内置RPC框架的使用(原始) ++ 基于接口封装优化好的RPC ++ Protobuf介绍与环境准备 ++ Protobuf编解码与语法介绍 + +详细说明: + +RPC使用场景以及概述 +Go语言内置RPC框架的基本使用 +RPC 服务端开发 +RPC 客户端开发 +测试客户端调用服务端 +基于接口来优化Go语言内置的RPC服务 +Go编码与跨语言RPC +Go语言内置RPC实现JSON ON TCP +Go语言内置RPC实现JSON ON HTTP +Protobuf概述 +Protobuf环境搭建与Go语言插件安装 +Protobuf 快速体验: 使用Protobuf生成Go语言代码 +Protobuf 语法详解: +定义消息类型 +Protobuf类型与Go语言类型对应关系 +枚举类型/数组类型/Map等基础类型的使用 +消息嵌套与限制 +Protobuf的包机制以及使用方式 +使用限制与更新规范 + +## RPC 概念 + +![alt text](image.png) + +RPC是远程过程调用的简称,是分布式系统中不同节点间流行的通信方式。在互联网时代,RPC已经和IPC一样成为一个不可或缺的基础构件。 + +## GO语言内置RPC + diff --git a/skills/rpc/hello_world/arch.drawio b/skills/rpc/hello_world/arch.drawio new file mode 100644 index 0000000..bec9724 --- /dev/null +++ b/skills/rpc/hello_world/arch.drawio @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/skills/rpc/hello_world/client/main.go b/skills/rpc/hello_world/client/main.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/skills/rpc/hello_world/client/main.go @@ -0,0 +1 @@ +package main diff --git a/skills/rpc/hello_world/server/main.go b/skills/rpc/hello_world/server/main.go new file mode 100644 index 0000000..2c07a09 --- /dev/null +++ b/skills/rpc/hello_world/server/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "net" + "net/rpc" +) + +// 实现业务功能 +// req := &HelloRequest{} +// resp := &HelloResponse{} +// err := &HelloServiceImpl{}.Hello(req, resp) +// net/rpc +// 1. 写好的对象, 注册给RPC Server +// 2. 再把RPC Server 启动起来 +type HelloServiceImpl struct { +} + +type HelloRequest struct { + MyName string `json:"my_name"` +} + +type HelloResponse struct { + Message string `json:"message"` +} + +// HTTP Handler +func (h *HelloServiceImpl) Hello(request *HelloRequest, response *HelloResponse) error { + // *response = "hello:" + request + return nil +} + +func main() { + // 1. 把业务 注册给RPC + // 2. RPC 业务RPC必须是满足条件的接口: (req any, req any) error + // Hello(request *HelloRequest, response *HelloResponse) error + if err := rpc.RegisterName("HelloService", &HelloServiceImpl{}); err != nil { + panic(err) + } + + // 启动RPC服务 + listener, err := net.Listen("tcp", ":1234") + if err != nil { + panic(err) + } + defer listener.Close() + for { + conn, err := listener.Accept() + if err != nil { + continue + } + // 用户的net层数据 转交给 rpc框架 + go rpc.ServeConn(conn) + } +} diff --git a/skills/rpc/image.png b/skills/rpc/image.png new file mode 100644 index 0000000..ababad8 Binary files /dev/null and b/skills/rpc/image.png differ