mszhe的技术分享 人心惟危,道心惟微。惟精惟一,允执厥中。

响应式编程


什么是响应式编程

From Wikipedia

In computing, reactive programming is a declarative programming paradigm concerned 
with data streams and the propagation of change.
  • 关键字:
    • declarative programming paradigm 声明式编程范式
    • data streams 数据流
    • propagation of change 变化的传播
  • declarative programming 声明式编程
    public static void declarative() {
      int[] iArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
      Arrays.stream(iArr).forEach(System.out::println);
    }
    
  • imperative programming 命令式编程
    public static void imperative() {
      int[] iArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
      for (int i = 0; i < iArr.length; i++) {
          System.out.println(iArr[i]);
      }
    }
    

From Spring WebFlux framework

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 vertically (i.e. within the JVM) 
rather than horizontally (i.e. through clustering).

A key aspect of reactive applications is the concept of backpressure which is a mechanism to 
ensure producers don’t overwhelm consumers. For example in a pipeline of reactive components 
extending from the database to the HTTP response when the HTTP connection is too slow the data 
repository can also slow down or stop completely until network capacity frees up.

Reactive programming also leads to a major shift from imperative to declarative async composition 
of logic. It is comparable to writing blocking code vs using the CompletableFuture from Java 8 to 
compose follow-up actions via lambda expressions.
  • 关键字
    • non-blocking applications 非阻塞程序
    • asynchronous 异步
    • event-driven 事件驱动
    • scale vertically 水平扩展
    • scale horizontally 垂直扩展
    • backpressure 背压
    • pipeline 管道

响应式编程框架


上一篇 syscall monitor