Can I use the block () method for Flux returned from Spring5 WebClient?

I created a Spring Boot 2.0 demo application that contains two applications that communicate using WebClient. And I suffer from the fact that they often stop communicating when I use the Block () Flux method from the WebClient response. I want to use List not Flux for some reason.

The server-side application is as follows. It just returns a Flux object.

@GetMapping
public Flux<Item> findAll() {
    return Flux.fromIterable(items);
}

And the client side application (or BFF) looks like this. I get Flux from the server and convert it to List by calling the block () method.

@GetMapping
public List<Item> findBlock() {
    return webClient.get()
        .retrieve()
        .bodyToFlux(Item.class)
        .collectList()
        .block(Duration.ofSeconds(10L));
}

, findBlock() . findBlock(), Flux, collectList() block(), . , block() .
, findAll(), List, .

. https://github.com/cero-t/webclient-example

"" - , "front" - . , localhost: 8080, , , localhost: 8080/block, , , .


, "spring -boot-starter-web" "" ( ) pom.xml, , tomcat, . Netty?

.

+4
1

-, , Flux.fromIterable(items) , items , -. , , API - . , . , Flux.just(item1, item2, item3).

:

@GetMapping("/")
public Flux<Item> findFlux() {
  return webClient.get()
    .retrieve()
    .bodyToFlux(Item.class);
}

Item /, / " " .

, :

@GetMapping("/block")
public List<Item> findBlock() {
  return webClient.get()
    .retrieve()
    .bodyToFlux(Item.class)
    .collectList()
    .block(Duration.ofSeconds(10L));
}

collectList, . , .

, . , :

WARN 3075 --- [ctor-http-nio-7] io.netty.util.concurrent.DefaultPromise  : An exception was thrown by reactor.ipc.netty.channel.PooledClientContextHandler$$Lambda$532/356589024.operationComplete()

reactor.core.Exceptions$BubblingException: java.lang.IllegalArgumentException: Channel [id: 0xab15f050, L:/127.0.0.1:59350 - R:localhost/127.0.0.1:8081] was not acquired from this ChannelPool
    at reactor.core.Exceptions.bubble(Exceptions.java:154) ~[reactor-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]

, -netty, ​​ 0.7.4.RELEASE. , , , HTTP- .

spring-boot-starter-web Tomcat, Spring WebFlux MVC Spring ( , ). Tomcat, spring-boot-starter-tomcat POM, Tomcat Spring WebFlux.

+2

Source: https://habr.com/ru/post/1692906/


All Articles