RPC通信相关

RPC

RPC, 远程过程调用(Remote Procedure Call,RPC)是一个计算机通信协议,该协议允许运行于一台计算机的程序程调用另一台计算机的上的程序。通俗讲,RPC通过把网络通讯抽象为远程的过程调用,调用远程的过程就像调用本地的子程序一样方便,从而屏蔽了通讯复杂性,使开发人员可以无需关注网络编程的细节,将更多的时间和精力放在业务逻辑本身的实现上,提高工作效率。

RPC本质上是一种 Inter-process communication(IPC)——进程间通信的形式。常见的进程间通信方式如管道、共享内存是同一台物理机上的两个进程间的通信,而RPC就是两个在不同物理机上的进程之间的通信。概括的说,RPC就是在一台机器上调用另一台机器上的方法,这种调用在远程机器上对代码的执行就像在本机上对代码的执行一样,只是迁移了一个执行环境而已。

RPC是一种C/S架构的服务模型,server端提供接口供client调用,client端向server端发送数据,server端接收client端的数据进行相关计算并将结果返回给client端。

执行一次RPC通常需要经历以下步骤(摘自 Wikipedia):

1.The client calls the client stub. The call is a local procedure call, with parameters pushed on to the stack in the normal way.

2.The client stub packs the parameters into a message and makes a system call to send the message. Packing the parameters is called marshalling.

3.The client's local operating system sends the message from the client machine to the server machine.

4.The local operating system on the server machine passes the incoming packets to the server stub.

5.The server stub unpacks the parameters from the message. Unpacking the parameters is called unmarshalling.

6.Finally, the server stub calls the server procedure. The reply traces the same steps in the reverse direction

为了实现上述RPC步骤,许多RPC工具被研发出来。这些RPC工具大多使用“接口描述语言” —— interface description language (IDL) 来提供跨平台跨语言的服务调用。现在生产中用的最多的IDL是Google开源的protobuf。

在日常开发中通常有两种形式来使用RPC,一种是团队内部完全实现上述RPC的6个步骤,自己序列化数据,然后自己利用socket或者http传输数据,最常见的就是游戏开发了。另一种就是利用现成的RPC工具,这些RPC工具实现了底层的数据通信,开发人员只需要利用IDL定义实现自己的服务即可而不用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值