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
grpc 入门(二)-- 服务接口类型的更多相关文章
- Go GRPC 入门(二)
前言 最近较忙,其实准备一篇搞定的 中途有事,只能隔了一天再写 正文 pb.go 需要注意的是,在本个 demo 中,客户端与服务端都是 Golang,所以在客户端与服务端都公用一个 pb.go 模板 ...
- gRPC (1):入门及服务端创建和调用原理
1. RPC 入门 1.1 RPC 框架原理 RPC 框架的目标就是让远程服务调用更加简单.透明,RPC 框架负责屏蔽底层的传输方式(TCP 或者 UDP).序列化方式(XML/Json/ 二进制)和 ...
- 微服务架构攀登之路(三)之gRPC入门
一.gRPC入门 1. gRPC 简介 gRPC 由 google 开发,是一款语言中立.平台中立.开源的远程过程调用系统 gRPC 客户端和服务端可以在多种环境中运行和交互,例如用 java 写一个 ...
- 【原创】NIO框架入门(二):服务端基于MINA2的UDP双向通信Demo演示
前言 NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能.这其中最流行的无非就是MINA和Netty了,MINA目前的主要版本是MINA2.而Netty的主要版本是Netty3和Netty ...
- 微服务(入门二):netcore通过consul注册服务
基础准备 1.创建asp.net core Web 应用程序选择Api 2.appsettings.json 配置consul服务器地址,以及本机ip和端口号信息 { "Logging&qu ...
- .net core grpc consul 实现服务注册 服务发现 负载均衡(二)
在上一篇 .net core grpc 实现通信(一) 中,我们实现的grpc通信在.net core中的可行性,但要在微服务中真正使用,还缺少 服务注册,服务发现及负载均衡等,本篇我们将在 .net ...
- go微服务系列(四) - gRPC入门
1. 前言 2. gRPC与Protobuf简介 3. 安装 4. 中间文件演示 4.1 编写中间文件 4.2 运行protoc命令编译成go中间文件 5. 创建gRPC服务端 5.1 新建Produ ...
- 微服务入门二:SpringCloud(版本Hoxton SR6)
一.什么是SpringCloud 1.官方定义 1)官方定义:springcloud为开发人员提供了在分布式系统中快速构建一些通用模式的工具(例如配置管理.服务发现.断路器.智能路由.微代理.控制总线 ...
- C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解
系列目录 [已更新最新开发文章,点击查看详细] 在我的博客<C#二次开发BIMFACE系列61 File Management文件管理服务接口二次开发及实战详解>最后列出了 Fil ...
随机推荐
- React学习笔记(七)条件渲染
React学习笔记(七) 六.条件渲染 使用if或条件运算符来创建表示当前状态的元素. 可以使用变量来存储元素.比如: let button = null; if (isLoggedIn) { but ...
- 乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters
乘风破浪:LeetCode真题_003_Longest Substring Without Repeating Characters 一.前言 在算法之中出现最多的就是字符串方面的问题了,关于字符串的 ...
- CentOS使用PXE网络启动自动安装操作系统
PXE工作原理简介 PXE (Preboot Excution Environment) 预启动执行环境 工作环境介绍: 要实现PXE自动网络安装CentOS至少需要两台主机 1)PXE服务器PXE ...
- Azure之旅
http://www.microsoft.com/web/downloads/platform.aspx http://www.microsoft.com/en-us/download/details ...
- August 08th 2017 Week 32nd Tuesday
The very essence of romance is uncertainty. 浪漫的精髓就在于它充满种种可能. Romance is the glamour that can turn th ...
- 为什么JS中0.1+0.2 != 0.3
为什么JS中0.1+0.2 != 0.3 在我曾经的一篇< javascript入门教程 (2) >中,讲到JS中数字运算时,我们提到过一个叫做 数字运算中的精度缺失的问题,当时我们只是简 ...
- PetaPoco轻量级ORM框架 - 对Database类的进行扩展,可以返回Table格式数据
一.有时我们需要将常用的功能添加到PetaPoco中的Database类中 实现方式有2种,以下以查询字段为例 1.通过扩展方式实现,此方式不改变被调用(Database)类名(只能增加方法) pub ...
- 5、Python,enumerate用法
1.enumerate()是python的内置函数 2.对于一个可迭代(iterable)/可遍历的对象(如列表,字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值 3.多用 ...
- socket端口复用问题一二
实际上,默认的情况下,如果一个网络应用程序的一个套接字 绑定了一个端口( 占用了 8000 ),这时候,别的套接字就无法使用这个端口( 8000 ), 验证例子如下: #include <std ...
- Java虚拟机13:Java类加载机制
前言 我们知道我们写的程序经过编译后成为了.class文件,.class文件中描述了类的各种信息,最终都需要加载到虚拟机之后才能运行和使用.而虚拟机如何加载这些.class文件?.class文件的信息 ...