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