我是 stackoverflow 的新手,但在这里阅读了大量的帖子,现在卡住了。我的 application.properties 被读取,但配置 hikaricp 的部分被忽略/没有效果。
我阅读了https://www.javadevjournal.com/spring-boot/spring-boot-hikari/并按照那里的步骤进行操作,仍然成功。
pom.xml
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.4.10.Final</version>
<exclusions>
<exclusion>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
<version>5.4.10.Final</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
<version>2.2.2.RELEASE</version>
</dependency>
应用程序.属性
spring.cache.jcache.config=classpath:ehcache.xml
spring.datasource.jdbc-url=jdbc:postgresql://VOC-APP202-db:5432/voice-app
spring.datasource.username=vocapp202
spring.datasource.password=******
srping.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.connectionTimeout=1000
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.maxLifetime=60000
spring.datasource.hikari.connectionTestQuery=SELECT * FROM table where id=1
spring.datasource.hikari.minimumIdle=1
spring.datasource.hikari.maximumPoolSize=5
spring.datasource.hikari.poolName=voiceapp-db-pool
spring.datasource.hikari.autoCommit=false
BlacklistApplication.class:
package de.mycompany.voice.blacklist_ng;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
@EnableCaching
@Configuration
public class BlacklistngApplication {
public static void main(String[] args) {
SpringApplication.run(BlacklistngApplication.class, args);
}
}
配置类:
@Configuration
@ConfigurationProperties("spring.datasource")
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = {"de.firsttelecom.voice.blacklist_ng.repository.vocapp202"}
)
public class VocApp202DbConfig extends HikariConfig {
@Primary
@Bean(name = "dataSource")
public DataSource dataSource() {
return new HikariDataSource(this);
}
我缺少什么?
spring.datasource.hikari.*
(以及 spring.datasource.url
)属性才有效。为了实现它,你需要删除这个bean:
@Primary
@Bean(name = "dataSource")
public DataSource dataSource() {
return new HikariDataSource(this);
}
Spring Boot 将使用所有这些属性自动为您创建它。您可以查看
DataSourceAutoConfiguration.class
了解更多信息。
如果由于某种原因,你无法删除这个bean(例如,你有另一堆数据源,并且你需要创建手动数据源才能将其标记为
@Primary
),你可以使用“raw”属性来配置光。因此,您应该通过删除 dataSource()
部分来修改属性,而不是删除 hikari.
方法:
...
spring.datasource.jdbc-url=jdbc:postgresql://VOC-APP202-db:5432/voice-app
...
spring.datasource.connectionTimeout=1000
spring.datasource.idleTimeout=30000
spring.datasource.maxLifetime=60000
spring.datasource.connectionTestQuery=SELECT * FROM table where id=1
spring.datasource.minimumIdle=1
spring.datasource.maximumPoolSize=5
spring.datasource.poolName=voiceapp-db-pool
spring.datasource.autoCommit=false
jdbc-url
也是 hikari 特定的属性,这就是它现在起作用的原因。
总结一下:不带
hikari.
和带 jdbc-url
的属性用于手动创建的数据源 bean,带 hikari.
和 url
的属性用于 Spring Boot 数据源自动配置。
如果您已经使用带有 hikari 和数据源配置的 application.properties 文件,那么当您使用 new HikariDataSource(this) 时,它将覆盖您的 application.properties 值。
您可以手动创建 HikariDataSource 并从 application.properties 中删除:
@Bean(name = "dataSource")
public DataSource dataSource() {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setMaximumPoolSize(5);
hikariDataSource.setMaxLifetime(60000);
hikariDataSource.setMinimumIdle(1);
//.. some other configs
return hikariDataSource;
}
或仅使用 application.properties 值。
为了保持配置为标准格式,并仍然显式创建数据源,可以将连接池特定的前缀用于配置属性。这与使用自动配置时 spring boot DataSourceConfiguration 所做的相同:
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public HikariDataSource dataSource(DataSourceProperties properties) {
HikariDataSource dataSource = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
if (StringUtils.hasText(properties.getName())) {
dataSource.setPoolName(properties.getName());
}
return dataSource;
}
样品
application.yaml
:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/postgres?gssEncMode=disable
username: postgres
password: postgres
hikari:
minimumIdle: 0
maximumPoolSize: 50
idleTimeout: 90000
maxLifetime: 900000
connectionTimeout: 45000
leakDetectionThreshold: 45000
但是有些东西不适用于 application.properties,所以我现在这样做了: DbConfig.class(es)
@Primary
@Bean(name = "dataSource")
public DataSource dataSource() {
HikariConfig config = new HikariConfig("/hikari_voiceapp.properties");
//HikariDataSource dataSource = new HikariDataSource(config);
return new HikariDataSource(config);
}
并在单独的 hikari.properties 文件中指定每个数据库的所有参数,包括数据库名称:
hikari_asterisk.properties
hikari_billing.properties
hikari_voiceapp.properties
对我有用。
我使用了以下方法
second.datasource.jdbc-url=jdbc-url
second.datasource.username=username
second.datasource.password=password
.
.
.
.
==================== 在 Java 配置文件中====================
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix = "second.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("secondDataSource") DataSource dataSource) {
Map<String, String> props = new HashMap<String, String>();
props.put("spring.jpa.database-platform", "org.hibernate.dialect.Oracle12cDialect");
.
.
.
return builder.dataSource(dataSource).packages("com.second.entity").persistenceUnit("secondDB")
.properties(props)
.build();
}
@Bean(name = "secondTransactionManager")
public PlatformTransactionManager secondTransactionManager(
@Qualifier("secondEntityManagerFactory") EntityManagerFactory secondEntityManagerFactory) {
return new JpaTransactionManager(secondEntityManagerFactory);
}
我也面临这个问题。我喜欢这个解决方案:
我的application.yml
spring:
db1:
datasource:
jdbc-url: 'jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/${DB_NAME:postgres}'
username: ${PG_USER}
password: ${PG_PASS}
driver-class-name: org.postgresql.Driver
schema: file_integration_manager_dev
maximum-pool-size: 2
connection-timeout: 30000
max-lifetime: 45000
我的结果配置bean文件:
@Bean(name = "datasource1")
@ConfigurationProperties(prefix = "spring.db1.datasource")
public DataSource dataSource() {
return new HikariDataSource();
}
在最新的springboot版本中,不需要在config中指定hikari。 并且属性应该有连字符 (-)
替换 spring.datasource.hikari.maximumPoolSize=5 与 spring.datasource.maximum-pool-size=5