使用 R2DBC 的动机是什么?

Nur*_*yev 2 spring-webflux spring-data-r2dbc r2dbc

我对反应式弹簧堆栈非常陌生,目前正在探索R2DBC.

Could you explain me what are the benefits of using R2dbcRepository over wrapping blocking JpaRepository into Mono/Flux?

Let me bring some example of that:

val producer: Mono<BookEntity> = booksReactiveRepository.findById(id)
Run Code Online (Sandbox Code Playgroud)

vs

val producer: Mono<BookEntity> = Mono.fromSupplier { booksBlockingRepository.findById(id) }
Run Code Online (Sandbox Code Playgroud)

Is there any conceptual difference on execution?

Mar*_*nyi 6

主要区别在于 JDBC/JPA 使用阻塞 I/O,这意味着每个请求都需要一个专用线程。在高度并发的系统中,这很容易导致扩展问题。

另一方面,R2DBC 使用非阻塞 I/O,这意味着它能够仅使用固定的少量线程处理请求,这使得扩展更容易且成本更低。

查看以下文章:https : //spring.io/blog/2018/12/07/reactive-programming-and-relational-databases

Java 使用 JDBC 作为与关系数据库集成的主要技术。JDBC 具有阻塞性质——没有任何明智的方法可以减轻 JDBC 的阻塞性质。如何使调用非阻塞的第一个想法是将 JDBC 调用卸载到 Executor(通常是线程池)。虽然这种方法在一定程度上有效,但它有几个缺点,忽略了反应式编程模型的好处。

线程池需要——毫不奇怪——线程来运行。响应式运行时通常使用与 CPU 内核数量匹配的有限数量的线程。额外的线程会引入开销并减少线程限制的影响。此外,JDBC 调用通常堆积在队列中,一旦线程被请求饱和,池将再次阻塞。因此,JDBC 现在不是一种选择。