端到端的反应流RESTful服务 [英] End-to-End Reactive Streaming RESTful service

查看:90
本文介绍了端到端的反应流RESTful服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读这篇文章我不知道这是不是一个愚蠢的问题。我是反应式编程的新手。

I have read this post and I don't know if it is a stupid question. I am completely newbie in reactive programming.

问题很简单:假设我有一个完全被动的后端,我怎样才能流式传输到浏览器(大文本) ,例如),并且一旦它们来自服务器就将每个块打印给用户?

The question is very simple: supposing I have a fully-reactive back-end, how can I stream to browser (a large text, for instance), and print each chunk to the user as soon as they come from server?

可能我错过了一些重要的概念点,但我只需要知道是:我可以使用一个HTTP GET(?或不是)请求发送小部分数据(从服务器到浏览器)?关键是:我可以写出那些小部件,直到整个数据被发送?

Probably I am missing some important conceptual points, but all I need to know is: Can I send small parts of data (from server to browser), with one single HTTP GET (? or not) request? The key is: can I write those small parts, until the whole data is sent?

感谢您的帮助。我以前真的尝试过谷歌,但我总是得到有关其他概念的信息(比如websockets,long-polling,react.js),我认为情况并非如此。

Thanks for any help. I really tried google before, but I always get information about other concepts (like websockets, long-polling, react.js), that I think it is not the case.

编辑:我不是要求特定的API ou库。我只是想理解这个概念,例如:你不能用HTTP协议做这个,期间!或者这与反应式编程无关,你对于什么流的真正含义感到困惑。请参阅'something_else.js'。

I'am not asking for a specific API ou library. I just want to understand the concept, for example: "You CANNOT do this with HTTP protocol, period!" or "This has NOTHING to do with Reactive Programming, you are confused about what stream really means. See 'something_else.js' ".

EDIT2:我做了一点休息使用spring-mvc(spring-boot)的控制器使用此方法:

I did a small rest controller using spring-mvc (spring-boot) with this method:

@RequestMapping(value = "/flush", method = RequestMethod.GET)
public void flushTest(HttpServletResponse response) {

    response.setContentType("text/html");
    response.setStatus(SC_OK);

    try (ServletOutputStream outputStream = response.getOutputStream()) {
        for (int i = 0; i < 3; i++) {
            String chunk = "chunk_" + i;
            outputStream.print(chunk);
            outputStream.flush();
            response.flushBuffer();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

在浏览器中,只有整体响应到了,甚至使用flushBuffer。经过一些研究,这可能是一个TOMCAT问题,我改变了我的pom.xml,包括下载:

In the browser, only the whole response arrived, even using the flushBuffer. After some research, that this could be a TOMCAT issue, I changed my pom.xml including undertow:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

使用默认配置:

server.undertow.accesslog.enabled=true
server.undertow.accesslog.dir=target/logs
server.undertow.accesslog.pattern=combined
server.compression.enabled=true
server.compression.min-response-size=1l

At这一刻,在我的IDE中使用debug,每个块都在flushBuffer之后写入。

At this moment, using debug in my IDE, each chunk was being written after the flushBuffer.

所以,我相信这可能是TOMCAT配置。长话短说,我知道有很多东西可以帮助我解决流媒体解决方案,并感谢所有的评论,但我的问题更具概念性。

So, I believe this could be a TOMCAT configuration. Long story short, I know there are a lot of things to help me on "streaming solutions", and thanks for all the comments, but my problem was a little bit more conceptual.

推荐答案

在HTTP之上有一个协议,允许使用HTTP传输将较小的,准备好的消息块中的服务器响应传递给浏览器。它被称为 SSE (服务器发送事件)或 EventSource 这里是关于该主题的非常完整的文章。

There is a protocol on top of HTTP which allows delivery of a server response in smaller, ready-to-be-consumed, chunks to the browser using HTTP transport. It's called SSE (Server Sent Events) or EventSource, here is pretty thoroughly assembled article on the topic.

还有其他方法可以使用HTTP协议流式传输数据。一种这样的替代方案是JSON流,其中您从服务器在线路上写入部分响应,并在它们到达时使用JSON块。在消费方面,一个流行的图书馆在服务器上被称为 Oboe.js 。当你想要发送它时,或者当它可用时,你基本上需要在响应线上写入部分数据。

There are other means to stream data using the HTTP protocol. One such alternative is JSON streaming, in which you write partial responses on the wire from the server and consume the JSON chunks when they arrive. On the consuming side a popular library is called Oboe.js, on the server side you basically need to write the partial data on the response wire when you want to sent it, or when it's available.

对于这两种方法,Rx对于处理流媒体非常有用服务器端的逻辑。您将这些碎片建模为流(处理错误等),最后,在订购者中,您在线上写下单个排放,在Observable完成时关闭响应。

For both approaches Rx is useful for handling the streaming logic on the server side. You model the pieces as a stream (handle errors, etc.) and eventually, in the Subscriber, you write the single emissions on the wire, closing the response when the Observable is finished.

在客户端,EventSource事件可以包装在一个新的Observable中(通过 Rx.Observable.create())并作为流进一步处理。此外,Oboe.js事件可以转换为Observables(通过 Rx.Observable.fromEvent()

On the client side, the EventSource events can be wrapped in a new Observable (via Rx.Observable.create()) and processed further as a stream. Also Oboe.js events can be converted into Observables (via Rx.Observable.fromEvent().

这篇关于端到端的反应流RESTful服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