RPC

作者: SongLiang | 来源:发表于2018-07-19 17:03 被阅读0次

    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){
    }
    

    相关文章

      网友评论

          本文标题:RPC

          本文链接:https://www.haomeiwen.com/subject/sicnpftx.html