 Reactor - 快速上手
Reactor - 快速上手
  # 介绍 Reactor
Reactor 是一个用于 JVM 的完全非阻塞的响应式编程框架,具备高效的需求管理(即对「背压(backpressure)」的控制)能力。它与 Java 8 函数式 API 直接集成,比如 CompletableFuture, Stream, 以及 Duration。它提供了异步序列 API Flux(用于 [N] 个元素)和 Mono(用于 [0|1] 个元素),并完全遵循和实现了「响应式扩展规范」(Reactive Extensions Specification)。
Reactor 的 reactor-ipc 组件还支持非阻塞的进程间通信(inter-process communication, IPC)。 Reactor IPC 为 HTTP(包括 Websockets)、TCP 和 UDP 提供了支持背压的网络引擎,从而适合 应用于微服务架构。并且完整支持响应式编解码(reactive encoding and decoding)。
# 前提
Reactor Core 运行于 Java 8 及以上版本。
依赖 org.reactive-streams:reactive-streams:1.0.2。
Andriod 支持方面:
- Reactor 3 并不正式支持 Andorid(如果需要可以考虑使用 RxJava 2)
- 但是,在 Android SDK 26(Android 0)及以上版本应该没问题
- 我们希望能够最大程度兼顾对 Android 的支持,但是我们并不能作出保证,具体情况具体分析
# 了解 BOM
自从 reactor-core 3.0.4,随着 Aluminium 版本发布上车(release train)以来,Reactor 3 使用了 BOM(Bill of Materials,一种标准的 Maven artifact)。
使用 BOM 可以管理一组良好集成的 maven artifacts,从而无需操心不同版本组件的互相依赖问题。
BOM 是一系列有版本信息的 artifacts,通过「列车发布」(release train)的发布方式管理, 每趟发布列车由一个「代号 + 修饰词」组成,比如:
Aluminium-RELEASE
Carbon-BUILD-SNAPSHOT
Aluminium-SR1
Bismuth-RELEASE
Carbon-SR32
2
3
4
5
代号替代了传统的「主版本.次版本」的数字形式。这些代号主要来自 Periodic Table of Elements (opens new window), 按首字母顺序依次选取。
修饰词有(按照时间顺序):
- BUILD-SNAPSHOT
- M1..- N: 里程碑号
- RELEASE: 第一次 GA (General Availability) 发布
- SR1..- N: 后续的 GA 发布(类似于 PATCH 号或 SR(Service Release))
# 获取 Reactor
前边提到,使用 Reactor 的最简单方式是在你的项目中配置 BOM 以及相关依赖。 注意,当你这样添加依赖的时候,要省略版本(<version>)配置,从而自动使用 BOM 中指定的版本。
当然,如果你希望使用某个版本的 artifact,仍然可以指定。甚至完全不使用 BOM,逐个配置 artifact 的版本也是可以的。
# Maven 配置
Maven 原生支持 BOM。首先,你需要在 pom.xml 内通过添加下边的代码引入 BOM。如果 (dependencyManagement) 已经存在,只需要添加其内容即可。
<dependencyManagement> 
    <dependencies>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-bom</artifactId>
            <version>Bismuth-RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
2
3
4
5
6
7
8
9
10
11
注意 dependencyManagement 标签用来补充通常使用的 dependencies 配置。
然后,在 dependencies 中添加相关的 reactor 项目,省略 <version>,如下:
<dependencies>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>  <!-- 1 -->
        <!-- 2 -->
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId> <!-- 3 -->
        <scope>test</scope>
    </dependency>
</dependencies>
2
3
4
5
6
7
8
9
10
11
12
- 依赖 Core 库
- 没有 version 标签
- reactor-test提供了对 reactive streams 的单测
# Gradle 配置
Gradle 没有对 Maven BOM 的支持,但是你可以使用 Spring 的 gradle-dependency-management (opens new window) 插件。
首先,apply 插件。
plugins {
    id "io.spring.dependency-management" version "1.0.1.RELEASE" 
}
2
3
请自行使用合适的版本。
然后用它引入 BOM:
dependencyManagement {
     imports {
          mavenBom "io.projectreactor:reactor-bom:Bismuth-RELEASE"
     }
}
2
3
4
5
最好添加一个依赖在你的项目,不需要有版本号
dependencies {
     compile 'io.projectreactor:reactor-core' 
}
2
3
# Milestones 和 Snapshots
里程碑版(Milestones)和开发预览版(developer previews)通过 Spring Milestones repository 而不是 Maven Central 来发布。 需要添加到构建配置文件中,如:
Milestones in Maven
<repositories>
        <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones Repository</name>
                <url>https://repo.spring.io/milestone</url>
        </repository>
</repositories>
2
3
4
5
6
7
gradle 使用下边的配置:
Milestones in Gradle
repositories {
  maven { url 'http://repo.spring.io/milestone' }
  mavenCentral()
}
2
3
4
类似的,snapshot 版也需要配置专门的库:
BUILD-SNAPSHOTs in Maven
<repositories>
        <repository>
                <id>spring-snapshots</id>
                <name>Spring Snapshot Repository</name>
                <url>https://repo.spring.io/snapshot</url>
        </repository>
</repositories>
2
3
4
5
6
7
BUILD-SNAPSHOTs in Gradle
repositories {
  maven { url 'http://repo.spring.io/snapshot' }
  mavenCentral()
}
2
3
4
