【问题标题】:Grpc throws deadline exceeded after negative number of seconds from now从现在开始的负数秒后,Grpc 抛出超过最后期限
【发布时间】:2021-09-11 16:30:43
【问题描述】:

第一次调用通常会成功,但随后出现异常消息,如下所示:

io.grpc.StatusRuntimeException:DEADLINE_EXCEEDED:ClientCall 在截止日期后开始:-175.597476157s 从现在开始

为什么秒数是负数?我该如何解决?

我的 grpc 配置:

public class MyAppLibGrpcSenderConfig {

    @Value("${grpc.client.host:localhost}")
    private String host;
    @Value("${grpc.client.port:9090}")
    private int port;
    @Value("${grpc.client.negotiationType:PLAINTEXT}")
    private String negotiationType;
    @Value("${grpc.client.deadline:300000}")
    private long deadline;

    @Autowired
    private Tracer tracer;

    @Bean
    public ManagedChannel managedChannel() {
        ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(host, port);
        if ("PLAINTEXT".equals(negotiationType)) {
            builder.usePlaintext();
        }
        return builder.build();
    }

    @Bean
    public TracingClientInterceptor tracingClientInterceptor(Tracer tracer) {
        return TracingClientInterceptor
                .newBuilder()
                .withTracer(this.tracer)
                .build();
    }

    @Bean
    public MyAppSenderServiceGrpc.MyAppSenderServiceBlockingStub myAppSenderServiceBlockingStub(
            TracingClientInterceptor tracingClientInterceptor,
            ManagedChannel managedChannel) {
        return MyAppSenderServiceGrpc
                .newBlockingStub(tracingClientInterceptor.intercept(managedChannel))
                .withDeadlineAfter(deadline, TimeUnit.MILLISECONDS);
    }

    @Bean
    public MyAppCodeLoaderServiceGrpc.MyAppCodeLoaderServiceBlockingStub myAppCodeLoaderServiceBlockingStub(
            TracingClientInterceptor tracingClientInterceptor,
            ManagedChannel managedChannel) {
        return MyAppCodeLoaderServiceGrpc
                .newBlockingStub(tracingClientInterceptor.intercept(managedChannel))
                .withDeadlineAfter(deadline, TimeUnit.MILLISECONDS);
    }
}

客户端代码:

@net.devh.boot.grpc.server.service.GrpcService
public class MyAppEventKafkaSender extends MyAppSenderServiceGrpc.MyAppSenderServiceImplBase {

    ...
    
    @SneakyThrows
    @Override
    public void sendMessage(ContextMyAppEventGrpc contextMyAppEventGrpc,
                            StreamObserver<Empty> responseObserver) {
        try {
            sendEvent(contextMyAppEventGrpc);
            Empty reply = Empty.newBuilder().build();
            responseObserver.onNext(reply);
            responseObserver.onCompleted();
        } catch (Exception e) {
            Status status = Status.INTERNAL.withDescription(e.getMessage());
            responseObserver.onError(status.asRuntimeException());
        }
    }
    
}

【问题讨论】:

    标签: spring-boot grpc grpc-java


    【解决方案1】:

    Deadline 是一个绝对时间点,并在您创建存根时立即设置(不一定在执行时设置) - 这与超时相对于打电话。

    所以负期限意味着它在你的存根被执行之前就过期了。

    要解决此问题,您应该在拨打电话前立即设置截止日期

    var response = blockingStub.withDeadlineAfter(300000, TimeUnit.MILLISECONDS)
                               .yourRpcName();
    

    阅读更多关于截止日期here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-04
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多