Spring Boot autoconfig 仍在使用 Tomcat 数据源而不是 HikariCP?
Posted
技术标签:
【中文标题】Spring Boot autoconfig 仍在使用 Tomcat 数据源而不是 HikariCP?【英文标题】:Spring Boot autoconfig is still taking Tomcat Datasource instead HikariCP? 【发布时间】:2015-12-10 20:55:40 【问题描述】:我有我的 Spring Boot 1.2.5.RELEASE 服务,我想使用 HikariCP
数据源而不是默认的 tomcat-jdbc。所以,根据这个Spring Boot Reference,我知道我只需要从类路径中排除tomcat-jdbc
并添加HikariCP
。
这是我的 pom.xml:
...
<dependencyManagement>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>$spring-boot.version</version>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>$spring-boot.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
</dependencies>
...
maven 依赖树:
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ myproject-user-manage-webservice ---
[INFO] com.mybusiness.myproject:myproject-user-manage-webservice:jar:0.0.1-SNAPSHOT
[INFO] +- com.mybusiness.myproject:myproject-commons:jar:0.0.1-SNAPSHOT:compile
[INFO] | \- com.mybusiness.myproject:myproject-core:jar:0.0.1-SNAPSHOT:compile
[INFO] | \- com.mybusiness.myproject:myproject-core-commons:jar:0.0.1-SNAPSHOT:compile
[INFO] +- com.mybusiness.myproject:myproject-api:jar:0.0.1-SNAPSHOT:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.4:compile (version managed from 2.4.6)
[INFO] | \- com.mybusiness.myproject:myproject-framework:jar:0.0.1-SNAPSHOT:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-security:jar:1.2.1.RELEASE:compile (version managed from 1.2.5.RELEASE)
[INFO] | +- mysql:mysql-connector-java:jar:5.1.34:compile (version managed from 5.1.35)
[INFO] | +- com.mybusiness.framework:despegar-fwk-logging:jar:1.4.148:compile
[INFO] | +- org.jenkins-ci.plugins:testInProgress-client:jar:1.4:compile
[INFO] | | \- org.json:json:jar:20140107:compile
[INFO] | +- commons-io:commons-io:jar:1.3.2:compile
[INFO] | \- org.apache.commons:commons-lang3:jar:3.4:compile
[INFO] +- com.mybusiness.myproject:myproject-user-manage-domain:jar:0.0.1-SNAPSHOT:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-aop:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.aspectj:aspectjrt:jar:1.8.4:compile (version managed from 1.8.2)
[INFO] | | \- org.aspectj:aspectjweaver:jar:1.8.4:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.2.1.RELEASE:compile
[INFO] | | | +- org.springframework:spring-jdbc:jar:4.1.4.RELEASE:compile
[INFO] | | | \- org.springframework:spring-tx:jar:4.1.4.RELEASE:compile
[INFO] | | +- org.hibernate:hibernate-entitymanager:jar:4.3.7.Final:compile
[INFO] | | | +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] | | | +- org.hibernate:hibernate-core:jar:4.3.7.Final:compile
[INFO] | | | | +- antlr:antlr:jar:2.7.7:compile
[INFO] | | | | \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] | | | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | | | | \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] | | | +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] | | | \- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] | | +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] | | +- org.springframework:spring-orm:jar:4.1.4.RELEASE:compile (version managed from 4.0.7.RELEASE)
[INFO] | | +- org.springframework.data:spring-data-jpa:jar:1.7.1.RELEASE:compile
[INFO] | | | \- org.springframework.data:spring-data-commons:jar:1.9.1.RELEASE:compile
[INFO] | | \- org.springframework:spring-aspects:jar:4.1.4.RELEASE:compile
[INFO] | +- org.springframework.security:spring-security-jwt:jar:1.0.2.RELEASE:compile (version managed from 1.0.3.RELEASE)
[INFO] | | \- org.bouncycastle:bcpkix-jdk15on:jar:1.47:compile
[INFO] | | \- org.bouncycastle:bcprov-jdk15on:jar:1.47:compile
[INFO] | \- org.flywaydb:flyway-core:jar:3.0:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.1.RELEASE:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.14:runtime
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.2.1.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.15:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.15:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.15:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.15:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.4:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.4.4:compile
[INFO] | +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | | \- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] | +- org.springframework:spring-core:jar:4.1.4.RELEASE:compile
[INFO] | +- org.springframework:spring-web:jar:4.1.4.RELEASE:compile
[INFO] | \- org.springframework:spring-webmvc:jar:4.1.4.RELEASE:compile
[INFO] | \- org.springframework:spring-expression:jar:4.1.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-actuator:jar:1.2.1.RELEASE:compile
[INFO] | \- org.springframework.boot:spring-boot-actuator:jar:1.2.1.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.2.1.RELEASE:test (scope not updated to compile)
[INFO] | +- junit:junit:jar:4.12:test
[INFO] | +- org.mockito:mockito-core:jar:1.10.8:test
[INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] | \- org.springframework:spring-test:jar:4.1.4.RELEASE:test
[INFO] +- org.springframework.boot:spring-boot-starter-log4j:jar:1.2.1.RELEASE:compile
[INFO] | +- org.slf4j:jcl-over-slf4j:jar:1.7.8:compile (version managed from 1.7.7)
[INFO] | +- org.slf4j:jul-to-slf4j:jar:1.7.8:compile
[INFO] | +- org.slf4j:slf4j-log4j12:jar:1.7.8:compile
[INFO] | \- log4j:log4j:jar:1.2.17:compile
[INFO] +- org.springframework.security.oauth:spring-security-oauth2:jar:2.0.7.RELEASE:compile
[INFO] | +- org.springframework:spring-beans:jar:4.1.4.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:4.1.4.RELEASE:compile
[INFO] | +- org.springframework.security:spring-security-core:jar:3.2.5.RELEASE:compile
[INFO] | | \- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- org.springframework.security:spring-security-config:jar:3.2.5.RELEASE:compile
[INFO] | +- org.springframework.security:spring-security-web:jar:3.2.5.RELEASE:compile
[INFO] | +- commons-codec:commons-codec:jar:1.6:compile
[INFO] | \- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.13:compile
[INFO] | \- org.codehaus.jackson:jackson-core-asl:jar:1.9.13:compile
[INFO] +- org.springframework.hateoas:spring-hateoas:jar:0.16.0.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:4.1.4.RELEASE:compile (version managed from 3.2.9.RELEASE)
[INFO] | +- org.objenesis:objenesis:jar:2.1:compile
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.8:compile (version managed from 1.7.7)
[INFO] +- com.zaxxer:HikariCP:jar:2.2.5:compile
[INFO] | \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] +- ma.glasnost.orika:orika-core:jar:1.4.5:compile
[INFO] | +- com.thoughtworks.paranamer:paranamer:jar:2.3:compile
[INFO] | +- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.2_jdk5:compile
[INFO] | \- com.carrotsearch:java-sizeof:jar:0.0.4:compile
[INFO] +- io.springfox:springfox-swagger2:jar:2.1.1:compile
[INFO] | +- org.mapstruct:mapstruct:jar:1.0.0.Beta4:compile
[INFO] | +- io.swagger:swagger-annotations:jar:1.5.0:compile
[INFO] | +- io.swagger:swagger-models:jar:1.5.0:compile
[INFO] | +- io.springfox:springfox-spi:jar:2.1.1:compile
[INFO] | | \- io.springfox:springfox-core:jar:2.1.1:compile
[INFO] | +- io.springfox:springfox-schema:jar:2.1.1:compile
[INFO] | +- io.springfox:springfox-swagger-common:jar:2.1.1:compile
[INFO] | +- io.springfox:springfox-spring-web:jar:2.1.1:compile
[INFO] | +- com.google.guava:guava:jar:18.0:compile
[INFO] | +- com.fasterxml:classmate:jar:1.2.0:compile
[INFO] | +- joda-time:joda-time:jar:2.5:compile
[INFO] | +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
[INFO] | \- org.springframework.plugin:spring-plugin-metadata:jar:1.2.0.RELEASE:compile
[INFO] \- io.springfox:springfox-swagger-ui:jar:2.1.1:compile
所以,HikariCP
肯定在类路径中,而tomcat-jdbc
不在。但是当我启动服务时,autoconfig
仍在创建 org.apache.tomcat.jdbc.pool.DataSource 而不是 HikariCP
一个。
我做错了什么?
编辑
这是我的应用程序启动器:
@SpringBootApplication
@EnableHypermediaSupport(type = HypermediaType.HAL)
public class ApplicationRunner
public static void main(String[] args)
SpringApplication.run(ApplicationRunner.class, args);
【问题讨论】:
你是如何启动服务的? 尝试清理项目,也许 jar 仍然在您的工件的输出中。 您是从命令行运行 jar 还是从 IDE 启动主类。 (如果后者你能提到你的IDE)至少尝试从命令行运行jar。 你能验证你的类路径上没有这个类org.apache.tomcat.jdbc.pool.DataSource
,在eclipse上打开类型(ctrl+shif+t)类型DataSource,这个类org.apache.tomcat.jdbc.pool.DataSource
是否出现在列表中?
先尝试最简单的。尝试清理您的项目以确保不存在 tomcat jar。此外,您确定您没有在 XML 中定义任何可能潜入您的类路径的上下文吗?
【参考方案1】:
在你的 pom 中包含 Hikari
将以下属性添加到您的 application.yml
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
...
【讨论】:
【参考方案2】:编辑:这个答案提供了在标准 Spring 中配置 Hikari 的方法,对 Boot 也有效。但是,自从我编写 Boot 以来,它已经为 Hikari 添加了更大的集成,因此像 this 这样的答案可能是有效的并且更适合 Boot。
只需在 @Configuration
类中提供数据源:
@Configuration
public class HikariCPConfig
@Value("$hikari.driverclassname")
private String driverClassName;
@Value("$hikari.jdbc.url")
private String jdbcUrl;
@Value("$hikari.username")
private String userName;
@Value("$hikari.password")
private String password;
@Value("$hikari.pool.size")
private int poolSize;
@Bean(destroyMethod = "close")
public DataSource dataSource()
final HikariDataSource ds = new HikariDataSource();
ds.setMaximumPoolSize(poolSize);
ds.setDriverClassName(driverClassName);
ds.setJdbcUrl(jdbcUrl);
ds.setUsername(userName);
ds.setPassword(password);
return ds;
然后,在 application.properties
文件中,声明数据源的属性,这些属性可以由 maven 构建提供:
#Hikari
hikari.driverclassname = com.mysql.jdbc.Driver
hikari.jdbc.url = jdbc:mysql://localhost:3306/my_db
hikari.username = $db.username
hikari.password = $db.password
hikari.pool.size = 5
然后你需要 Spring 来扫描你的 HikariCPConfig
类,但是当你使用 @SpringBootApplication
时,它等同于 @Configuration
@EnableAutoConfiguration
和 @ComponentScan
它应该不是问题。
另请参阅:
Using the@SpringBootApplication
annotation
Configuring a datasource in Spring Boot
【讨论】:
由于这是一个有效的解决方案,我试图避免这种方式来利用 Spring Boot 自动配置。但如果问题仍然存在,我肯定会这样做。谢谢! 我知道,但是 tomcat 连接在 prod 中失败,因为它会在一段时间不活动时关闭。我认为 hikaricp 可能不会发生这种情况(或者我必须为每个请求设置测试连接,这会大大降低性能) 我解决了这个问题,(但我不知道实际上如何:P)。只是出于另一个原因重新安装了eclipse,一切正常。谢谢!接受你的回答,因为无论如何这是一个解决方案 这听起来可能有点陈词滥调,但我什至不知道发生了什么……我刚刚重新安装了 eclipse(出于其他原因),它就可以工作了……这是一个eclipse-cache 相关问题可能吗?这很奇怪,因为我尝试清理项目并更新依赖项但没有成功。无论如何,我明天会尝试重现错误,看看会发生什么:-) @jscherman,我最近遇到了与 Tomcat 连接池相同的问题(它在一段时间后被关闭,仅在生产中)。我用这个配置解决了这个问题:spring.datasource.test-on-borrow: true
和spring.datasource.validation-query: SELECT 1
。这样你就可以强制池验证每个连接。【参考方案3】:
spring.datasouce.type 以 spring boot 1.3.0 开头,所以在 1.2.5 上不起作用
【讨论】:
什么意思?我在哪里使用 spring.datasource.type? 如果你使用的是1.3.0+,你可以指定属性spring.datasouce.type,否则你必须定义一个bean来覆盖spring-boot自动配置。 我认为您的评论是对@Ben 评论的回应。我明白你所说的,但是当我提出这个问题时,正如你所说,我不能使用 datasour r 类型参数。这样做的方法是我说提到的文档:从类路径中获取 hikari。问题是它也不起作用。然后重新安装eclipse后问题就奇怪了,所以我不知道是什么问题 如果你把war部署到tomcat的webapps目录下。 tomcat-jdbc lib 已经存在。 spring-boot 将在 hikaricp 之前搜索 tomcat-jdbc。所以你的配置将不起作用。我写了一个样本。 github.com/zhanhb/spring-boot-druid-sample/blob/…以上是关于Spring Boot autoconfig 仍在使用 Tomcat 数据源而不是 HikariCP?的主要内容,如果未能解决你的问题,请参考以下文章