GKE上Quarkus连接Cloud SQL时Vert.x原生传输异常求助
将Quarkus服务部署在GKE后,调用REST API抛出java.lang.IllegalArgumentException,错误提示为Vertx实例必须使用原生传输才能通过域套接字连接。连接其他数据库时服务运行正常,但连接Cloud SQL时失败。
相关配置
pom.xml配置
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.pe.example</groupId> <artifactId>generate-quarkus-app</artifactId> <version>1.0.0-SNAPSHOT</version> <properties> <compiler-plugin.version>3.12.1</compiler-plugin.version> <maven.compiler.release>21</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id> <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id> <quarkus.platform.version>3.10.0</quarkus.platform.version> <skipITs>true</skipITs> <surefire-plugin.version>3.2.5</surefire-plugin.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>${quarkus.platform.artifact-id}</artifactId> <version>${quarkus.platform.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-rest</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-arc</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-container-image-docker</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-junit5</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-qute</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-rest-jackson</artifactId> </dependency> <dependency> <groupId>net.glxn.qrgen</groupId> <artifactId>javase</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.newrelic.agent.java</groupId> <artifactId>newrelic-java</artifactId> <version>8.9.0</version> <scope>provided</scope> <type>zip</type> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-hibernate-reactive-panache</artifactId> </dependency> <!-- Hibernate Reactive uses the reactive-pg-client with PostgreSQL under the hood --> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-reactive-pg-client</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-hibernate-validator</artifactId> </dependency> <dependency> <groupId>com.google.cloud.sql</groupId> <artifactId>postgres-socket-factory</artifactId> <version>1.15.2</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-transport-native-epoll</artifactId> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-transport-native-unix-common</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>quarkus-maven-plugin</artifactId> <version>${quarkus.platform.version}</version> <extensions>true</extensions> <executions> <execution> <goals> <goal>build</goal> <goal>generate-code</goal> <goal>generate-code-tests</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${compiler-plugin.version}</version> <configuration> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <systemPropertyVariables> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> </configuration> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>${surefire-plugin.version}</version> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> <configuration> <systemPropertyVariables> <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> </configuration> </plugin> <!-- Unzip New Relic Java agent into target/ --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>unpack-newrelic</id> <phase>package</phase> <goals> <goal>unpack-dependencies</goal> </goals> <configuration> <includeGroupIds>com.newrelic.agent.java</includeGroupIds> <includeArtifactIds>newrelic-java</includeArtifactIds> <!-- you can optionally exclude files --> <!-- <excludes>**/newrelic.yml</excludes> --> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> <profiles> <profile> <id>native</id> <activation> <property> <name>native</name> </property> </activation> <properties> <skipITs>false</skipITs> <quarkus.package.type>native</quarkus.package.type> </properties> </profile> </profiles> </project>
应用配置文件(application.properties)
quarkus.datasource.db-kind=postgresql quarkus.datasource.username=xxxxxx quarkus.datasource.password=yyyyyy google.application-credentials=${GOOGLE_APPLICATION_CREDENTIALS} quarkus.datasource.reactive.url=postgresql://:5432/qr-database?host=/cloudsql/${DB_URL} quarkus.vertx.prefer-native-transport=true quarkus.reactive.implementation = smallrye
解决方案
针对该异常,可从以下几个方面排查修复:
对齐Netty原生传输依赖版本:移除pom.xml中手动引入的
netty-transport-native-epoll和netty-transport-native-unix-common,改用Quarkus提供的quarkus-netty依赖,它会自动管理适配的Netty版本,避免版本冲突导致原生传输组件失效。强制启用Vertx原生传输:在配置文件中添加
quarkus.vertx.native-transport=true,配合已有的quarkus.vertx.prefer-native-transport=true,确保Vertx实例强制使用原生传输层,而非回退到Java NIO。调整Cloud SQL连接URL格式:将Reactive PG Client的域套接字连接URL简化为
postgresql:///qr-database?host=/cloudsql/${DB_URL}(省略端口前的冒号),避免URL解析异常。移除冗余依赖:
postgres-socket-factory是JDBC连接组件,Quarkus Reactive PG Client无需此依赖,移除后可避免与原生传输组件产生冲突。原生镜像构建配置:若构建原生镜像,需确保Netty原生库被正确包含。在pom.xml的
nativeprofile中添加构建参数:<properties> <quarkus.native.additional-build-args>--enable-all-security-services,--include-library=netty_transport_native_epoll_x86_64</quarkus.native.additional-build-args> </properties>
内容的提问来源于stack exchange,提问作者user07

