如何在GRPC服务器端流式传输中,在客户端重新启动时继续数据流?

huangapple go评论87阅读模式
英文:

How to continue stream if client is restart in GRPC Server Side Streaming?

问题

@Override
public void response(RequestApp request, Grpc.Stub asyncStub) {

    StreamObserver<Dto> streamObserver = new StreamObserver<Dto>() {
        @Override
        public void onNext(Response response) {
            LOGGER.info("已接收到响应。");
        }

        @Override
        public void onError(Throwable thrwbl) {
            LOGGER.error("发生错误。");
        }

        @Override
        public void onCompleted() {
            LOGGER.info("流已完成。");
        }
    };

    asyncStub.method(request, streamObserver);
}

Grpc客户端代码如上所示。其中涉及服务器端流式传输。如果客户端重新启动,在服务器端流式传输开始后,如何继续此流式传输?

英文:
    @Override
    public void response(RequestApp request, Grpc.Stub asyncStub) {

        StreamObserver&lt;Dto&gt; streamObserver = new StreamObserver&lt;Dto&gt;() {
            @Override
            public void onNext(Response response) {
                LOGGER.info(&quot;Response is received.&quot;);
            }

            @Override
            public void onError(Throwable thrwbl) {
                LOGGER.error(&quot;Get error.&quot;);
            }

            @Override
            public void onCompleted() {
                LOGGER.info(&quot;Stream is completed.&quot;);
            }
        };

        asyncStub.method(request, streamObserver);
}

The Grpc Client code is like that above. There is server side streaming in here. If the client is restart, after server side stream is started, how to continue this stream ?

答案1

得分: 1

你不能在连接由于任何原因断开时恢复正在进行的流。如果您希望服务器从数据流中的先前某一点开始发送数据,可以要求客户端在请求中包含一个标记,服务器可以使用该标记来指示从何处开始。

英文:

You cannot resume a stream in progress if the connection is lost for any reason. If you want the server to start sending data from a previous point in the data stream, you can have the client include a token in the request that the server can use as an indication of where to begin.

huangapple
  • 本文由 发表于 2020年10月7日 16:06:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64239834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定