5

我在尝试将 Quarkus Flyway 扩展与 Quarkus Reactive Hibernate & RESTEasy 一起使用时遇到问题。启动我的应用程序时,我收到以下错误:

[io.qu.ru.Application] (Quarkus Main Thread) Failed to start application (with profile dev): java.lang.IllegalStateException: Booting an Hibernate Reactive serviceregistry on a non-reactive RecordedState!
    at io.quarkus.hibernate.reactive.runtime.boot.registry.PreconfiguredReactiveServiceRegistryBuilder.checkIsReactive(PreconfiguredReactiveServiceRegistryBuilder.java:76)
    at io.quarkus.hibernate.reactive.runtime.boot.registry.PreconfiguredReactiveServiceRegistryBuilder.<init>(PreconfiguredReactiveServiceRegistryBuilder.java:66)
    at io.quarkus.hibernate.reactive.runtime.FastBootHibernateReactivePersistenceProvider.rewireMetadataAndExtractServiceRegistry(FastBootHibernateReactivePersistenceProvider.java:177)
    at io.quarkus.hibernate.reactive.runtime.FastBootHibernateReactivePersistenceProvider.getEntityManagerFactoryBuilderOrNull(FastBootHibernateReactivePersistenceProvider.java:156)
    at io.quarkus.hibernate.reactive.runtime.FastBootHibernateReactivePersistenceProvider.createEntityManagerFactory(FastBootHibernateReactivePersistenceProvider.java:82)

以下是相关的 Quarkus 配置:

quarkus:
  datasource:
    db-kind: "postgresql"
    username: "sarah"
    password: "connor"
    jdbc:
      ~: true
      url: "jdbc:postgresql://localhost:5432/mybase"
    reactive:
      ~: true
      url: "postgresql://localhost:5432/mybase"

以及相关的依赖:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-reactive-panache</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-reactive-pg-client</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-flyway</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>

禁用 JDBC 配置可~: false避免异常,但应用程序不会在启动时启动 Flyway 迁移。在这种情况下,我看到以下消息:

[io.qu.ag.de.AgroalProcessor] (build-39) The Agroal dependency is present but no JDBC datasources have been defined.

我在一些 Quarkus 问题上发现,确实不可能同时运行反应式和阻塞式数据库连接,但是有没有办法让 Flyway 与反应式Quarkus 应用程序一起工作?

4

1 回答 1

5

目前,它们确实似乎并不同时支持阻塞 JDBC 和反应式 sql 客户端。一种解决方法是为 Quarkus 运行时禁用 JDBC,并编写自己的包装器来执行 Flyway 迁移。

以下解决方法基于其相应的GitHub 问题

Flyway 包装器在应用程序启动时运行:

@ApplicationScoped
public class RunFlyway {

    @ConfigProperty(name = "myapp.flyway.migrate")
    boolean runMigration;

    @ConfigProperty(name = "quarkus.datasource.reactive.url")
    String datasourceUrl;
    @ConfigProperty(name = "quarkus.datasource.username")
    String datasourceUsername;
    @ConfigProperty(name = "quarkus.datasource.password")
    String datasourcePassword;

    public void runFlywayMigration(@Observes StartupEvent event) {
        if (runMigration) {
            Flyway flyway = Flyway.configure().dataSource("jdbc:" + datasourceUrl, datasourceUsername, datasourcePassword).load();
            flyway.migrate();
        }
    }
}

pom.xml:

<!-- DB -->
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-reactive-pg-client</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-reactive</artifactId>
</dependency>

<!-- Flyway -->
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-flyway</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>

应用程序.yml:

myapp:
  flyway:
    migrate: true
quarkus:
  datasource:
    db-kind: postgresql
    username: myuser
    password: mypassword
    jdbc: false
    reactive:
      url: postgresql://localhost:5432/mydb
于 2021-03-07T14:16:04.933 回答