用例是,我们有一个单独的流和通道,我们通过它发送具有非常大的有效负载的消息。同时,我们也有一个应用程序级别的ping,如果我们在最后10秒内没有看到新的消息,它就会超时。在速度较慢的网络上,从流中读取数据需要花费大量时间。
当完整的消息被读取和反序列化时,我们从GRPC得到的唯一回调是onNext。有没有办法获取较低级别的事件,比如正在进行的流读取?或者是否有一种方法可以拦截流阅读器
public interface StreamObserver<V> {
/**
* Receives a value from the stream.
*
* <p>Can be called many times but is never called after {@link #onError(Throwable)} or {@link
* #onCompleted()} are called.
*
* <p>Unary calls must invoke onNext at most once. Clients may invoke onNext at most once for
* server streaming calls, but may receive many onNext callbacks. Servers may invoke onNext at
* most once for client streaming calls, but may receive many onNext callbacks.
*
* <p>If an exception is thrown by an implementation the caller is expected to terminate the
* stream by calling {@link #onError(Throwable)} with the caught exception prior to
* propagating it.
*
* @param value the value passed to the stream
*/
void onNext(V value);
/**
* Receives a terminating error from the stream.
*
* <p>May only be called once and if called it must be the last method called. In particular if an
* exception is thrown by an implementation of {@code onError} no further calls to any method are
* allowed.
*
* <p>{@code t} should be a {@link io.grpc.StatusException} or {@link
* io.grpc.StatusRuntimeException}, but other {@code Throwable} types are possible. Callers should
* generally convert from a {@link io.grpc.Status} via {@link io.grpc.Status#asException()} or
* {@link io.grpc.Status#asRuntimeException()}. Implementations should generally convert to a
* {@code Status} via {@link io.grpc.Status#fromThrowable(Throwable)}.
*
* @param t the error occurred on the stream
*/
void onError(Throwable t);
/**
* Receives a notification of successful stream completion.
*
* <p>May only be called once and if called it must be the last method called. In particular if an
* exception is thrown by an implementation of {@code onCompleted} no further calls to any method
* are allowed.
*/
void onCompleted();
}发布于 2021-11-18 02:39:13
您可以添加ServerInterceptor或ServerStreamTracer.Factory来观察流事件。
或更改日志级别以获取所有事件的更多详细信息,如下所示:
public static void main(String[] args) throws InterruptedException{
setLogger("io.grpc");
}
private static void setLogger(String className) {
Logger logger = Logger.getLogger(className);
logger.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
logger.addHandler(handler);
}如果您希望使用logback打印日志,只需参考此文档:grpc-java-example#log
https://stackoverflow.com/questions/69998348
复制相似问题