RPC

2018-07-19  本文已影响0人  SongLiang

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

用途主要有两个

正宗(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.

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

rpc SayHello(HelloRequest) returns (HelloResponse){
}
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse){
}
rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse) {
}
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse){
}
上一篇 下一篇

猜你喜欢

热点阅读