易筋SpringBoot 2.1 | 第十四篇:SpringBoot的HikariCP

写作时间:2019-08-07
Spring Boot: 2.1 ,JDK: 1.8, IDE: IntelliJ IDEA

说明

Fast, simple, reliable. HikariCP is a “zero-overhead” production ready JDBC connection pool. At roughly 130Kb, the library is very light.

Using a stub-JDBC implementation to isolate and measure the overhead of HikariCP, comparative benchmarks were performed on a commodity PC.
易筋SpringBoot 2.1 | 第十四篇:SpringBoot的HikariCP_第1张图片

官网详细地说明了HikariCP所做的一些优化:

  1. 字节码精简 :优化代码,直到编译后的字节码最少,这样,CPU缓存可以加载更多的程序代码;
  2. 优化代理和拦截器:减少代码,例如HikariCP的Statement proxy只有100行代码,只有BoneCP的十分之一;
  3. 自定义数组类型(FastStatementList)代替ArrayList:避免每次get()调用都要进行range check,避免调用remove()时的从头到尾的扫描;
  4. 自定义集合类型(ConcurrentBag:提高并发读写的效率;
  5. 其他针对BoneCP缺陷的优化,比如对于耗时超过一个CPU时间片的方法调用的研究(但没说具体怎么优化)。
  6. 调用代码优化: invokevirtual vs invokestatic,从一个单例调用invokevirtual,到静态方法调用invokestatic,少了两个方法get, set, 字节码更少。
    参考文章:https://github.com/brettwooldridge/HikariCP/wiki/Down-the-Rabbit-Hole

工程建立

参照教程【SpringBoot 2.1 | 第十三篇:SpringBoot综合应用多个DataSoure
】, 新建工程和代码实现。

Hikari Datasource 的默认配置

External Library > Maven: org.springframework.boot:spring-boot-autoconfigure

易筋SpringBoot 2.1 | 第十四篇:SpringBoot的HikariCP_第2张图片

org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration

@Configuration
  @ConditionalOnClass({HikariDataSource.class})
  @ConditionalOnMissingBean({DataSource.class})
  @ConditionalOnProperty(
    name = {"spring.datasource.type"},
    havingValue = "com.zaxxer.hikari.HikariDataSource",
    matchIfMissing = true
  )
  static class Hikari {
    Hikari() {
    }

    @Bean
    @ConfigurationProperties(
      prefix = "spring.datasource.hikari"
    )
    public HikariDataSource dataSource(DataSourceProperties properties) {
      HikariDataSource dataSource = (HikariDataSource)DataSourceConfiguration.createDataSource(properties, HikariDataSource.class);
      if (StringUtils.hasText(properties.getName())) {
        dataSource.setPoolName(properties.getName());
      }

      return dataSource;
    }
  }

启动应用

查看相关Hikari 的日志: HikariPool

c.z.d.DemomultidatasourceApplication     : foo datasource: jdbc:h2:mem:foo
c.z.d.DemomultidatasourceApplication     : bar datasource: jdbc:h2:mem:bar
o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
o.s.b.a.e.web.EndpointLinksResolver      : Exposing 15 endpoint(s) beneath base path '/actuator'
o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
c.z.d.DemomultidatasourceApplication     : Started DemomultidatasourceApplication in 2.872 seconds (JVM running for 5.245)
com.zaxxer.hikari.HikariDataSource       : 'HikariPool-1 - Starting...'
o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
 o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
o.s.web.servlet.DispatcherServlet        : Completed initialization in 12 ms
com.zaxxer.hikari.HikariDataSource       : 'HikariPool-1 - Start completed.'
com.zaxxer.hikari.HikariDataSource       : 'HikariPool-2 - Starting...'
com.zaxxer.hikari.HikariDataSource       : 'HikariPool-2 - Start completed.'

总结

Sprint boot 2中默认DataSource是HikariCP。
代码下载:

https://github.com/zgpeace/Spring-Boot2.1/tree/master/db/demomultidatasource

参考

http://brettwooldridge.github.io/HikariCP/
https://github.com/brettwooldridge/HikariCP/wiki/Down-the-Rabbit-Hole
https://www.baeldung.com/hikaricp
https://github.com/brettwooldridge/HikariCP
http://blog.didispace.com/Springboot-2-0-HikariCP-default-reason/

你可能感兴趣的:(易实战Sprint,Boot,2.1)