什么时候必须创建异步存根< GRPC&gt ;? [英] When I must create async stub <GRPC>?

查看:227
本文介绍了什么时候必须创建异步存根< GRPC&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 8888).usePlaintext().build();
Grpc.Stub stub = CLIGrpc.newStub(managedChannel);

何时必须创建存根?当每个方法被调用或仅被调用一次时?

When I must create stub? When every method being called or only once?

推荐答案


Stub层是暴露给大多数人的东西开发人员,并为您要适应的任何数据模型/ IDL /接口提供类型安全的绑定。

The Stub layer is what is exposed to most developers and provides type-safe bindings to whatever datamodel/IDL/interface you are adapting.

因此,您基本上是在创建一个存根来与一个远程服务进行交互。 。它是用于调用远程服务的客户端接口。通常建议对多个调用重复使用相同的存根。

So you are basically creating one stub for interacting with one remote service. It is the client side interface for invoking the remote service. It is generally recommended to reuse the same Stub for multiple calls.

RPC截止日期实现为 CallOptions ,可以对其进行访问/更改。在发送呼叫之前。如果要为每个单独的RPC分别设置截止日期,则可以实现 ClientInterceptor 可以使用动态值修改呼叫的 CallOptions 。类似于

RPC deadlines are implemented as CallOptions, which can be accessed/mutated before the call is sent. If you want to set deadline for each individual RPCs independently, you can implement a ClientInterceptor that modifies the call's CallOptions with dynamic values. Something similar to

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));
}

这篇关于什么时候必须创建异步存根&lt; GRPC&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