设为首页 加入收藏

TOP

ScalaPB(5):用akka-stream实现reactive-gRPC(五)
2019-08-15 00:11:46 】 浏览:282
Tags:ScalaPB akka-stream 实现 reactive-gRPC
ger: Logger
= Logger.getLogger(classOf[gRPCAkkaStreamClient].getName) val channel = ManagedChannelBuilder .forAddress(host,port) .usePlaintext(true) .build() val stub = SumGrpcAkkaStream.stub(channel) def addPair(num1: Int, num2: Int): Source[String,NotUsed] = { logger.info(s"Requesting to add $num1, $num2") Source .single(NumPair(num1,num2)) .via(stub.sumPair) .map(r => s"the result: ${r.result}") } def genIncNumbers(len: Int): Source[Int,NotUsed] = { logger.info(s"Requesting to produce ${len} inc numbers") Source .single(Num(len)) .via(stub.genIncsFrom) .map(n => n.num) } def sumManyNumbers(nums: Seq[Int]): Source[String,NotUsed] = { logger.info(s"Requesting to sum up ${nums}") Source(nums.map(Num(_)).to[collection.immutable.Iterable]) .throttle(1, 500.millis, 1, ThrottleMode.shaping) .map { n => logger.info(s"Sending number: $n") n } .via(stub.sumStreamNums) .map(r => s"the result: ${r.result}") } def ContSum(nums: Seq[Int]): Source[String,NotUsed] = { logger.info(s"Requesting to sum up ${nums}") Source(nums.map(Num(_)).to[collection.immutable.Iterable]) .throttle(1, 500.millis, 1, ThrottleMode.shaping) .map { n => logger.info(s"Sending number: $n") n } .via(stub.keepAdding) .map(r => s"current sum = ${r.result}") } } object UnaryCallClient extends App { implicit val system = ActorSystem("UnaryClient") implicit val mat = ActorMaterializer.create(system) val client = new gRPCAkkaStreamClient("localhost", 50051) client.addPair(29,33).runForeach(println) scala.io.StdIn.readLine() mat.shutdown() system.terminate() } object ServerStreamingClient extends App { implicit val system = ActorSystem("ServerStreamingClient") implicit val mat = ActorMaterializer.create(system) val client = new gRPCAkkaStreamClient("localhost", 50051) client.genIncNumbers(5).runForeach(println) scala.io.StdIn.readLine() mat.shutdown() system.terminate() } object ClientStreamingClient extends App { implicit val system = ActorSystem("ClientStreamingClient") implicit val mat = ActorMaterializer.create(system) val client = new gRPCAkkaStreamClient("localhost", 50051) client.sumManyNumbers(Seq(12,4,8,19)).runForeach(println) scala.io.StdIn.readLine() mat.shutdown() system.terminate() } object BiDiStreamingClient extends App { implicit val system = ActorSystem("BiDiStreamingClient") implicit val mat = ActorMaterializer.create(system) val client = new gRPCAkkaStreamClient("localhost", 50051) client.ContSum(Seq(12,4,8,19)).runForeach(println) scala.io.StdIn.readLine() mat.shutdown() system.terminate() }

 

首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇重要的博客收集 下一篇ScalaPB(3): gRPC streaming

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目