Java编程方法论:响应式Spring Reactor 3设计与实现
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

推荐序二

Dear Reader,

Welcome on your journey to building more efficient and resilient applications, welcome to Reactive Programming!

On this path, you will challenge yourself and you will be rewarded with a new mindset that will help you create your next distributed Java services.Interacting with remote services is a common task performed by distributed systems and those interactions take time outside of the calling application control.Multiple reasons are behind this time fluctuation:network latency, the nature of the task run or even a system interruption. At the end of the day, if the calling code needs a result back it will be negatively impacted by that same time because it needs to actively wait for it.

Enter Reactive Programming which gives you the great power of bending space-time to your will!Or sort of... It gives you the ability to remove the negative impact of waiting for anything, so your Thread or CPU is not pinned and can perform other tasks.

Since you can't control how long a remote call will last, you will need to "schedule" the activities your application needs to perform "when" those remote services produce a result.For instance, if your application talks to a remote REST endpoint, it will eventually produce an HTTP response.

"Non Blocking" applications will provide a listener reacting only"when"an underlying network event signals the response back to you.What will do the calling Thread then? It will be able to return to the server worker pool or preparing a next HTTP call, thus increasing service concurrent capacity. It's a scalable design some runtimes have adopted for years, you can implement it with a single thread like Node.JS!

"Non-Blocking" is foundational to Reactive Programming but what does "Reactive" mean anyway? It's maybe not the first time you read the word "Reactive", it has various meanings and it's even mistaken for the popular UI library ReactJS. In this book, when "Reactive" is mentioned, it will always refer to the "reactive-streams" specification which is a clear documented definition adopted by major libraries. "Reactive Streams" defines how an arbitrary "producer" can interact with one or more "consumers" in a Reactive way. Once implemented, the contract offers two key qualities designed to help developers build 100% non-blocking applications: Error Isolation and Flow Control. The former quality contributes to a greater resiliency and availability, producers errors must not interrupt the application, and instead they will forward errors to registered listeners. The latter quality means that producers can't produce more data than consumers are able to consume at any given time.This rule is also known as "backpressure" and it is designed to avoid "unbounded buffers" which has resources consequences on the application host. It helps that one of the major adopters of "Reactive Streams" is the Spring Reactive stack itself including Spring WebFlux and Project Reactor. They do a great job at hiding away a lot of the technical details from the specification I've briefly mentioned in this intro. They provide rich APIs to build reactive flows and help you focus on the "what" and not the "how".

In fact, there are various elements in these libraries designed to ease your first experience with Reactive Programming.First and foremost, Spring can become as much reactive as you need:You can start developing on top of a well-known programming model, Spring MVC, running on Tomcat and you can selectively introduce the modern reactive "WebClient" coming with Spring WebFlux.You can also start returning Project Reactor reactive types Flux and Mono in your Spring MVC controllers the same way you can return CompletableFuture.Ultimately you can just swap your container and start running on top of Netty or Tomcat reactive bridge. Spring conventions such as annotations also matter and most Java developers have learned them for sometimes many years! For instance, @RestController works in both Spring MVC and WebFlux as one could expect. Learning Reactive Programming with Spring is intended to feel like you are learning at home, in a familiar setup.

In this book, you will work with the Spring Reactive stack. I highly recommend you pair this learning opportunity with a good use case that could benefit from going Reactive. For instance, an application depending on a remote service, or an application on the edge of your system serving as a port of entry for many concurrent users.

Learning a new mindset is never easy but it is highly rewarding.Many of the lessons Reactive programming offers apply to all modern software engineering. I think the greatest of these lessons is that it always pays off to be ready with a plan when a component outside your control does not work as you expect.

Good luck!

Stephane Maldini

Netflix高级软件工程师

Spring Reactor项目创始人

Reactor-Netty项目负责人

Spring Framework项目贡献者