【问题标题】:Difference between ListenableFuture-style stub and async stub in grpc javagrpc java中ListenableFuture风格的存根和异步存根之间的区别
【发布时间】:2021-01-05 13:18:38
【问题描述】:

gRPC java 实现中 ListenableFuture 风格的存根和异步存根有什么区别?

哪一个用于异步通信?

public static MyServiceStub newStub(io.grpc.Channel channel) {
    io.grpc.stub.AbstractStub.StubFactory<MyServiceStub> factory =
      new io.grpc.stub.AbstractStub.StubFactory<MyServiceStub>() {
        @java.lang.Override
        public MyServiceStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
          return new MyServiceStub(channel, callOptions);
        }
      };
    return MyServiceStub.newStub(factory, channel);
  }


public static MyServiceBlockingStub newBlockingStub(
      io.grpc.Channel channel) {
    io.grpc.stub.AbstractStub.StubFactory<MyServiceBlockingStub> factory =
      new io.grpc.stub.AbstractStub.StubFactory<MyServiceBlockingStub>() {
        @java.lang.Override
        public MyServiceBlockingStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
          return new MyServiceBlockingStub(channel, callOptions);
        }
      };
    return MyServiceBlockingStub.newStub(factory, channel);
  }




public static MyServiceFutureStub newFutureStub(
      io.grpc.Channel channel) {
    io.grpc.stub.AbstractStub.StubFactory<MyServiceFutureStub> factory =
      new io.grpc.stub.AbstractStub.StubFactory<MyServiceFutureStub>() {
        @java.lang.Override
        public MyServiceFutureStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
          return new MyServiceFutureStub(channel, callOptions);
        }
      };
    return MyServiceFutureStub.newStub(factory, channel);
  }

【问题讨论】:

    标签: grpc grpc-java


    【解决方案1】:

    同步与异步存根对通信没有影响。 grpc-java 中的存根是 Channel API 之上的一个小垫片,存根的不同之处在于它们是不同的 API。只需选择与您希望接收响应的方式最匹配的存根类型。如果您喜欢异步 API,请使用异步 API;如果您喜欢 Future API,请使用 Future API;如果您喜欢同步,请使用同步。

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2010-09-25
      相关资源
      最近更新 更多