Stream response from HTTP client with Spring/Project reactor

主宰稳场 提交于 2021-01-28 12:20:08

问题


How to stream response from reactive HTTP client to the controller without having the whole response body in the application memory at any time?

Practically all examples of project reactor client return Mono<T>. As far as I understand reactive streams are about streaming, not loading it all and then sending the response.

Is it possible to return kind of Flux<Byte> to make it possible to transfer big files from some external service to the application client without a need of using a huge amount of RAM memory to store intermediate result?


回答1:


It should be done naturally by simply returning a Flux<WHATEVER>, where each WHATEVER will be flushed on the network as soon as possible. In such a case, the response uses chunked HTTP encoding, and the bytes from each chunk are discarded once they've been flused to the network.

Another possibility is to upgrade the HTTP response to SSE (Server Sent Events), which can be achieved in WebFlux by setting the Controller method to something like @GetMapping(path = "/stream-flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE) (the produces part is the important one).




回答2:


I dont think that in your scenario you need to create an event stream because event stream is more used to emit event in real time i think you better do it like this.

@GetMapping(value = "bytes")
public Flux<Byte> getBytes(){
    return byteService.getBytes();
}

and you can send it es a stream. if you still want it as a stream

@GetMapping(value = "bytes",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<List<Byte>> getBytes(){
    return byteService.getBytes();
}


来源:https://stackoverflow.com/questions/53592011/stream-response-from-http-client-with-spring-project-reactor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!