接着上期讨论的gRPC unary服务我们跟着介绍gRPC streaming,包括: Server-Streaming, Client-Streaming及Bidirectional-Streaming。我们首先在.proto文件里用IDL描述Server-Streaming服务:

/*
* responding stream of increment results
*/
service SumOneToMany {
rpc AddOneToMany(SumRequest) returns (stream SumResponse) {}
} message SumRequest {
int32 toAdd = ;
} message SumResponse {
int32 currentResult = ;
}

SumOneToMany服务中AddOneToMany函数接受一个SumRequest然后返回stream SumResponse,就这么简单。经过编译后产生了SumOneToManyGrpc.scala文件,在这个文件里提供了有关RPC操作的api。我们看看protoc把IDL描述的服务函数变成了什么样的scala函数:

def addOneToMany(request: SumRequest, responseObserver: StreamObserver[SumResponse]): Unit 

调用scala函数addOneToMany需要传入参数SumRequest和StreamObserver[SumResponse],也就是说用户需要准备这两个入参数。在调用addOneToMany函数时用户事先构建这个StreamObserver传给server,由server把结果通过这个结构传回用户。gRPC是通过StreamObserver类型实例来实现数据streaming的。这个类型的构建例子如下:

    val responseObserver = new StreamObserver[SumResponse] {
def onError(t: Throwable): Unit = println(s"ON_ERROR: $t")
def onCompleted(): Unit = println("ON_COMPLETED")
def onNext(value: SumResponse): Unit =
println(s"ON_NEXT: Current sum: ${value.currentResult}")
}

server端通过onNext把结果不断传回给client端,因为这个responseObserver是在client端构建的。下面是SumManyToMany的实现:

 class SumOne2ManyService extends SumOneToManyGrpc.SumOneToMany {
override def addOneToMany(request: SumRequest, responseObserver: StreamObserver[SumResponse]): Unit = {
val currentSum: AtomicInt = Atomic()
( to request.toAdd).map { _ =>
responseObserver.onNext(SumResponse().withCurrentResult(currentSum.incrementAndGet()))
}
Thread.sleep() //delay and then finish
responseObserver.onCompleted()
}
}

这个addOneToMany服务函数把 1-request.toAdd之间的数字逐个通过responseObserver返还调用方。 在客户端如下调用服务:

    // get asyn stub
val client: SumOneToManyGrpc.SumOneToManyStub = SumOneToManyGrpc.stub(channel)
// prepare stream observer
val streamObserver = new StreamObserver[SumResponse] {
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}")
override def onCompleted(): Unit = println("Done incrementing !!!")
override def onNext(value: SumResponse): Unit = println(s"current value: ${value.currentResult}")
}
// call service with stream observer
client.addOneToMany(SumRequest().withToAdd(),streamObserver)

Client-Streaming服务的IDL如下:

/*
* responding a result from a request of stream of numbers
*/
service SumManyToOne {
rpc AddManyToOne(stream SumRequest ) returns (SumResponse) {}
}

传入stream SumRequest, 返回SumResponse。scalaPB自动产生scala代码中的addManyToOne函数款式如下:

def addManyToOne(responseObserver: StreamObserver[SumResponse]): StreamObserver[SumRequest]

调用方提供StreamObserver[SumResponse]用作返回结果,函数返回客方需要的StreamObserver[SumRequest]用以传递request流。注意:虽然在.proto文件中AddManyToOne的返回结果是单个SumResponse,但产生的scala函数则提供了一个StreamObserver[SumResponse]类型,所以需要谨记只能调用一次onNext。下面是这个服务的实现代码:

  class Many2OneService extends SumManyToOneGrpc.SumManyToOne {
val currentSum: AtomicInt = Atomic()
override def addManyToOne(responseObserver: StreamObserver[SumResponse]): StreamObserver[SumRequest] =
new StreamObserver[SumRequest] {
val currentSum: AtomicInt = Atomic()
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}")
override def onCompleted(): Unit = println("Done summing!")
override def onNext(value: SumRequest): Unit = {
//only allow one response
if (value.toAdd > )
currentSum.add(value.toAdd)
else
responseObserver.onNext(SumResponse(currentSum.addAndGet(value.toAdd)))
}
}
}

