当前位置:网站首页>RPC(远程过程调用协议)
RPC(远程过程调用协议)
2022-06-21 12:00:00 【attempt_to_do】
RPC(Remote Procedure Call Protocol)——远程过程调用协议,是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。它假定某些传输协议的存在,如TCP或UDP,以便为通信程序之间携带信息数据。通过它可以使函数调用模式网络化。在OSI网络通信模型中,RPC跨越了传输层和应用层。
RPC工作原理

运行时,一次客户机对服务器的RPC调用,其内部操作大致有如下十步:
1.调用客户端句柄;执行传送参数
2.调用本地系统内核发送网络消息
3.消息传送到远程主机
4.服务器句柄得到消息并取得参数
5.执行远程过程
6.执行的过程将结果返回服务器句柄
7.服务器句柄返回结果,调用远程系统内核
8.消息传回本地主机
9.客户句柄由内核接收消息
10.客户接收句柄返回的数据
Go RPC
Go RPC的函数只有符合下面的条件才能被远程访问,不然会被忽略,详细的要求如下:
- 函数必须是导出的(首字母大写)
- 必须有两个导出类型的参数,
- 第一个参数是接收的参数,第二个参数是返回给客户端的参数,第二个参数必须是指针类型的
- 函数还要有一个返回值error
举个例子,正确的RPC函数格式如下:
func (t *T) MethodName(argType T1, replyType *T2) error
T、T1和T2类型必须能被encoding/gob包编解码。
任何的RPC都需要通过网络来传递数据,Go RPC可以利用HTTP和TCP来传递数据,利用HTTP的好处是可以直接复用net/http里面的一些函数。详细的例子请看下面的实现
HTTP RPC
服务器代码
package main
import (
"errors"
"fmt"
"net/http"
"net/rpc"
)
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
func main() {
arith := new(Arith)
rpc.Register(arith)
rpc.HandleHTTP()
err := http.ListenAndServe(":1234", nil)
if err != nil {
fmt.Println(err.Error())
}
}
通过上面的例子可以看到,我们注册了一个Arith的RPC服务,然后通过rpc.HandleHTTP函数把该服务注册到了HTTP协议上,然后我们就可以利用http的方式来传递数据了。
客户端代码
package main
import (
"fmt"
"log"
"net/rpc"
"os"
)
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], "server")
os.Exit(1)
}
serverAddress := os.Args[1]
client, err := rpc.DialHTTP("tcp", serverAddress+":1234")
if err != nil {
log.Fatal("dialing:", err)
}
// Synchronous call
args := Args{
17, 8}
var reply int
err = client.Call("Arith.Multiply", args, &reply)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
var quot Quotient
err = client.Call("Arith.Divide", args, ")
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d/%d=%d remainder %d\n", args.A, args.B, quot.Quo, quot.Rem)
}
我们把上面的服务端和客户端的代码分别编译,然后先把服务端开启,然后开启客户端,输入代码,就会输出如下信息:
$ ./http_c localhost
Arith: 17*8=136
Arith: 17/8=2 remainder 1
通过上面的调用可以看到参数和返回值是我们定义的struct类型,在服务端我们把它们当做调用函数的参数的类型,在客户端作为client.Call的第2,3两个参数的类型。客户端最重要的就是这个Call函数,它有3个参数,第1个要调用的函数的名字,第2个是要传递的参数,第3个要返回的参数(注意是指针类型),通过上面的代码例子我们可以发现,使用Go的RPC实现相当的简单,方便。
边栏推荐
- 2022年138套数学分析高等代数考研真题参考解答勘误
- [Harbin Institute of technology] information sharing for the first and second examinations of postgraduate entrance examination
- Summary of UART problems in stm32cubemx
- Citus 11 for Postgres is completely open source and can be queried from any node (citus official blog)
- 站在数字化风口,工装企业如何“飞起来”
- TypeScript 变量声明 —— 类型断言(Type Assertion)
- 广东发产品检测券,消费者也有份
- SSD的anchor_box计算
- 一文搞懂 Flink OperatorChain 对象重用
- Brief discussion on four full bonding processes of oca\uv-oca loca\sloca
猜你喜欢

蜜雪冰城(已黑化)

One's deceased father grind politics English average cent furnace! What is your current level?

STM32开发之 VS Code + GDB下载调试
![[Harbin Institute of technology] information sharing for the first and second examinations of postgraduate entrance examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Harbin Institute of technology] information sharing for the first and second examinations of postgraduate entrance examination

【yolov5s目标检测】opencv加载onnx模型在GPU上进行推理

1108. IP 地址无效化

2-zabbix automatically add hosts using autodiscover

20N10-ASEMI中低压MOS管20N10

2022 safety officer-b certificate retraining question bank and simulated examination

南京大学 静态软件分析(static program analyzes)-- Intermediate Representation 学习笔记
随机推荐
i.MX - RT1052 SPI和 I2C接口
看懂UML类图和时序图
Typescript variable declaration - type assertion
matrial3d参数分析
How does Huawei build a project centered project management system from 0 to 1?
矩形覆盖面积
Adapter power supply automatic test equipment | introduction to charger ATE test system nsat-8000
SSD【目标检测篇】
WPF 使用 MAUI 的自绘制逻辑
External attention tensorflow (under update)
100w的数据表比1000w的数据表查询更快吗?
Est le logiciel d'oscilloscope allemand, le logiciel d'ordinateur hôte d'oscilloscope keysight NS scope
动手学数据分析 数据重构
巨头局终战:即时零售
Chapter VIII web project testing
Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!
Customization of power aging test system | overview of charging pile automatic test system nsat-8000
findpanel的相关代码
Broken knowledge
Vs code + GCC environment compilation for STM32 development