server 全局拦截器
@GrpcGlobalServerInterceptor
@Slf4j
public class LogInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
log.info(serverCall.getMethodDescriptor().getFullMethodName());
return serverCallHandler.startCall(serverCall, metadata);
}
}
client全局拦截器
@GrpcGlobalClientInterceptor
@Slf4j
public class LogInterceptor implements ClientInterceptor {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
log.info("global .... LogInterceptor"+method.getFullMethodName());
return next.newCall(method, callOptions);
}
}
client自定义拦截器
AdRpcGrpc.AdRpcBlockingStub stub = AdRpcGrpc.newBlockingStub(serverChannel);
AdInfo.AdId adId = AdInfo.AdId.newBuilder().setId(id).build();
AdInfo.Ad ad = stub
.withDeadline(Deadline.after(100, TimeUnit.MILLISECONDS))
.withInterceptors(log2Interceptor)
.getAd(adId);
AdInfo.Ad ad = stub
.withDeadline(Deadline.after(100, TimeUnit.MILLISECONDS))
.withInterceptors(log2Interceptor)
.getAd(adId);
但是超时可能会存在问题,初次建立连接会有短暂的超时情况
responseObserver.onError(new StatusException(Status.NOT_FOUND));
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。