Logging a Reactive Sequence – 记录一个反应性的序列

最后修改: 2018年 11月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

With the introduction of Spring WebFlux, we got another powerful tool to write reactive, non-blocking applications. While using this technology is now way easier than before, debugging reactive sequences in Spring WebFlux can be quite cumbersome.

随着Spring WebFlux的推出,我们得到了另一个强大的工具来编写反应式、非阻塞式应用程序。虽然现在使用这项技术比以前容易得多,但在Spring WebFlux中调试反应式序列可能相当麻烦

In this quick tutorial, we’ll see how to easily log events in asynchronous sequences and how to avoid some simple mistakes.

在这个快速教程中,我们将看到如何在异步序列中轻松记录事件,以及如何避免一些简单的错误。

2. Maven Dependency

2.Maven的依赖性

Let’s add the Spring WebFlux dependency to our project so we can create reactive streams:

让我们把Spring WebFlux的依赖性添加到我们的项目中,这样我们就可以创建反应式流。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

We can get the latest spring-boot-starter-webflux dependency from Maven Central.

我们可以从Maven中心获得最新的spring-boot-starter-webflux依赖。

3. Creating a Reactive Stream

3.创建一个反应式流

To begin let’s create a reactive stream using Flux and use the log() method to enable logging:

首先,让我们使用Flux创建一个反应式流,并使用log() 方法来启用记录。

Flux<Integer> reactiveStream = Flux.range(1, 5).log();

Next, we will subscribe to it to consume generated values:

接下来,我们将订阅它以消耗生成的值。

reactiveStream.subscribe();

4. Logging Reactive Stream

4.记录反应流

After running the above application we see our logger in action:

运行上述应用程序后,我们看到我们的记录器在运行。

2018-11-11 22:37:04 INFO | onSubscribe([Synchronous Fuseable] FluxRange.RangeSubscription)
2018-11-11 22:37:04 INFO | request(unbounded)
2018-11-11 22:37:04 INFO | onNext(1)
2018-11-11 22:37:04 INFO | onNext(2)
2018-11-11 22:37:04 INFO | onNext(3)
2018-11-11 22:37:04 INFO | onNext(4)
2018-11-11 22:37:04 INFO | onNext(5)
2018-11-11 22:37:04 INFO | onComplete()

We see every event that occurred on our stream. Five values were emitted and then stream closed with an onComplete() event.

我们看到我们的流上发生的每一个事件。五个值被发射出来,然后流以onComplete()事件关闭。

5. Advanced Logging Scenario

5.高级记录场景

We can modify our application to see a more interesting scenario. Let’s add take() to Flux which will instruct the stream to provide only a specific number of events:

我们可以修改我们的应用程序以看到一个更有趣的场景。让我们在Flux中添加take(),这将指示流只提供特定数量的事件。

Flux<Integer> reactiveStream = Flux.range(1, 5).log().take(3);

After executing the code, we’ll see the following output:

执行该代码后,我们将看到以下输出。

2018-11-11 22:45:35 INFO | onSubscribe([Synchronous Fuseable] FluxRange.RangeSubscription)
2018-11-11 22:45:35 INFO | request(unbounded)
2018-11-11 22:45:35 INFO | onNext(1)
2018-11-11 22:45:35 INFO | onNext(2)
2018-11-11 22:45:35 INFO | onNext(3)
2018-11-11 22:45:35 INFO | cancel()

As we can see, take() caused the stream to cancel after emitting three events.

我们可以看到,take()导致流在发出三个事件后取消。

The placement of log() in your stream is crucial. Let’s see how placing log() after take() will produce different output:

log()在你的流中的位置至关重要。让我们看看将log()放在take()之后会产生什么样的输出。

Flux<Integer> reactiveStream = Flux.range(1, 5).take(3).log();

And the output:

还有输出。

2018-11-11 22:49:23 INFO | onSubscribe([Fuseable] FluxTake.TakeFuseableSubscriber)
2018-11-11 22:49:23 INFO | request(unbounded)
2018-11-11 22:49:23 INFO | onNext(1)
2018-11-11 22:49:23 INFO | onNext(2)
2018-11-11 22:49:23 INFO | onNext(3)
2018-11-11 22:49:23 INFO | onComplete()

As we can see changing the point of observation changed the output. Now the stream produced three events, but instead of cancel(), we see onComplete(). This is because we observe the output of using take() instead of what was requested by this method.

我们可以看到,改变观察点改变了输出。现在流产生了三个事件,但是我们看到的不是cancel(),而是onComplete()这是因为我们观察的是使用take()的输出,而不是这个方法所要求的。

6. Conclusion

6.结论

In this quick article, we saw how to log reactive streams using built-in log() method.

在这篇快速文章中,我们看到了如何使用内置的log()方法来记录反应流。

And as always, the source code for the above example can be found over on GitHub.

和往常一样,上述例子的源代码可以在GitHub上找到