简体   繁体   English

Spring Boot Webflux 反应式 API

[英]Spring Boot Webflux reactive api

I was hoping to get some hints for a small Spring Boot Application I'm building.我希望得到一些关于我正在构建的小型 Spring Boot 应用程序的提示。

The application itself should function much like consuming a Kafka topic (or any pub/sub pattern).应用程序本身的功能应该很像使用 Kafka 主题(或任何发布/订阅模式)。 I however have no tools like Kafka available so it should all be done within the application itself.然而,我没有像 Kafka 这样的工具可用,所以这一切都应该在应用程序本身内完成。

The application will have a RestController that should expose a “hot” stream of Events when they happen.应用程序将有一个 RestController,它应该在事件发生时公开一个“热”事件流。

@GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux<Event> getApis() {
    return … 
}

The application will itself consume a stream of events from another application.应用程序本身将使用来自另一个应用程序的事件流。

Where I need some hints is - how can I treat each consumer of this API as a new subscriber - and publish event to them as I get them in?我需要一些提示的地方是 - 我如何将这个 API 的每个消费者视为新订阅者 - 并在他们进入时向他们发布事件? Implementing a full pub-sub pattern keeping track of subscribers and when they stop consuming the endpoint should of course be possible.实现一个完整的 pub-sub 模式来跟踪订阅者以及他们何时停止使用端点当然应该是可能的。 But I'm wondering if there isn't an easier solution using some of Springs Reactive functionality.但我想知道是否有使用某些 Springs Reactive 功能的更简单的解决方案。 The Events I get in doesn't need to be stored - just forwarded to each consumer subscribing at the moment of the event.我进入的事件不需要存储 - 只需转发给在事件发生时订阅的每个消费者。

Thanks谢谢

Before returning your Flux<Event> from the controller handler, make sure that you're always returning:在从控制器处理程序返回您的Flux<Event>之前,请确保您始终返回:

  • the same instance, which is kept for example as an attribute in the controller相同的实例,例如作为控制器中的属性保留
  • to apply the Flux.share() on it, so that all subscribers share the same hot stream of events在其上应用Flux.share() ,以便所有订阅者共享相同的热事件流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 MySql 启动响应式 Spring (Webflux) - Spring boot reactive (Webflux) with MySql WebFlux Spring Boot @Transactional 与反应式 MongoDB - WebFlux Spring Boot @Transactional with reactive MongoDB 如何使用 Spring Boot Reactive Webflux 在测试中解析字符串 []? - How to parse a String[] in tests with Spring Boot Reactive Webflux? 审核(@CreatedDate)不适用于带有反应式 MongoDB 的 WebFlux Spring Boot - Auditing (@CreatedDate) does not work for WebFlux Spring Boot with reactive MongoDB 在 Spring 响应式 Web (webflux) 中使用“/api”或其他前缀为所有端点添加前缀 - Prefix all endpoints with "/api" or another prefix, in spring reactive web (webflux) 在Maven中看不到spring-boot-starter-data-mongodb-active和spring-boot-starter-webflux依赖性 - Dont see spring-boot-starter-data-mongodb-reactive and spring-boot-starter-webflux dependency in maven Spring webflux 未运行响应式调用 - Spring webflux not running reactive call 更新 1 个或多个特定字段 MongoDB 使用 Spring 启动 WebFlux,Spring 数据 Z206E3718AF0921CC1D12F80ZReposit7 - Update 1 or multiple specific field MongoDB using Spring boot WebFlux,Spring Data MongoDB Reactive and ReactiveMongoRepository 重定向 /logout 请求到 /api/logout webflux spring boot - Redirect /logout request to /api/logout webflux spring boot WebTestClient - CORS 与 Spring 引导和 Webflux - WebTestClient - CORS with Spring Boot and Webflux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM