adv*_*sov 5 spring-boot grpc grpc-java
第一次调用通常会成功,但随后我会收到如下消息:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
客户端代码:
@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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
截止日期是一个绝对时间点,在您创建存根时立即设置(不一定是在执行它时) - 这与相对于调用开始的超时形成对比。
因此,负期限意味着它在您的存根执行之前就过期了。
要解决此问题,您应该在拨打电话之前立即设定截止日期。
var response = blockingStub.withDeadlineAfter(300000, TimeUnit.MILLISECONDS)
.yourRpcName();
Run Code Online (Sandbox Code Playgroud)
在此阅读有关截止日期的更多信息
| 归档时间: |
|
| 查看次数: |
10721 次 |
| 最近记录: |