10.2.2 添加 Reactor 依赖

让我们开始使用 Reactor 吧,把一下依赖添加到项目构建中:

  1. <dependency>
  2. <groupId>io.projectreactor</groupId>
  3. <artifactId>reactor-core</artifactId>
  4. </dependency>

Reactor 还提供了测试支持。你会写在你的 Reactor 代码周围写很多的测试,所以你肯定会想把这个依赖添加到项目构建中:

  1. <dependency>
  2. <groupId>io.projectreactor</groupId>
  3. <artifactId>reactor-test</artifactId>
  4. <scope>test</scope>
  5. </dependency>

我假定你要向 Spring Boot 项目中添加这些依赖,它可以为你处理的依赖管理,所以没有必要指定依赖的 <version> 元素。但是如果你想在非 Spring Boot 项目中使用 Reactor,那么你需要在构建中设置 Reactor 的 BOM(物料清单)。下面的依赖管理条目增加了 Reactor 的 Bismuth-RELEASE 到构建中:

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>io.projectreactor</groupId>
  5. <artifactId>reactor-bom</artifactId>
  6. <version>Bismuth-RELEASE</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>

现在,Reactor 在你的项目构建中了,可以使用 Mono 和 Flux 开始创建响应式管道了。对于本章的其余部分,我们将使用 Mono 和 Flux 提供的一些操作。