我的 Spring Boot 应用程序使用 Flyway 进行数据库迁移并连接到 GCP Cloud 中的 Postgres。
它在 JRE 11 上工作,但在我们升级到 JRE 17 后开始崩溃。
日志显示服务启动成功并启动 Hikari 连接,但随后在 Flyway 尝试获取连接时中断。
所以它是这样开始的:
20:57:19.727 [main] INFO com.zaxxer.hikari.HikariDataSource [] - HikariPool-1 - Starting...
20:57:19.920 [main] INFO c.g.cloud.sql.core.CoreSocketFactory [] - First Cloud SQL connection, generating RSA key pair.
20:57:27.227 [main] INFO com.zaxxer.hikari.HikariDataSource [] - HikariPool-1 - Start completed.
20:57:27.420 [main] INFO o.f.c.i.d.base.BaseDatabaseType [] - Database: jdbc:postgresql://google/dbname_dev (PostgreSQL 13.7)
20:57:27.627 [main] INFO o.f.core.internal.command.DbValidate [] - Successfully validated 4 migrations (execution time 00:00.106s)
显示它已建立连接,验证了迁移,但随后显示错误:
WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext [] - Exception
encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'flywayInitializer' defined in class path resource
[org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]:
Unable to obtain connection from database: HikariPool-1 - Connection is not available,
request timed out after 30000ms.
HikariPool-1 - Connection is not available, request timed out after 30000ms.
最后:
20:57:57.653 [main] INFO com.zaxxer.hikari.HikariDataSource [] - HikariPool-1 - Shutdown initiated...
20:57:57.656 [main] INFO com.zaxxer.hikari.HikariDataSource [] - HikariPool-1 - Shutdown completed.
问题是由 Flyway 库引起的,因为一旦我禁用
spring.flyway.enabled=false
应用程序就会按预期工作并成功地与数据库一起工作。
这里是 POM 片段:
JRE 11 版本:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>9.7.0</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.4.4</version>
</dependency>
JRE 17 版本:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>9.16.1</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>9.16.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.11.0</version>
</dependency>
但我试着玩这些版本,我也试着保留旧版本,但它还是坏了。
谢谢