RPC

RPC(Remote procedure call)

In distributed computing, a remote procedure call(RPC) is when a computer program causes a procedure (subroutine) to execute in a different address space (commonly on another computer on a shared network), which is coded as if it were a normal (local) procedure call, without the programmer explicitly coding the details for the remote interaction.

gRPC (gRPC Remote Procedure Calls)

is an open source remote procedure call(RPC) system initially developed at Google. It uses HTTP/2 for transport, Protocal Buffers as the interface description language, and provides features such as authentication, bidirectional streaming and flow control, blocking or nonblocking bindings, and cancellation and timeouts. It generates cross-platform client and server bindings for many languages.

Interface description language

An interface description language or interface definition language(IDL), is a specification language used to describe a software component's application programming interface(API). IDLs describe an interface in a language-independent way, enabling communication between software components that do not share one language. For example, between those written in C++ and those written in Java.
IDLs are commonly used in remote procedure call software. In these cases the machines at either of the link may be using different operating systems and computer languages. IDLs offer a bridge between the two different systems.

protouf

用途主要有两个
  • 数据的存储(序列化和反序列化),类似于xml、json等。
  • 制作网络通信协议。

正宗(Google 自己内部用的)的protobuf支持三种语言:Java、C++和Python,很遗憾的是并不支持.Net 或者 Lua 等语言,但社区的力量是不容忽视的,由于 protobuf 确实比 Json、XML 有速度上的优势和使用的方便。

ProtoBuf的原理

Socket 通信中,客户端与服务器之间传递的是字节流。而在现实的应用中我们需要传递有一定含义的结构,使得通信的双方都能够识别该结构。实现对象(Class 和 Struct)Socket传输的关键就在于 Class 或 Struct 的序列和反序列化。

使用protobuf协议

定义 protobuf 协议必须创建一个以 .proto为后缀的文件,下面是一个 proto 脚本的简单例子:

gRPC 服务定义

Like many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. By default, gRPC uses protocol buffers as the Interface Definition Language (IDL) for describing both the service interface and the structure of the payload messages. It is possible to use other alternatives if desired.

  • gPRC 是基于一个想法,定义 service 能根据参数和返回类型能远程的调用其他函数。
  • gRPC 使用 protocol buffers 作为 Interface Definition Language(IDL) 接口定义语言,用来标书服务接口和载荷信息的结构。也可以用其他的东西来替代。

gRPC 允许你定义四种服务方法

  • Unary PRC
    客户端向服务器发送单个的 request,获取单个的 response 回来,就像一个普通的函数调用
rpc SayHello(HelloRequest) returns (HelloResponse){
}
  • Server streaming RPC
    客户端向服务器发送一条 request,拿到一个流来读取一连串的信息回来。客户端从返回流里读数据,一直到没有更多的数据为止。
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse){
}
  • Client streaming PRC
    客户端向服务器发送一连串的信息(使用提供的 stream),一旦客户端结束写信息的过程,客户端就等服务器来读取这些信息,并且读取 response。
rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse) {
}
  • Bidirectional streaming RPC
    这两条流独立操作,客户端和服务器可以以任何他们想要的顺序来进行读和写。比如,服务器可以在接收到客户端的所有信息之后,再去写他的resposes,或者可以选择性的读一条信息然后写一条信息,或者其他读写的组合。
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse){
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容