为什么不推荐使用 StreamListener

时间:2021-04-02 20:38:28

标签: spring spring-cloud-stream spring-cloud-stream-binder-kafka

我将 Spring Cloud Stream 3.1.2 用于 KafkaStreams。编程模型是:

  • 函数式编程
  • 命令式编程

后者使用注解作为 Spring 提供使用的所有其他注解。但是,有人提到

<块引用>

从 3.1.0 版本的 binder 开始,我们建议对基于 Kafka Streams binder 的应用程序使用上述函数式编程模型。从 Spring Cloud Stream 3.1.0 开始,不推荐使用对 StreamListener 的支持。

因为我认为旧模型更具可读性(至少对我而言)。任何人都可以解释为什么决定弃用它以支持函数式编程并且会被删除吗?

1 个答案:

答案 0 :(得分:3)

在 Spring 博客文章 (https://spring.io/blog/2019/10/17/spring-cloud-stream-functional-and-reactive) 中,它说 a functional programming model in Spring Cloud Stream (SCSt). It’s less code, less configuration. Most importantly, though, your code is completely decoupled and independent from the internals of SCSt。这有利于使用 Project Reactor (https://projectreactor.io/) 提供的事件流抽象(例如 Flux 和 Mono)。命令式函数在每个单独的事件上触发,而响应式函数则触发一次。