Non-blocking Spring Boot with Kotlin Coroutines
Posted Kotlin 开发者社区
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Non-blocking Spring Boot with Kotlin Coroutines相关的知识,希望对你有一定的参考价值。
Non-blocking Spring Boot with Kotlin Coroutines
1. Overview
Kotlin Coroutines can often add readability to reactive, callback-heavy code.
In this tutorial, we’ll find out how to leverage these coroutines for building non-blocking Spring Bootapplications. We’ll also compare the reactive and coroutine approaches.
2. Coroutine Motivation
Nowadays, it is common for systems to serve thousands or even millions of requests. Consequently, the development world is moving towards non-blocking computation and request handling. Utilizing system resource efficiently, by offloading I/O operations from core threads makes it possible to handle many more requests compared to the traditional thread per-request approach.
Asynchronous processing is not a trivial task and can be error-prone. Fortunately, we have tools for tackling this complexity like Java CompletableFutures or reactive libraries like RxJava. Indeed, the Spring framework already supports reactive approaches with the Reactor and WebFlux frameworks.
Asynchronous code can be hard to read, but the Kotlin language provides the concept of Coroutines to allow writing concurrent and asynchronous code in a sequential style.
Coroutines are very flexible, so we have more control over the execution of tasks via Jobs and Scopes. Besides that, Kotlin coroutines work perfectly side by side with existing Java non-blocking frameworks.
Spring will support Kotlin Coroutines from version 5.2.
3. Project Setup
Let’s start by adding in the dependencies we’ll need.
As most of the dependencies used in this tutorial don’t have stable releases yet, we’ll need to include the snapshots and milestones repositories:
1
以上是关于Non-blocking Spring Boot with Kotlin Coroutines的主要内容,如果未能解决你的问题,请参考以下文章 blocking cache和non-blocking cache Java NIO (10) Non-blocking Server blocking和non-blocking的区别 synchronous IO和asynchronous IO的区别 为啥 Spring Boot 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web? |