为什么Spring不为关系数据库提供反应性(非阻塞)客户端? [英] Why does Spring not provide reactive (non-blocking) clients for relational databases?

查看:142
本文介绍了为什么Spring不为关系数据库提供反应性(非阻塞)客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Vert.x 工具包来创建响应式应用程序,并支持诸如

I've used Vert.x toolkit for creating reactive applications with support for relational DBs like MySQL and Postgres. I know Spring provides reactive support for some NoSQL DBs like Cassandra and Mongo but are they willing to provide the same for relational DBs?

推荐答案

Spring框架背后的想法是什么?

Spring Framework是一个用于提高开发人员生产力的库,Spring的投资组合项目(例如Spring Data,Spring Security,Spring Cloud)也是这样.

What's the idea behind the Spring Framework?

Spring Framework is a library to improve developer productivity, and so are Spring's portfolio projects such as Spring Data, Spring Security, Spring Cloud.

这些项目建立在现有的API之上,这些API通过JSR或JEP进行了标准化,或者建立在已被证明是有用且广泛使用的库之上. Spring团队不会为数据库或其他集成构建驱动程序,这取决于数据库/驱动程序供应商.

These projects build on top of existing APIs which are either standardized through a JSR or a JEP or on top of libraries that have proved to be useful and widely used. The Spring team does not build drivers for databases or other integrations, that's up to the database/driver vendors.

Spring WebFlux是典型的Spring模块的一个很好的例子.它基于现有的非阻塞服务器(通过netty,Undertow和Jetty的Project Reactor)之上. WebFlux为利用Spring组件的无阻塞,反应式应用程序提供了运行时容器,以协助开发和运行此类应用程序.

Spring WebFlux is an good example for a typical Spring module. It builds on top of existing non-blocking servers (Project Reactor via netty, Undertow, and Jetty). WebFlux provides a runtime container for non-blocking, reactive applications leveraging Spring components to assist in developing and running such applications.

Vert.x是集成环境的一个很好的例子,它提供了自己的底层实现. Vert.x经过了高度优化,并且这样的生态系统需要优化的集成. Vert.x提出了自己的用于各种数据库的实现,并提供了在Vert.x上下文中运行良好的API,但这些API并非JDBC.

Vert.x is an excellent example of an integrated environment that provides its own low-level implementations. Vert.x is heavily optimized and such an eco-system requires optimized integrations. Vert.x came up with own implementations for various databases and provides APIs that work well in a Vert.x context but these APIs are not JDBC.

正如 M-Razavi 所述,Java使用JDBC来与关系数据库集成,并且JDBC是一个障碍本质-减轻JDBC的阻塞本质没有明智的选择.将JDBC调用卸载到Executor(通常是Thread池)的用途受到限制,因为该池最终会因请求而饱和. TL; DR,没有可用的API,我们可以提供反应性的关系数据库集成.

As M-Razavi already mentioned, Java uses JDBC to integrate with relational databases and JDBC is of a blocking nature – there's nothing sensible one could do about to mitigate the blocking nature of JDBC. Offloading JDBC calls to an Executor (typically Thread pool) is limited in its usefulness as the pool eventually saturates with requests). TL;DR, there's no API available on top of which we could provide a reactive relational database integration.

M-Razavi 已经提到 Postgres ADBA驱动程序,该驱动程序可用于首次实验.

M-Razavi already mentioned ADBA that is an initiative from Oracle to provide a standardized API for asynchronous database access in Java using futures. Everything in ADBA is still work in progress and the team behind ADBA is happy to get feedback. A bunch of Postgres folks is working on a Postgres ADBA driver that can be used for first experiments.

但是,ADBA是未来的目标,我希望我们不会看到ADBA与Java 12一起发布.

However, ADBA is a future goal and I expect that we don't see ADBA released with Java 12.

有几个独立的驱动程序,例如 Reactiverse的react-pg-client .这些驱动程序带有特定于供应商的API,并不真正适合于Spring中的更广泛的集成.我们将需要提供更多的层来公开通用的API,并且不能将新的驱动程序仅插入到您的应用程序中,因此它可以即开即用地运行.拥有标准API可以实现可插入性,因此拥有标准API具有巨大的价值.

There are a couple of independent drivers such as Reactiverse's reactive-pg-client. These drivers come with a vendor-specific API and aren't really suited for a broader integration in Spring. We would need to provide additional layers to expose a common API, and new drivers couldn't be just plugged into your application so it works-out-of-the-box™. Having a standard API allows pluggability, so there's huge value in having a standard API.

由于缺乏标准的API和驱动程序不可用, Pivotal 的团队开始研究反应式关系API这将是反应式编程的理想选择.他们想出了 R2DBC ,它表示反应式关系数据库连接".到目前为止,R2DBC是一个孵化器项目,旨在评估可行性并开始讨论驱动程序供应商是否对支持反应性/非阻塞/异步驱动器完全有兴趣.

Lacking a standard API and the non-availability of drivers, a team at Pivotal started to investigate on a reactive relational API that would be an ideal fit for reactive programming purposes. They came up with R2DBC which stands for Reactive Relational Database Connectivity. As of now, R2DBC is an incubator project to evaluate the feasibility and to start discussions whether driver vendors are interested at all in supporting reactive/non-blocking/asynchronous drivers.

到目前为止,共有三种驱动程序实现:

As of now, there are three driver implementations:

  • PostgreSQL
  • H2
  • Microsoft SQL Server

R2DBC带有API规范(r2dbc-spi)和客户端(r2dbc-client),使SPI可用于应用程序.我们开始探索 Spring Data R2DBC 集成,该集成通过数据库客户端提供反应性API.并通过支持反应性存储库.

R2DBC comes with an API specification (r2dbc-spi) and a client (r2dbc-client) that makes the SPI usable for applications. We started exploring on a Spring Data R2DBC integration that provides reactive APIs through a database client and by supporting reactive repositories.

R2DBC及其生态系统还很年轻,要求进行实验和反馈以收集用例,并查看反应性关系数据库集成是否有意义.

R2DBC and its eco-system are still young and ask for experiments and feedback to collect use cases and to see whether a reactive relational database integration would make sense.

现在,您可以通过Spring Data使用R2DBC,以下代码段显示了DatabaseClient的用法:

Right now, you can consume R2DBC through Spring Data and the following snippet shows DatabaseClient usage:

PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(…);

DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);

Mono<Integer> count = databaseClient.execute()
                .sql("INSERT INTO legoset (id, name, manual) VALUES($1, $2, $3)")
                .bind("$1", 42055)
                .bind("$2", "Description")
                .bindNull("$3", Integer.class)
                .fetch()
                .rowsUpdated();

Flux<Map<String, Object>> rows = databaseClient.execute()
                .sql("SELECT id, name, manual FROM legoset")
                .fetch()
                .all();

这篇关于为什么Spring不为关系数据库提供反应性(非阻塞)客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