简体   繁体   English

R2DBC 和 liquibase

[英]R2DBC and liquibase

So starting a new project and i want to use r2dbc and webflux, been looking into what support there is to handle database migration.所以开始一个新项目,我想使用 r2dbc 和 webflux,一直在研究有什么支持来处理数据库迁移。 The last answer i could find here was from july 2019 that liquibase does not support R2DBC and after googling, this seems to still be the case.我在这里能找到的最后一个答案是从 2019 年 7 月开始,liquibase 不支持 R2DBC,在谷歌搜索之后,情况似乎仍然如此。

The dream would be to use r2dbc-h2 while developing locally, and then use something like postgres during production.梦想是在本地开发时使用r2dbc-h2 ,然后在生产期间使用类似 postgres 的东西。 Liquibase would manage the table structure both locally and in production. Liquibase 将在本地和生产中管理表结构。

Been trying to google a bit about how such a setup would look like and there is very little information out there.一直在尝试用谷歌搜索一下这种设置的外观,但那里的信息很少。

I have been thinking about setting up the tables using the liquibase-maven-plugin , but i don't know if that will work with r2dbc-h2 .我一直在考虑使用liquibase-maven-plugin设置表,但我不知道这是否适用于r2dbc-h2

So several questions:所以几个问题:

  • How to setup so that liquibase uses a regular driver during migration, while the rest of the application uses the reactive driver?如何设置使 liquibase 在迁移期间使用常规驱动程序,而应用程序的 rest 使用反应式驱动程序?
  • if using the maven plugin can this be used with H2 or do i need postgres as a docker?如果使用 maven 插件可以与 H2 一起使用还是我需要 postgres 作为 docker?

This is a very black hole for me, does any have any information?这对我来说是一个非常黑洞,有任何信息吗?

I think there should be no problem using 2 drivers in application.我认为在应用程序中使用 2 个驱动程序应该没有问题。 As liquibase uses standard jdbc driver you can configure it to use that one for migrations and configure r2dbc to run the application.由于 liquibase 使用标准 jdbc 驱动程序,您可以将其配置为使用该驱动程序进行迁移并配置 r2dbc 以运行应用程序。 Maybe some tweeks needs to be done but I would start with something like:也许需要完成一些 tweeks 但我会从以下内容开始:

spring:
  liquibase:
    url: jdbc:postgresql://localhost:5432/mydb
    user: postgres
  r2dbc:
    url: r2dbc:postgresql://localhost:5432/mydb
    username: postgres

and include both libraries:并包括两个库:

io.r2dbc:r2dbc-postgresql
org.postgresql:postgresql

If there is error keep us posted.如果有错误,请让我们发布。

note: for testing you can use testcontainers or embedded postgresql also注意:对于测试,您也可以使用 testcontainers 或嵌入式 postgresql

Complementing @bilak's answer, you must add the following dependency to your pom.xml:补充@bilak 的答案,您必须将以下依赖项添加到您的 pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>

I was facing the same problem and I had added the properties user and URL to Liquibase configuration on my application.yml, and then Spring started to claim about one class not found, adding this dependency solved the issue我遇到了同样的问题,我已经在我的 application.yml 上将属性userURL添加到 Liquibase 配置中,然后 Spring 开始声称大约一个 class 解决了这个问题

As of Spring Boot 2.6.3 with Spring Framework 5.3.15, the following configuration works for R2DBC with Liquibase从 Spring 启动 2.6.3 和 Spring 框架 5.3.15 开始,以下配置适用于带有 Liquibase 的 R2DBC

build.gradle snippet build.gradle片段

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'

    //database
    implementation "io.r2dbc:r2dbc-postgresql"
    runtimeOnly 'org.postgresql:postgresql'

    //liquibase
    implementation "org.liquibase:liquibase-core"
    runtimeOnly 'org.springframework:spring-jdbc'

    testImplementation 'io.projectreactor:reactor-test'
}

application.yml应用程序.yml

spring:
  main:
    web-application-type: REACTIVE
  r2dbc:
    url: r2dbc:postgresql://localhost/mydb
    username: postgres
  liquibase:
    url: jdbc:postgresql://localhost/mydb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM