理解Reactive Programing

现在很多人都在讨论响应式编程,不少框架中都可以看到响应式编程的概念,比较好奇,到底什么是响应式编程?

定义

常见的Reactive说法:

  • 异步非阻塞编程
  • 能够提升系统性能
  • 能解决传统编程的困境

维基百科

In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. With this paradigm it is possible to express static (e.g., arrays) or dynamic (e.g., event emitters) data streams with ease, and also communicate that an inferred dependency within the associated execution model exists, which facilitates the automatic propagation of the changed data flow

在计算时,响应式编程是一种声明式的编程范式连接数据流和传播数据的变化,使用这种范式可以更加简单的表达静态(如数组)或动态(如事件发射器)的数据流。

另外它能推断执行中的依赖关系,有助于数据流改变的时候能够自动的传播变化。

关键:

  • 数据流(data streams)
  • 传播变化(propagation of change)

Reactive programming has been proposed as a way to simplify the creation of interactive user interfaces and near-real-time system animation

响应式编程已经被提议为一种简化用户交互接口和实时响应系统的方式。

https://en.wikipedia.org/wiki/Reactive_programming

Reactive宣言

Reactive Systems are:Responsive,Resilient,Elastic,Message Driver

响应式系统是:响应式的,弹性的,优雅的,消息驱动的。

  • Responsive
  • Resilient
  • Elastic
  • Message Driver

https://www.reactivemanifesto.org/

Spring定义

In plain terms reactive programming is about non-blocking applications that are asynchronous and event-driven and require a small number of threads to scale. A key aspect of that definition is the concept of backpressure which is a mechanism to ensure producers don’t overwhelm consumers. For example in a pipeline of reactive components that extends from the database to the HTTP socket when the HTTP client is slow the data repository slows down or stops until capacity frees up.

From a programming model perspective reactive programming involves a major shift from imperative style logic to a declarative composition of async logic. It is comparable to using CompletableFuture in Java 8 and composing follow-up actions via lambda expressions.

响应式编程是一种非阻塞的应用,它是异步和事件驱动的,并且在扩展的时候需要很少量的线程。背压是其一个关键的概念,也是一种机制来确保生产者不会压垮消费者。

从编程模型的视角,响应式编程主要从表达式的写法迁移到声明式的写法。

关键点:

  • 非阻塞应用
  • 异步
  • 事件驱动
  • 扩展时需要线程少
  • 背压

https://docs.spring.io/spring-framework/docs/5.0.0.M1/spring-framework-reference/html/web-reactive.html

https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/spring-framework-reference/web-reactive.html#webflux-why-reactive

Reactor定义

The reactive programming paradigm is often presented in object-oriented languages as an extension of the Observer design pattern. You can also compare the main reactive streams pattern with the familiar Iterator design pattern, as there is a duality to the Iterable-Iterator pair in all of these libraries. One major difference is that, while an Iterator is pull-based, reactive streams are push-based.

Reactor是维基百科中对响应式编程的实现,并且做了一些自己的理解。响应式编程是一种编程范式,在面向对象编程中扩展了观察者模式,也可以用于和迭代器模式做对比,与迭代器模式的主要区别是它是push数据,而迭代器是pull数据。

关键点:

  • 维基百科定义的实现
  • 扩展了观察者模式
  • push数据

https://projectreactor.io/docs/core/release/reference/#intro-reactive

ReactiveX定义

ReactiveX并没有说自己是响应式编程,它利用了响应式编程的东西。

ReativeX: An API for asynchronous programming with observable streams

ReactiveX is more than an API, it's an idea and a breakthrough in programming. It has inspired several other APIs, frameworks, and even programming languages.

它不仅仅是一个API,它是一种理念和编程中的突破,影响了一些其它的API,框架,甚至编程语言。

至于实现上:则是扩展了观察者模式,提供了操作符处理数据流,声明式的写法。

http://reactivex.io/intro.html

最后

概念很多,但进行一次整合,大体概念:

响应式编程,异步,事件驱动,扩展观察者模式,扩容时消耗资源少,关注数据与数据的变化。

先这样说吧...

参考:

https://medium.com/@kevalpatel2106/what-is-reactive-programming-da37c1611382

你可能感兴趣的:(理解Reactive Programing)