Quarkus中Hibernate Reactive Panache启动报错:Vertx需开启preferNativeTransport
问题描述
在同一Quarkus项目中同时使用Hibernate Reactive Panache和普通Hibernate Panache,启动时触发错误:The Vertx instance must be created with the preferNativeTransport option set to true,即使已配置quarkus.vertx.prefer-native-transport=true,错误仍未消失。
错误日志
2024-06-06 10:30:26,888 INFO [org.hib.rea.pro.imp.ReactiveIntegrator] (JPA Startup Thread) HR000001: Hibernate Reactive 2024-06-06 10:30:27,411 WARN [org.hib.rea.pro.ser.ReactiveGenerationTarget] (vert.x-eventloop-thread-0) HR000021: DDL command failed [java.lang.IllegalArgumentException: The Vertx instance must be created with the preferNativeTransport option set to true to create domain sockets] 2024-06-06 10:30:27,427 WARN [org.hib.rea.pro.ser.ReactiveGenerationTarget] (vert.x-eventloop-thread-0) HR000021: DDL command failed [java.lang.IllegalArgumentException: The Vertx instance must be created with the preferNativeTransport option set to true to create domain sockets] 2024-06-06 10:30:27,473 INFO [io.qua.grp.run.GrpcServerRecorder] (main) Starting new Quarkus gRPC server (using Vert.x transport)... 2024-06-06 10:30:27,531 INFO [io.quarkus] (main) quarkus-demo 1.0.0 on JVM (powered by Quarkus 3.10.0) started in 1.789s. Listening on: http://0.0.0.0:8081 2024-06-06 10:30:27,531 INFO [io.quarkus] (main) Profile prod activated. 2024-06-06 10:30:27,531 INFO [io.quarkus] (main) Installed features: [agroal, cdi, grpc-server, hibernate-orm, hibernate-orm-panache, hibernate-reactive, hibernate-reactive-panache, jdbc-mysql, narayana-jta, reactive-mysql-client, rest, smallrye-context-propagation, vertx]
当前配置
application.properties
quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/test quarkus.datasource.username=root quarkus.datasource.password=password quarkus.datasource.db-kind=mysql quarkus.hibernate-orm.database.generation=drop-and-create quarkus.hibernate-orm.sql-load-script=import.sql quarkus.hibernate-orm.log.sql=false #quarkus.datasource.reactive.url=mysql:///test?host=/var/run/mysqld/mysqld.sock quarkus.datasource.reactive.url = vertx-reactive:mysql://localhost:3306/test?host=/var/run/mysqld/mysqld.sock quarkus.vertx.prefer-native-transport=true quarkus.grpc.server.use-separate-server=false quarkus.http.port=8081
build.gradle依赖
dependencies { implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}") implementation 'io.quarkus:quarkus-grpc' implementation 'io.quarkus:quarkus-hibernate-orm' implementation 'io.quarkus:quarkus-hibernate-reactive-panache' implementation 'io.quarkus:quarkus-hibernate-orm-panache' implementation 'io.quarkus:quarkus-hibernate-reactive' implementation 'io.quarkus:quarkus-container-image-docker' implementation 'io.quarkus:quarkus-jdbc-mysql' implementation 'io.quarkus:quarkus-reactive-mysql-client' implementation 'io.quarkus:quarkus-arc' implementation 'io.quarkus:quarkus-rest' testImplementation 'io.quarkus:quarkus-junit5' }
解决方案
1. 修正Reactive数据源URL
你当前的Reactive数据源URL同时混用了TCP连接和Unix域套接字参数,导致Vert.x需要Native Transport但初始化逻辑混乱。二选一修改:
- 若使用Unix域套接字:
quarkus.datasource.reactive.url=vertx-reactive:mysql:///test?host=/var/run/mysqld/mysqld.sock - 若使用普通TCP连接:
quarkus.datasource.reactive.url=vertx-reactive:mysql://localhost:3306/test
2. 添加Native Transport依赖
Quarkus的Vert.x Native Transport需要对应的底层库支持,在build.gradle中补充依赖:
implementation 'io.vertx:vertx-native-transport'
该依赖会自动适配当前操作系统的本地库,无需手动下载。
3. 调整Hibernate ORM初始化时机
同时使用JDBC和Reactive数据源时,JDBC的DDL生成可能早于Vert.x Native Transport初始化完成。可以暂时关闭Hibernate自动DDL生成,改用Flyway/Liquibase手动管理Schema:
quarkus.hibernate-orm.database.generation=none
4. 验证Native Transport生效
启动项目后,查看日志是否出现对应标识:
- Linux环境会显示
Epoll event loop group - macOS环境会显示
KQueue event loop group - Windows环境会显示
IOCP event loop group
出现以上内容则说明Vert.x已启用Native Transport。
内容的提问来源于stack exchange,提问作者Sujay DSa
相关产品推荐
相关产品推荐

