我必须重新实现一些后端服务,主要要求之一是使整个流程具有反应性。以前,服务使用 PostgreSQL 休眠,因此上述连接是由框架提供的。
由于我必须保留原始数据库并更改服务实现,因此我必须使用 r2dbc-postgresql。我找不到关于这个主题的任何资源,但我最好的猜测是做一些类似于我会用 JDBC 做的事情,并在我的实体之间引入一些新的连接表。
postgresql project-reactor spring-webflux spring-data-r2dbc r2dbc
看起来 Spring Data r2dbc 不支持复合主键。这是一个已知问题吗?
我没有看到对@Embedded 的支持。
在我的 spring-boot 2.3 应用程序中,我使用了一个简单的数据方法DatabaseClient:
fun getCurrentTime(): Mono<LocalDateTime> =
databaseClient
.execute("SELECT NOW()")
.asType<LocalDateTime>()
.fetch()
.first()
}
Run Code Online (Sandbox Code Playgroud)
使用 spring-boot 2.4(和 spring 5.3 和 spring-data-r2dbc 1.2),org.springframework.data.r2dbc.core.DatabaseClient不推荐使用 spring-data-r2dbc,取而代之的org.springframework.r2dbc.core.DatabaseClient是 spring-r2dbc - 它具有不同的 API。
调整它非常简单 - 除了 kotlin 扩展asType,它不是新的 DatabaseClientExtensions 的一部分。
fun getCurrentTime(): Mono<LocalDateTime> =
databaseClient
.sql("SELECT NOW()")
.map { row: Row ->
row.get(0, LocalDateTime::class.java)!!
}
.one()
Run Code Online (Sandbox Code Playgroud)
这些扩展是其他地方还是我如何使用具体化类型参数进行转换?
在java反应器中,r2dbc。我有两个表 A、B。我也为它们定义了存储库。如何获取由 A 连接 B 组成的数据?
我只提出了以下方法:从 A 调用 databaseClient.select,因此在循环中从 B 调用 select。
但我想要更有效和反应性的方式。怎么做?
在Spring Data R2DBC 中,我可以使用
logging.level.org.springframework.data.r2dbc=DEBUG
Run Code Online (Sandbox Code Playgroud)
在application.properties.
但是,这不会记录绑定为查询参数的实际值。
如何在 Spring Data R2DBC 中记录查询参数的实际值?
我正在尝试利用 H2 进行测试并在 中有以下配置src/test/resources/application.yaml:
spring:
r2dbc:
url: r2dbc:h2:file://testdb
Run Code Online (Sandbox Code Playgroud)
我有一个带有注释的空测试,@SpringBootTest但是当我运行它时,出现以下错误:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.r2dbc.pool.ConnectionPool]: Factory method 'connectionFactory' threw exception; nested exception is java.lang.IllegalStateException: Unable to create a ConnectionFactory for 'ConnectionFactoryOptions{options={driver=h2, protocol=file, host=testdb}}'. Available drivers: [ pool, postgresql, h2 ]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
... 131 common frames omitted
Caused by: java.lang.IllegalStateException: Unable to create a ConnectionFactory for 'ConnectionFactoryOptions{options={driver=h2, protocol=file, host=testdb}}'. Available drivers: [ pool, postgresql, h2 ]
at io.r2dbc.spi.ConnectionFactories.get(ConnectionFactories.java:145)
at org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryBuilder.build(ConnectionFactoryBuilder.java:125)
at org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations.createConnectionFactory(ConnectionFactoryConfigurations.java:56)
at …Run Code Online (Sandbox Code Playgroud) 在 Spring data JPA 中,有一个@Sql注释对于设置持久层的集成测试非常方便。它可以在每次测试之前推出测试数据并在测试之后执行清理。
但是,我在模块中找不到它spring-data-r2dbc。有没有类似的东西可以轻松处理这个任务spring-data-r2dbc?
我有一个简单的问题,是否可以使用 Spring Boot Data r2dbc for MySQL 或其他数据库自动生成表?在 JPA 中,我添加了 spring.jpa.hibernate.ddl-auto=update 并创建了表
这是我的pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.43.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud) 我正在开发一个多租户反应式应用程序,使用 Spring-Webflux + Spring-data-r2dbc 和 r2dbc 驱动程序连接到 Postgresql 数据库。多租户部分是基于模式的:每个租户一个模式。因此,根据上下文(例如用户登录),请求将命中数据库的特定架构。
我正在努力研究如何在 r2dbc 中实现这一点。理想情况下,这将是 Hibernate 使用MultiTenantConnectionProvider 的方式(参见示例 16.3)。
到目前为止我发现了什么以及我做了什么:
我查看了PostgresqlConnectionFactory。有趣的是,有prepareConnection一个电话setSchema(connection):
private Mono<Void> setSchema(PostgresqlConnection connection) {
if (this.configuration.getSchema() == null) {
return Mono.empty();
}
return connection.createStatement(String.format("SET SCHEMA '%s'", this.configuration.getSchema()))
.execute()
.then();
}
Run Code Online (Sandbox Code Playgroud)我可能需要找到一种方法来覆盖它,以便从上下文而不是配置中动态获取模式?
否则,我可以尝试将请求中的架构指定为表前缀:
String s = "tenant-1";
databaseClient.execute("SELECT * FROM \"" + s + "\".\"city\"")
.as(City.class)
.fetch()
.all()
Run Code Online (Sandbox Code Playgroud)但我不能再使用 SpringData,或者我需要重写每个请求以将租户作为参数传递。
任何提示/帮助表示赞赏:)
我有一个应用程序,它使用 Spring 数据 JPA 和 hibernate envers 进行数据库审计。由于 R2DBC 尚不支持审计,是否可以在单个应用程序中结合使用两者?
如果是,Plan 将使用 Spring Data JPA 进行插入、更新和删除操作,以便所有 DB 审计都由 hibernate envers 处理。并使用 R2DBC 进行反应式非阻塞 API 来读取数据。
如果没有,是否有关于如何同时实现反应式 API 和审计的建议?
r2dbc ×5
spring-boot ×4
java ×2
spring ×2
spring-data ×2
h2 ×1
kotlin ×1
multi-tenant ×1
mysql ×1
postgresql ×1
reactive ×1