客户方调用示范如下:

    //pass to server for result
val respStreamObserver = new StreamObserver[SumResponse] {
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}")
override def onCompleted(): Unit = println("Done responding!")
override def onNext(value: SumResponse): Unit =
println(s"Result: ${value.currentResult}")
}
//get async stub
val client = SumManyToOneGrpc.stub(channel) //get request stream observer from server
val reqStreamObserver = client.addManyToOne(respStreamObserver) List(,,,,).map { n =>
reqStreamObserver.onNext(SumRequest(n))
}

Bidirectional-Streaming的IDL描述如下:

/*
* Sums up numbers received from the client and returns the current result after each received request.
*/
service SumInter {
rpc AddInter(stream SumRequest) returns (stream SumResponse) {}
}

这个service SumInter 描述了stream SumRequest 及 stream SumResponse运算模式。产生的对应scala函数如下:

def addInter(responseObserver: StreamObserver[SumResponse]): StreamObserver[SumRequest]

这个函数的款式与Client-Streaming服务函数是一样的。但是,我们可以通过responseObserver传递多个SumResponse。这个服务的实现代码是这样的:

  class Many2ManyService extends SumInterGrpc.SumInter {
override def addInter(responseObserver: StreamObserver[SumResponse]): StreamObserver[SumRequest] =
new StreamObserver[SumRequest] {
val currentSum: AtomicInt = Atomic()
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}")
override def onCompleted(): Unit = println("Done requesting!")
override def onNext(value: SumRequest): Unit = {
responseObserver.onNext(SumResponse(currentSum.addAndGet(value.toAdd)))
}
}
}

我们可以多次调用responseObserver.onNext。客户端源代码如下:

    //create stream observer for result stream
val responseObserver = new StreamObserver[SumResponse] {
def onError(t: Throwable): Unit = println(s"ON_ERROR: $t")
def onCompleted(): Unit = println("ON_COMPLETED")
def onNext(value: SumResponse): Unit =
println(s"ON_NEXT: Current sum: ${value.currentResult}")
}
//get request container
val requestObserver = client.addInter(responseObserver) scheduler.scheduleWithFixedDelay(.seconds, .seconds) {
val toBeAdded = Random.nextInt()
println(s"Adding number: $toBeAdded")
requestObserver.onNext(SumRequest(toBeAdded))
}

下面是本次示范的源代码:

project/scalapb.sbt

addSbtPlugin("com.thesamet" % "sbt-protoc" % "0.99.18")
libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.7.1"

build.sbt

import scalapb.compiler.Version.scalapbVersion
import scalapb.compiler.Version.grpcJavaVersion name := "learn-gRPC" version := "0.1" scalaVersion := "2.12.6" libraryDependencies ++= Seq(
"com.thesamet.scalapb" %% "scalapb-runtime" % scalapbVersion % "protobuf",
"io.grpc" % "grpc-netty" % grpcJavaVersion,
"com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapbVersion,
"io.monix" %% "monix" % "2.3.0"
) PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
)

src/main/protobuf/sum.proto

syntax = "proto3";

package learn.grpc.services;

/*
* responding stream of increment results
*/
service SumOneToMany {
rpc AddOneToMany(SumRequest) returns (stream SumResponse) {}
} /*
* responding a result from a request of stream of numbers
*/
service SumManyToOne {
rpc AddManyToOne(stream SumRequest ) returns (SumResponse) {}
} /*
* Sums up numbers received from the client and returns the current result after each received request.
*/
service SumInter {
rpc AddInter(stream SumRequest) returns (stream SumResponse) {}
} message SumRequest {
int32 toAdd = ;
} message SumResponse {
int32 currentResult = ;
}

