我有一个大列表调用列表对象= 1000+记录,所以我在spring引导中创建了一个端点
@GetMapping("/path")
public Flux<Something> streamAPI(){
List<Something> object = [1000+ Records]
return Flux.fromIterable(object).delayElements(Duration.ofSeconds(1));
}作为消费者,我使用webclient从另一个应用程序调用上面的API。
FileWriter jsonFileWriter = new FileWriter("example.json");
return webclient.get().uri(builder -> builder.scheme("http").host("localhost").port("8888")
.path("/path").build()).retrieve().onStatus(HttpStatus::is4xxClientError, response ->
{
throw new NotFoundException("Not Found");
}).onStatus(HttpStatus::is5xxServerError, response -> {
throw new InternalServerError("Internal Server Error");
}).bodyToFlux(Something.class).doOnNext(data -> {
try {
jsonFileWriter.write(data.toString() + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}).doOnComplete(() -> {
try {
jsonFileWriter.flush();
jsonFileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
})如何--但是在这里我正面临着将数据写入文件的问题,它花费了大约20秒的时间,并立即写入了20条记录,我无法在每秒钟看到写入数据。有人能帮上忙吗.
发布于 2021-05-21 00:55:37
这是意料之中的。
光是添加通量是不够的。您需要在您的produces = MediaType.TEXT_EVENT_STREAM_VALUE中添加GetMapping。
@GetMapping("/path", produces = MediaType.TEXT_EVENT_STREAM_VALUE)https://stackoverflow.com/questions/67598503
复制相似问题