有一个由Flux.create
方法以编程方式创建的通量:
Flux<Tweet> flux = Flux.<Tweet>create(emitter -> ...);
有一个休息控制器:
@RestController
public class StreamController {
...
@GetMapping("/top-words")
public Flux<TopWords> streamTopWords() {
return topWordsStream.getTopWords();
}
}
有几个 Web 客户端(在独立进程中):
Flux<TopWords> topWordsFlux = WebClient.create(".../top-words")
.method(HttpMethod.GET)
.accept(MediaType.TEXT_EVENT_STREAM)
.retrieve()
.bodyToFlux(TopWords.class)
.subscribe(System.out::println);
JavaScript 中有几个 EventSource 实例:
var eventSource = new EventSource(".../top-words");
eventSource.onmessage = function (e) {
console.log("Processing message: ", e.data);
};
只有前两个“订阅者”会开始接收消息(无论是 Web 客户端还是 EventSource 实例)。另一个将打开连接,获得 HTTP 200 状态,但事件流保持为空。客户端或服务器端都没有错误。
我不明白,对“2 个订阅者”的限制在哪里。如果我想支持超过 2 个订阅者,我该怎么做?
该应用程序使用 Spring Boot 2.0.0.RELEASE 构建并使用 spring-boot-starter-webflux 自动配置。默认配置没有改变。