Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In spring boot webflux based microservice, who is the subscriber?

Note: Here the terms Subscriber and Subscription are being used from the reactive streams specification.

Consider the following @RestController methods in a spring boot webflux based microservice.

    @GetMapping(path = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
    public Flux<TradingUser> listUsers() {
        return this.tradingUserRepository.findAll();
    }

    @GetMapping(path = "/users/{username}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<TradingUser> showUsers(@PathVariable String username) {
        return this.tradingUserRepository.findByUserName(username);
    }
  1. Here "who/what" will act as the "Subscriber"? I assume the spring boot framework provides a Subscriber(?) Can someone please provide details or any links around this?

  2. Say I am invoking the restful endpoint above using client like postman/curl/browser, then in that case how can the client signal demand to the reactive server? (Only the Subscriber has a handle on the Subscription object with the request(n) method to signal demand. However, since Subscriber is probably also on the server side implemented by the spring boot framework, how can the actual client signal the demand?) I am obviously missing something.

like image 877
Jatin Avatar asked Jan 10 '18 06:01

Jatin


2 Answers

Currently with HTTP, the exact backpressure information is not transmitted over the network, since the HTTP protocol doesn't support this. This can change if we use a different wire protocol.

So the reactive streams demand is translated to/from the actual read/writes at the HTTP level.

If you looks at Spring Framework's org.springframework.http.server.reactive.ServletHttpHandlerAdapter, you'll see that this class does the adaptation between Servlet 3.1 Async I/O and Reactive Streams. It does implement a specific Subscriber class.

There are other specific adapter implementations for this as well: Undertow, Jetty, Tomcat, Reactor Netty. If the underlying server supports reactive streams, we'll simply let the server handle the demand. If not, a Subscriber implementation is used.

like image 98
Brian Clozel Avatar answered Oct 22 '22 02:10

Brian Clozel


Inside the dependency org.springframework.spring-web there is a function named public void service(... which calls .subscribe in ServletHttpHandlerAdapter. I think it is confusing sometimes to understand that the framework is handling this subscription under the hood when many tutorials on WebFlux show the subscriber of a Mono or Flux explicitly to demonstrate how reactive streams work but here it is done by the framework for us.

like image 3
caladeve Avatar answered Oct 22 '22 00:10

caladeve