问题
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);
}
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?
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.
回答1:
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.
来源:https://stackoverflow.com/questions/48181801/in-spring-boot-webflux-based-microservice-who-is-the-subscriber