在 ProjectReactor 或 Reactive Streams 中,在您 subscribe() 之前不会发生任何事情。
除非有人订阅,否则响应式流数据流不会发生,但我看到所有 REST API(如查找、保存和插入)都没有显式调用订阅,但数据在生产者和订阅者之间流动。
@RestController
class PersonController {
private final PersonRepository repository;
public PersonController(PersonRepository repository) {
this.repository = repository;
}
@GetMapping("/all")
public Flux<Person> index() {
return repository.findAll();
}
@GetMapping("/people")
Flux<String> namesByLastname(@RequestParam Mono<String> lastname) {
Flux<Person> result = repository.findByLastname(lastname);
return result.map(it -> it.getFullName());
}
@PostMapping("/people")
Flux<People> AddPeople(@RequestBody Flux<Person> people) {
return repository.saveAll(people);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不需要调用REST 端点的订阅来启动 Project Reactor 中的数据流?
当我从浏览器调用时,REST 端点(HTTP 请求)如何自动订阅 Reactive Streams 以获取数据流?
我在这里错过了什么吗?
reactive-programming project-reactor reactive-streams spring-webflux
注意:此处,从反应式流规范中使用术语订户和订户。
在基于Spring Boot Webflux的微服务中考虑以下@RestController方法。
@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);
}
Run Code Online (Sandbox Code Playgroud)
在这里,“谁/什么”将充当“订户”?我假设Spring Boot框架提供了Subscriber(?),有人可以提供详细信息或与此相关的任何链接吗?
假设我正在使用诸如postman / curl / browser之类的客户端来调用上述宁静的端点,那么在这种情况下,客户端如何向响应服务器发出需求信号?(只有Subscriber在Subscription对象上具有使用request(n)方法来表示需求的句柄。但是,由于Subscriber可能也是在Spring Boot框架实现的服务器端,因此实际的客户端如何发出信号?)我显然缺少了一些东西。
spring-boot ×1