补充rpc
This commit is contained in:
parent
00687c94d1
commit
8745b86e2f
37
skills/rpc/README.md
Normal file
37
skills/rpc/README.md
Normal file
@ -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 概念
|
||||
|
||||

|
||||
|
||||
RPC是远程过程调用的简称,是分布式系统中不同节点间流行的通信方式。在互联网时代,RPC已经和IPC一样成为一个不可或缺的基础构件。
|
||||
|
||||
## GO语言内置RPC
|
||||
|
35
skills/rpc/hello_world/arch.drawio
Normal file
35
skills/rpc/hello_world/arch.drawio
Normal file
@ -0,0 +1,35 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="NVg1F5LXBrNApti-Oyyw" name="第 1 页">
|
||||
<mxGraphModel dx="893" dy="424" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="6" style="edgeStyle=none;html=1;exitX=1;exitY=0.25;exitDx=0;exitDy=0;entryX=0;entryY=0.25;entryDx=0;entryDy=0;" edge="1" parent="1" source="2" target="3">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="7" value="bob" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="6">
|
||||
<mxGeometry x="-0.0033" y="-2" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="2" value="client" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="110" y="170" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="8" style="edgeStyle=none;html=1;exitX=0;exitY=0.75;exitDx=0;exitDy=0;entryX=1;entryY=0.75;entryDx=0;entryDy=0;" edge="1" parent="1" source="3" target="2">
|
||||
<mxGeometry relative="1" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" value="hello, bob" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="8">
|
||||
<mxGeometry x="0.2633" y="1" relative="1" as="geometry">
|
||||
<mxPoint as="offset"/>
|
||||
</mxGeometry>
|
||||
</mxCell>
|
||||
<mxCell id="3" value="server" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="430" y="130" width="170" height="150" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="5" value="<h1 style="margin-top: 0px;">Hello业务</h1><div>client(req):&nbsp; &nbsp; bob</div><div>server(resp):&nbsp; hello, bob</div><p><br></p>" style="text;html=1;whiteSpace=wrap;overflow=hidden;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="250" y="20" width="180" height="100" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
1
skills/rpc/hello_world/client/main.go
Normal file
1
skills/rpc/hello_world/client/main.go
Normal file
@ -0,0 +1 @@
|
||||
package main
|
54
skills/rpc/hello_world/server/main.go
Normal file
54
skills/rpc/hello_world/server/main.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
BIN
skills/rpc/image.png
Normal file
BIN
skills/rpc/image.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 116 KiB |
Loading…
x
Reference in New Issue
Block a user