Spring集成R2DBC启动报错:无法实例化ConnectionPool的解决方案
解决Spring初始化时R2DBC连接池创建失败的问题
Spring应用初始化时出现如下报错:
r2dbc + Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.r2dbc.pool.ConnectionPool]: Factory method 'connectionFactory' threw exception with message: Unable to create a ConnectionFactory for 'ConnectionFactoryOptions{options={database=company, host=127.0.0.1, driver=postgresql, password=REDACTED, port=5433, user=postgres}}'. Available drivers: [ pool ]
我整理了一套可行的解决方案(适用于使用FlywayDB做数据库脚本迁移的场景),分享给大家:
一、Maven依赖配置
确保包含以下依赖,需同时引入PostgreSQL的R2DBC驱动(供R2DBC连接池使用)和JDBC驱动(供Flyway做迁移使用):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-r2dbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>9.16.1</version> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>r2dbc-postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>
二、application.yaml配置
注意区分R2DBC与Flyway的配置规则:Flyway需使用JDBC协议的URL,R2DBC则使用对应r2dbc:开头的协议URL:
DB_IP: 127.0.0.1 DB_PORT: 5432 DB_DATABASE_NAME: db DB_SCHEMA: public database: url: postgresql://localhost:5432/db server: port: 8090 error: include-message: always spring: r2dbc: username: postgres password: postgres url: r2dbc:postgresql://127.0.0.1:5432/db flyway: baseline-description: true baseline-on-migrate: true create-schemas: true enabled: true default-schema: ${DB_SCHEMA} locations: classpath:db/migration password: ${spring.r2dbc.password} schemas: public url: jdbc:${database.url} user: ${spring.r2dbc.username} validate-on-migrate: true
内容的提问来源于stack exchange,提问作者skyho
相关产品推荐
相关产品推荐

