欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-10 11:02:28
本节是继上一章节 Hello world 的进一步深入挖掘; 一、grpc服务接口类型 在godoc的网站上对grpc的端口类型进行了简单的介绍,总共有下面4种类型[1]: 上面是从官网摘抄过来的,简单的来讲客户端和服务发送数据有两种形式:Unary和Streaming。Unary (一元)一次只发 ......

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

一、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