gRPCServer.scala

package learn.grpc.server
import io.grpc.{ServerBuilder,ServerServiceDefinition} trait gRPCServer {
def runServer(service: ServerServiceDefinition): Unit = {
val server = ServerBuilder
.forPort()
.addService(service)
.build
.start // make sure our server is stopped when jvm is shut down
Runtime.getRuntime.addShutdownHook(new Thread() {
override def run(): Unit = server.shutdown()
}) server.awaitTermination()
} }

OneToManyServer.scala

package learn.grpc.sum.one2many.server
import io.grpc.stub.StreamObserver
import learn.grpc.services.sum._
import monix.execution.atomic.{Atomic,AtomicInt}
import learn.grpc.server.gRPCServer object One2ManyServer extends gRPCServer { class SumOne2ManyService extends SumOneToManyGrpc.SumOneToMany {
override def addOneToMany(request: SumRequest, responseObserver: StreamObserver[SumResponse]): Unit = {
val currentSum: AtomicInt = Atomic()
( to request.toAdd).map { _ =>
responseObserver.onNext(SumResponse().withCurrentResult(currentSum.incrementAndGet()))
}
Thread.sleep() //delay and then finish
responseObserver.onCompleted()
}
} def main(args: Array[String]) = {
val svc = SumOneToManyGrpc.bindService(new SumOne2ManyService, scala.concurrent.ExecutionContext.global)
runServer(svc)
} }

OneToManyClient.scala

package learn.grpc.sum.one2many.client
import io.grpc.stub.StreamObserver
import learn.grpc.services.sum._ object One2ManyClient {
def main(args: Array[String]): Unit = { //build connection channel
val channel = io.grpc.ManagedChannelBuilder
.forAddress("LocalHost",)
.usePlaintext(true)
.build() // get asyn stub
val client: SumOneToManyGrpc.SumOneToManyStub = SumOneToManyGrpc.stub(channel)
// prepare stream observer
val streamObserver = new StreamObserver[SumResponse] {
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}")
override def onCompleted(): Unit = println("Done incrementing !!!")
override def onNext(value: SumResponse): Unit = println(s"current value: ${value.currentResult}")
}
// call service with stream observer
client.addOneToMany(SumRequest().withToAdd(),streamObserver) // wait for async execution
scala.io.StdIn.readLine()
}
}

ManyToOneServer.scala

package learn.grpc.sum.many2one.server
import io.grpc.stub.StreamObserver
import learn.grpc.services.sum._
import learn.grpc.server.gRPCServer
import monix.execution.atomic.{Atomic,AtomicInt} object Many2OneServer extends gRPCServer {
class Many2OneService extends SumManyToOneGrpc.SumManyToOne {
val currentSum: AtomicInt = Atomic()
override def addManyToOne(responseObserver: StreamObserver[SumResponse]): StreamObserver[SumRequest] =
new StreamObserver[SumRequest] {
val currentSum: AtomicInt = Atomic()
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}")
override def onCompleted(): Unit = println("Done summing!")
override def onNext(value: SumRequest): Unit = {
//only allow one response
if (value.toAdd > )
currentSum.add(value.toAdd)
else
responseObserver.onNext(SumResponse(currentSum.addAndGet(value.toAdd)))
}
}
} def main(args: Array[String]): Unit = {
val svc = SumManyToOneGrpc.bindService(new Many2OneService,scala.concurrent.ExecutionContext.global)
runServer(svc)
}
}

ManyToOneClient.scala

package learn.grpc.sum.many2one.client
import io.grpc.stub.StreamObserver
import learn.grpc.services.sum._ object Many2OneClient {
def main(args: Array[String]): Unit = {
//build channel
val channel = io.grpc.ManagedChannelBuilder
.forAddress("LocalHost",)
.usePlaintext(true)
.build()
//pass to server for result
val respStreamObserver = new StreamObserver[SumResponse] {
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}")
override def onCompleted(): Unit = println("Done responding!")
override def onNext(value: SumResponse): Unit =
println(s"Result: ${value.currentResult}")
}
//get async stub
val client = SumManyToOneGrpc.stub(channel) //get request stream observer from server
val reqStreamObserver = client.addManyToOne(respStreamObserver) List(,,,,).map { n =>
reqStreamObserver.onNext(SumRequest(n))
}
scala.io.StdIn.readLine()
}
}

