【发布时间】:2020-12-28 19:36:59
【问题描述】:
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 8888).usePlaintext().build();
Grpc.Stub stub = CLIGrpc.newStub(managedChannel);
什么时候必须创建存根?什么时候每个方法都被调用或只调用一次?
【问题讨论】:
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 8888).usePlaintext().build();
Grpc.Stub stub = CLIGrpc.newStub(managedChannel);
什么时候必须创建存根?什么时候每个方法都被调用或只调用一次?
【问题讨论】:
只有一次。创建存根后,您将继续对其调用方法。
【讨论】:
存根层是向大多数开发人员公开的,它为您正在适应的任何数据模型/IDL/接口提供类型安全的绑定。
所以你基本上是在创建一个存根来与一个远程服务交互。它是用于调用远程服务的客户端接口。一般建议多次调用重复使用同一个 Stub。
RPC 截止日期以CallOptions 实现,可以在发送调用之前访问/更改。如果您想为每个单独的 RPC 单独设置截止日期,您可以实现一个 ClientInterceptor,它使用动态值修改调用的 CallOptions。类似于
private final AtomicInteger deadlineNano = new AtomicInteger();
class DeadlineAttachingInterceptor implements ClientInterceptor {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT>interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel next) {
return next.newCall(method, callOptions.withDeadlineAfter(deadlineNano.get(), TimeUnit.NANOSECONDS));
}
【讨论】: