在 gRPC Java 应用程序中制作客户端流 synchronous/blocking

Make client-side streaming synchronous/blocking in gRPC Java application

我想阻止客户端流式传输。该协议的定义可能如下所示:

rpc RecordRoute(stream Point) returns (RouteSummary) {}

如文档中所述,对于某些类型的流式调用,只能使用异步存根:

a non-blocking/asynchronous stub that makes non-blocking calls to the server, where the response is returned asynchronously. You can make certain types of streaming call only using the asynchronous stub.

那我怎么才能打电话blocking/synchronous?可能吗?

阻塞存根只能用于客户端只发送一个请求的RPC。对于客户端流式调用,您只能使用异步存根。生成的阻塞存根代码不包含客户端流式传输或双向流式传输方法的 RPC 方法。

如果想避免异步请求造成的缓冲过多,可以使用CallStreamObServerAPI进行手动流控。对于某些外部同步,例如 CountDownLatch,异步 API 可以同步运行。查看 gRPC's manual flow control example 的工作原理。