如何在 Webflux 应用程序中制作 Spring Cloud Stream 消费者?
How to make Spring Cloud Stream consumer in Webflux application?
我有一个基于 Webflux 的微服务,它有一个简单的响应式存储库:
public interface NotificationRepository extends ReactiveMongoRepository<Notification, ObjectId> {
}
现在我想扩展这个微服务来使用来自 Kafka 的事件消息。这个 message/event 将被保存到数据库中。
对于 Kafka 监听器,我使用了 Spring Cloud Stream。我创建了一些简单的 Consumer,它运行良好 - 我能够使用消息并将其保存到数据库中。
@Bean
public Consumer<KStream<String, Event>> documents(NotificationRepository repository) {
return input ->
input.foreach((key, value) -> {
LOG.info("Received event, Key: {}, value: {}", key, value);
repository.save(initNotification(value)).subscribe();
});
}
但这是连接 Spring Cloud Stream 消费者和反应式存储库的正确方法吗?它看起来不像是我最后必须调用 subscribe()
的时候。
我读了 Spring Cloud Stream documentation (for 3.0.0 release) 他们说
Native support for reactive programming - since v3.0.0 we no longer distribute spring-cloud-stream-reactive modules and instead relying on native reactive support provided by spring cloud function. For backward compatibility you can still bring spring-cloud-stream-reactive from previous versions.
并且在 this presentation video 中他们提到他们有使用项目反应器的反应式编程支持。所以我想有一种方法我只是不知道。你能告诉我正确的做法吗?
如果这听起来太愚蠢,我深表歉意,但我对 Spring Cloud Stream 和响应式编程还很陌生,还没有找到很多描述这一点的文章。
只要使用 Flux 作为消费类型,就像这样:
@Bean
public Consumer<Flux<Message<Event>>> documents(NotificationRepository repository) {
return input ->
input
.map(message-> /*map the necessary value like:*/ message.getPayload().getEventValue())
.concatMap((value) -> repository.save(initNotification(value)))
.subscribe();
}
如果您使用 Function
和空 return 类型 (Function<Flux<Message<Event>>, Mono<Void>>
) 而不是消费者,则框架可以自动订阅。使用 Consumer
您必须手动订阅,因为框架没有对流的引用。但在 Consumer
情况下,您订阅的不是存储库,而是整个流,这没问题。
我有一个基于 Webflux 的微服务,它有一个简单的响应式存储库:
public interface NotificationRepository extends ReactiveMongoRepository<Notification, ObjectId> {
}
现在我想扩展这个微服务来使用来自 Kafka 的事件消息。这个 message/event 将被保存到数据库中。
对于 Kafka 监听器,我使用了 Spring Cloud Stream。我创建了一些简单的 Consumer,它运行良好 - 我能够使用消息并将其保存到数据库中。
@Bean
public Consumer<KStream<String, Event>> documents(NotificationRepository repository) {
return input ->
input.foreach((key, value) -> {
LOG.info("Received event, Key: {}, value: {}", key, value);
repository.save(initNotification(value)).subscribe();
});
}
但这是连接 Spring Cloud Stream 消费者和反应式存储库的正确方法吗?它看起来不像是我最后必须调用 subscribe()
的时候。
我读了 Spring Cloud Stream documentation (for 3.0.0 release) 他们说
Native support for reactive programming - since v3.0.0 we no longer distribute spring-cloud-stream-reactive modules and instead relying on native reactive support provided by spring cloud function. For backward compatibility you can still bring spring-cloud-stream-reactive from previous versions.
并且在 this presentation video 中他们提到他们有使用项目反应器的反应式编程支持。所以我想有一种方法我只是不知道。你能告诉我正确的做法吗?
如果这听起来太愚蠢,我深表歉意,但我对 Spring Cloud Stream 和响应式编程还很陌生,还没有找到很多描述这一点的文章。
只要使用 Flux 作为消费类型,就像这样:
@Bean
public Consumer<Flux<Message<Event>>> documents(NotificationRepository repository) {
return input ->
input
.map(message-> /*map the necessary value like:*/ message.getPayload().getEventValue())
.concatMap((value) -> repository.save(initNotification(value)))
.subscribe();
}
如果您使用 Function
和空 return 类型 (Function<Flux<Message<Event>>, Mono<Void>>
) 而不是消费者,则框架可以自动订阅。使用 Consumer
您必须手动订阅,因为框架没有对流的引用。但在 Consumer
情况下,您订阅的不是存储库,而是整个流,这没问题。