ManyToManyServer.scala

package learn.grpc.sum.many2many.server
import io.grpc.stub.StreamObserver
import learn.grpc.services.sum._
import learn.grpc.server.gRPCServer
import monix.execution.atomic.{Atomic,AtomicInt}
object Many2ManyServer extends gRPCServer {
class Many2ManyService extends SumInterGrpc.SumInter {
override def addInter(responseObserver: StreamObserver[SumResponse]): StreamObserver[SumRequest] =
new StreamObserver[SumRequest] {
val currentSum: AtomicInt = Atomic() override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}") override def onCompleted(): Unit = println("Done requesting!") override def onNext(value: SumRequest): Unit = {
responseObserver.onNext(SumResponse(currentSum.addAndGet(value.toAdd)))
}
}
}
def main(args: Array[String]): Unit = {
val svc = SumInterGrpc.bindService(new Many2ManyService, scala.concurrent.ExecutionContext.global)
runServer(svc)
} }

ManyToManyClient.scala

package learn.grpc.sum.many2many.client
import monix.execution.Scheduler.{global => scheduler}
import learn.grpc.services.sum._ import scala.concurrent.duration._
import scala.util.Random
import io.grpc._
import io.grpc.stub.StreamObserver object Many2ManyClient {
def main(args: Array[String]): Unit = {
val channel = ManagedChannelBuilder.forAddress("localhost", ).usePlaintext(true).build
val client = SumInterGrpc.stub(channel)
//create stream observer for result stream
val responseObserver = new StreamObserver[SumResponse] {
def onError(t: Throwable): Unit = println(s"ON_ERROR: $t")
def onCompleted(): Unit = println("ON_COMPLETED")
def onNext(value: SumResponse): Unit =
println(s"ON_NEXT: Current sum: ${value.currentResult}")
}
//get request container
val requestObserver = client.addInter(responseObserver) scheduler.scheduleWithFixedDelay(.seconds, .seconds) {
val toBeAdded = Random.nextInt()
println(s"Adding number: $toBeAdded")
requestObserver.onNext(SumRequest(toBeAdded))
} scala.io.StdIn.readLine()
} }

ScalaPB(3): gRPC streaming的更多相关文章

  1. ScalaPB(2): 在scala中用gRPC实现微服务

    gRPC是google开源提供的一个RPC软件框架,它的特点是极大简化了传统RPC的开发流程和代码量,使用户可以免除许多陷阱并聚焦于实际应用逻辑中.作为一种google的最新RPC解决方案,gRPC具 ...

  2. ScalaPB(0): 找寻合适的内部系统微服务集成工具

    前一段时间我们探讨了SDP的一个基于集群的综合数据平台解决方案,由多种数据库组成,包括:JDBC, Cassandra 及MongoDB.其中Cassandra和MongoDB属于分布式数据库,可以在 ...

  3. ScalaPB(5):用akka-stream实现reactive-gRPC

      在前面几篇讨论里我们介绍了scala-gRPC的基本功能和使用方法,我们基本确定了选择gRPC作为一种有效的内部系统集成工具,主要因为下面gRPC支持的几种服务模式: .Unary-Call:独立 ...

  4. ScalaPB(4): 通用跨系统protobuf数据,sbt设置

    我们知道,在集群环境节点之间进行交换的数据必须经过序列化/反序列化处理过程,而在这方面protobuf是一个比较高效.易用的模式.用户首先在.proto文件中用IDL来定义系统中各种需要进行交换的数据 ...

  5. SDP(12): MongoDB-Engine - Streaming

    在akka-alpakka工具包里也提供了对MongoDB的stream-connector,能针对MongoDB数据库进行streaming操作.这个MongoDB-connector里包含了Mon ...

  6. ScalaPB(1): using protobuf in akka

    任何类型的实例作为消息在两端独立系统的机器之间进行传递时必须经过序列化/反序列化serialize/deserialize处理过程.假设以下场景:在一个网络里有两台连接的服务器,它们分别部署了独立的a ...

  7. gRPC(2):客户端创建和调用原理

    1. gRPC 客户端创建流程 1.1 背景 gRPC 是在 HTTP/2 之上实现的 RPC 框架,HTTP/2 是第 7 层(应用层)协议,它运行在 TCP(第 4 层 - 传输层)协议之上,相比 ...

  8. Spark2.2(三十三):Spark Streaming和Spark Structured Streaming更新broadcast总结(一)

    背景: 需要在spark2.2.0更新broadcast中的内容,网上也搜索了不少文章,都在讲解spark streaming中如何更新,但没有spark structured streaming更新 ...

  9. gRPC(2):四种基本通信模式

    在 gRPC(1):入门及简单使用(go) 中,我们实现了一个简单的 gRPC 应用程序,其中双方通信是简单的请求-响应模式,没发出一个请求都会得到一个响应,然而,借助 gRPC 可以实现不同的通信模 ...

