Spring Boot 2 忽略了 application.yml 中设置的 HikariCP 属性
Spring Boot 2 is ignoring HikariCP properties set in application.yml
我有一个 spring boot 2 应用程序连接到 Mariadb 数据库。此应用程序在 Cloud Foundry 中运行。它从 VCAP_* 环境变量中获取数据库连接属性。
应用程序运行正常,可以连接到数据库。但是,我发现应用程序没有使用 application.yml.
中指定的 hikari 配置
你能指出这里有什么问题吗?
build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.6.2'
compile group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector', version: '2.0.7.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector', version: '2.0.7.RELEASE'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'com.h2database:h2:1.4.200'
}
application.yml
datasource:
hikari:
pool-name: mypool
auto-commit: true
connection-timeout: 5000
minimum-idle: 8
maximum-pool-size: 8
idle-timeout: 600000
max-lifetime: 3600000
CloudConfig.java
@Configuration
public class CloudConfig {
@Bean
public CloudFactory cloudFactory() {
return new CloudFactory();
}
private DataSource createDataSource(CloudFactory cloudFactory, String serviceId) {
return cloudFactory.getCloud().getServiceConnector(serviceId, DataSource.class, null);
}
@Primary
@Bean(name = "myDataSource")
public DataSource becscmDataSource(CloudFactory cloudFactory) {
return createDataSource(cloudFactory, "my-maria-rds");
}
}
您正在使用 Spring 云连接器库创建数据库连接。
compile group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector', version: '2.0.7.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector', version: '2.0.7.RELEASE'
这个图书馆在 maintenance mode, and its use is discouraged. One of the reasons for discouraging its use is that it does not work well with Spring Boot configuration. When Connectors APIs like cloudFactory.getCloud().getServiceConnector()
are used to create the service connection bean, Spring Boot auto-configuration backs off and properties like spring.datasource.*
are ignored. If there is a reason why you must continue to use Connectors, then you will need to use the Connectors API to configuring pooling options as shown in the documentation.
更好的选择是删除对连接器的依赖并改用 Java CFEnv。
在听从 Scott 的建议后。我能够使它按如下方式工作。
build.yml
implementation 'io.pivotal.cfenv:java-cfenv-boot:2.3.0'
application.yml
保持不变
CloudConfig.java
@Configuration
public class CloudConfig {
@Primary
@Bean(name = "myDataSource")
@ConfigurationProperties("spring.datasource.hikari") // added this
public DataSource becscmDataSource() {
CfJdbcEnv cfJdbcEnv = new CfJdbcEnv();
CfJdbcService myJdbc = cfJdbcEnv.findJdbcServiceByName("my-maria-rds");
DataSourceBuilder builder = DataSourceBuilder.create();
builder.url(myJdbc.getJdbcUrl());
builder.username(myJdbc.getUsername());
builder.password(myJdbc.getPassword());
builder.driverClassName("org.mariadb.jdbc.Driver");
return builder.build();
}
}
我有一个 spring boot 2 应用程序连接到 Mariadb 数据库。此应用程序在 Cloud Foundry 中运行。它从 VCAP_* 环境变量中获取数据库连接属性。
应用程序运行正常,可以连接到数据库。但是,我发现应用程序没有使用 application.yml.
中指定的 hikari 配置你能指出这里有什么问题吗?
build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.6.2'
compile group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector', version: '2.0.7.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector', version: '2.0.7.RELEASE'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'com.h2database:h2:1.4.200'
}
application.yml
datasource:
hikari:
pool-name: mypool
auto-commit: true
connection-timeout: 5000
minimum-idle: 8
maximum-pool-size: 8
idle-timeout: 600000
max-lifetime: 3600000
CloudConfig.java
@Configuration
public class CloudConfig {
@Bean
public CloudFactory cloudFactory() {
return new CloudFactory();
}
private DataSource createDataSource(CloudFactory cloudFactory, String serviceId) {
return cloudFactory.getCloud().getServiceConnector(serviceId, DataSource.class, null);
}
@Primary
@Bean(name = "myDataSource")
public DataSource becscmDataSource(CloudFactory cloudFactory) {
return createDataSource(cloudFactory, "my-maria-rds");
}
}
您正在使用 Spring 云连接器库创建数据库连接。
compile group: 'org.springframework.cloud', name: 'spring-cloud-cloudfoundry-connector', version: '2.0.7.RELEASE'
compile group: 'org.springframework.cloud', name: 'spring-cloud-spring-service-connector', version: '2.0.7.RELEASE'
这个图书馆在 maintenance mode, and its use is discouraged. One of the reasons for discouraging its use is that it does not work well with Spring Boot configuration. When Connectors APIs like cloudFactory.getCloud().getServiceConnector()
are used to create the service connection bean, Spring Boot auto-configuration backs off and properties like spring.datasource.*
are ignored. If there is a reason why you must continue to use Connectors, then you will need to use the Connectors API to configuring pooling options as shown in the documentation.
更好的选择是删除对连接器的依赖并改用 Java CFEnv。
在听从 Scott 的建议后。我能够使它按如下方式工作。
build.yml
implementation 'io.pivotal.cfenv:java-cfenv-boot:2.3.0'
application.yml
保持不变
CloudConfig.java
@Configuration
public class CloudConfig {
@Primary
@Bean(name = "myDataSource")
@ConfigurationProperties("spring.datasource.hikari") // added this
public DataSource becscmDataSource() {
CfJdbcEnv cfJdbcEnv = new CfJdbcEnv();
CfJdbcService myJdbc = cfJdbcEnv.findJdbcServiceByName("my-maria-rds");
DataSourceBuilder builder = DataSourceBuilder.create();
builder.url(myJdbc.getJdbcUrl());
builder.username(myJdbc.getUsername());
builder.password(myJdbc.getPassword());
builder.driverClassName("org.mariadb.jdbc.Driver");
return builder.build();
}
}