• grpc 入门(二)-- 服务接口类型


    本节是继上一章节 Hello world 的进一步深入挖掘;

    一、grpc服务接口类型

      在godoc的网站上对grpc的端口类型进行了简单的介绍,总共有下面4种类型[1]:

    gRPC lets you define four kinds of service method:
    
        Unary RPCs where the client sends a single request to the server and gets a single response back, just like a normal function call.
    
      rpc SayHello(HelloRequest) returns (HelloResponse){
      }
    
        Server streaming RPCs where the client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages.
    
      rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse){
      }
    
        Client streaming RPCs where the client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them and return its response.
    
      rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse) {
      }
    
        Bidirectional streaming RPCs where both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved.
    
      rpc BidiHello(stream HelloRequest) returns (stream HelloResponse){
      }
    
      We’ll look at the different types of RPC in more detail in the RPC life cycle section below.
    

       上面是从官网摘抄过来的,简单的来讲客户端和服务发送数据有两种形式:Unary和Streaming。Unary (一元)一次只发送一个包; Streaming(流)一次可以发送多个包。两种方式组合一下就形成了4种类型:

      服务端 客户端
    1 Unary Unary
    2 Streaming Streaming
    3 Unary Streaming
    4 Streaming Unary

     

      在上一篇Hello world文章里面的示例就是第一种类型接口,它最终声明了一个需要开发者去实习具体业务逻辑的接口:

    // Server API for Greeter service
    
    type GreeterServer interface {
        // Sends a greeting
        SayHello(context.Context, *HelloRequest) (*HelloReply, error)
    }

       开发者需要根据业务需求来考虑使用不同的grpc服务接口类型 。

    参考网址

    [1] https://grpc.io/docs/guides/concepts.html#service-definition

     

       

  • 相关阅读:
    如何做一个按钮二个事件
    JavaScript中onmouseover时如何让鼠标指针变成一个小手状
    英语口语整理,灵活运用了,口语基本就没问题了
    javascript 传递 '' 时出错 换成 '&' ok
    一个按钮触发两个事件可以吗?
    Select中DISTINCT关键字的用法?
    使下拉框某项不可选的方法
    新打开窗口属性 a href
    调试和测试 Swing 代码
    打造专业外观九宫图
  • 原文地址:https://www.cnblogs.com/cnblogs-wangzhipeng/p/9112751.html
Copyright © 2020-2023  润新知