随机推荐

  1. React Native之AppRegistry模块

    我们在写react native的js的时候,在最后总会加上一段代码: AppRegistry.registerComponent('ReactDemo', () => ReactDemo); ...

  2. (NO.00001)iOS游戏SpeedBoy Lite成形记(二)

    打开SpriteBuilder生成的Xcode项目文件,首先在MainScene添加play回调函数: -(void)play{ CCScene *gameScene = [CCBReader loa ...

  3. 支持老版本IE的3种解决方案

    原文链接: 3 Solutions for Supporting Internet Explorer 翻译人员: 铁锚 翻译时间: 2014年2月14日 在刚发布的那几年,Internet Explo ...

  4. 循环链表设计与API实现

    基本概念 循环链表的定义:将单链表中最后一个数据元素的next指针指向第一个元素 循环链表拥有单链表的所有操作 创建链表 销毁链表 获取链表长度 清空链表 获取第pos个元素操作 插入元素到位置pos ...

  5. SSRS2008中控件ID冲突问题

    [BC30179] class"textbox21_TextBoxExprHost"和 class"Textbox21_TextBoxExprHost"在 cl ...

  6. TrueType字体的后缀名解释

    OpenType标准定义了OpenType文件名称的后缀名.包含TureType字体的OpenType文件后缀名为.ttf,包含PostScript字体的文件后缀名为.OTF.如果是包含一系列True ...

  7. VCC、 VDD、VEE、VSS 电压理解

    VCC. VDD.VEE.VSS 版本一: 简单说来,可以这样理解: 一.解释 VCC:C=circuit 表示电路的意思, 即接入电路的电压: VDD:D=device 表示器件的意思, 即器件内部 ...

  8. Oracle统一访问代理层方案

    目标 提供一个oracle数据库统一访问代理层,统一管理所有oracle数据库用户名的连接池,让多个应用系统相同的数据库用户公用连接池以节省oracle服务器的总连接数,并且提供统一管理oracle能 ...

  9. Android BLE与终端通信(四)——实现服务器与客户端即时通讯功能

    Android BLE与终端通信(四)--实现服务器与客户端即时通讯功能 前面几篇一直在讲一些基础,其实说实话,蓝牙主要为多的还是一些概念性的东西,当你把概念都熟悉了之后,你会很简单的就可以实现一些逻 ...

  10. linux下shell中执行命令的顺序问题

    在shell中有很多种命令的同义词,到底先执行的是哪一个命令呢,我们可以借助type -a命令来检查. 为了测试我们就对type本身做一条alias: alias type=type 然后我们通过ty ...