A tour of gRPC:07 - gRPC bidirectional straming 双向流

go bidirectional-streaming call
1. 修改 laptop_service.proto 添加 ReteLaptop 相关内容,运行 make clean 、make gen 进行 代码生成

2. rating_store.go 中编写和评分相关的内容

3. 在 laptop_server.go 中实现 添加 RatingStore 并修初始化函数(修改之前单元测试中为 ratingStore 添加 nil 值)实现服务端 RateLaptop 内容

4. 在 sample 包 generator 中增加一个生成 rating 的功能

5. 在 client/main.go 中实现 rateLaptop

6. 编写一个单元测试

运行一下单元测试

运行一下 server 和 client 进行测试

可以看到,client 发送完 3 条 create laptop 后,server 返回了评分的提示,此时 client 再次发送 y 后, server 接收到后会进行评分,并进行返回。也就是说,client 和 server 在一次连接中都发送了多次消息。
Java bidirectional-streaming call
1. 拷⻉ gRPC go bidirectional-straming 中的 laptop_service.proto 运行 mvn clean compile 生成最新的代码
2. 新建一个 Rating 实体类

3. 新建一个 RatingStore 接口

4. 新建一个 InMemoryRatingStore 实现 RatingStore

5.编写一个单元测试进行测试


单元测试通过,结果符合预期
6. 在 LaptopService 中实现 RatingStore 相关内容,并修改 LaptopServer 中使用 LaptopService 方法的地方。

7. 接下来实现客户端,在 Generator 中添加一个新随机生成分数的方法

8. 在客户端编写一个调用 rating 的方法

9. 运行 server 和 client 进行测试

同上,可以看到,client 发送完 3 条 create laptop 后,server 返回了评分的提示,此时 client 再次发送 y 后,server 接收到后会进行评分,并进行返回。也就是说,client 和 server 在一次连接中都发送了多次 消息。
跨语言的 bindirectional-streaming 测试
最后,来到了我们的熟悉的跨语言测试环节
我们先使用 Java Server 和 go client 进行测试

测试结果符合预期。
我们再反顾来使用 go Server 和 Java client 进行测试

效果符合预期



版权声明: 本文为 InfoQ 作者【BUG侦探】的原创文章。
原文链接:【http://xie.infoq.cn/article/00f2032fedd862fcd0285a880】。文章转载请联系作者。
评论