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种类型:

 服务端客户端
1UnaryUnary
2StreamingStreaming
3UnaryStreaming
4StreamingUnary

 

  在上一篇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

 

   

 

转载于:https://www.cnblogs.com/cnblogs-wangzhipeng/p/9112751.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值