1. 程式人生 > >SpringBoot效能優化之HikariCP連線池

SpringBoot效能優化之HikariCP連線池

以前一直使用阿里Druid資料庫連線池,這段時間聽說有個號稱速度最快、程式碼最簡的後起之秀——HikariCP,於是動手實踐一下

1.依賴如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.codekjm</groupId>
    <artifactId>website</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>website</name>
    <description>官網</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
<!--JPA預設包含號稱最快的HikariCP資料庫連線池,不用引進依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- 阿里fast-json --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2.yml配置如下:

# 檔案上傳配置
spring:
  ## 資料庫配置
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/website?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
    username: root
    password: root
##  Hikari 連線池配置 ------ 詳細配置請訪問:https://github.com/brettwooldridge/HikariCP
    hikari:
## 最小空閒連線數量
     minimum-idle: 10
## 空閒連線存活最大時間,預設600000(10分鐘)
     idle-timeout: 18000
## 連線池最大連線數,預設是10
     maximum-pool-size: 1000
## 此屬性控制從池返回的連線的預設自動提交行為,預設值:true
     auto-commit: true
## 連線池母子
     pool-name: OfficialWebsiteHikariCP
## 此屬性控制池中連線的最長生命週期,值0表示無限生命週期,預設1800000即30分鐘
     max-lifetime: 1800000
## 資料庫連線超時時間,預設30秒,即30000
     connection-timeout: 300000
     connection-test-query: SELECT 1
  jpa:
   show-sql: true
   open-in-view: true
  jackson:
   default-property-inclusion: non_null
logging:
  config: classpath:logback.xml

3.啟動服務,看日誌都是INFO,沒有ERROR,好像沒報啥錯,但是啟動失敗了

 

INFO  [2020-02-08 21:08:29,641][] org.springframework.boot.StartupInfoLogger:55 - Starting OfficialWebsiteApplication on CodeKjm with PID 517424 (E:\code\TcProject\official-website\target\classes started by Code in E:\code\TcProject\official-website) 
INFO  [2020-02-08 21:08:29,642][] org.springframework.boot.SpringApplication:655 - The following profiles are active: dev 
INFO  [2020-02-08 21:08:30,094][] org.springframework.data.repository.config.RepositoryConfigurationDelegate:127 - Bootstrapping Spring Data JPA repositories in DEFAULT mode. 
INFO  [2020-02-08 21:08:30,116][] org.springframework.data.repository.config.RepositoryConfigurationDelegate:187 - Finished Spring Data repository scanning in 4ms. Found 0 JPA repository interfaces. 
INFO  [2020-02-08 21:08:30,502][] com.zaxxer.hikari.HikariDataSource:110 - WebsiteHikariCP - Starting... 
INFO  [2020-02-08 21:08:31,292][] com.zaxxer.hikari.HikariDataSource:123 - WebsiteHikariCP - Start completed. 
INFO  [2020-02-08 21:08:31,448][] org.hibernate.Version:46 - HHH000412: Hibernate Core {5.4.10.Final} 
INFO  [2020-02-08 21:08:31,639][] org.hibernate.annotations.common.reflection.java.JavaReflectionManager:49 - HCANN000001: Hibernate Commons Annotations {5.1.0.Final} 
INFO  [2020-02-08 21:08:31,902][] org.hibernate.dialect.Dialect:172 - HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect 
INFO  [2020-02-08 21:08:32,078][] org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator:52 - HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 
INFO  [2020-02-08 21:08:32,089][] org.springframework.orm.jpa.AbstractEntityManagerFactoryBean:416 - Initialized JPA EntityManagerFactory for persistence unit 'default' 
INFO  [2020-02-08 21:08:32,189][] org.springframework.boot.StartupInfoLogger:61 - Started OfficialWebsiteApplication in 3.075 seconds (JVM running for 4.566) 
INFO  [2020-02-08 21:08:32,194][] org.springframework.orm.jpa.AbstractEntityManagerFactoryBean:598 - Closing JPA EntityManagerFactory for persistence unit 'default' 
INFO  [2020-02-08 21:08:32,198][] com.zaxxer.hikari.HikariDataSource:350 - WebsiteHikariCP - Shutdown initiated... 
Disconnected from the target VM, address: '127.0.0.1:55321', transport: 'socket'
INFO  [2020-02-08 21:08:32,202][] com.zaxxer.hikari.HikariDataSource:352 - WebsiteHikariCP - Shutdown completed. 

調配置調了半天,換回原來的Druid連線池也是不行,再看看日誌,發現倒數第四行:Closing JPA EntityManagerFactory for persistence unit 'default'

發現問題了,這個問題似曾熟悉,好久之前遇到過,而且不是ERROR報出來的,太不明顯了(為自己的粗心找個理由,哈哈),再回去看看依賴,發現少了一個web依賴:
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>

4.把這個依賴加上就行,問題解決,成功啟動

INFO  [2020-02-08 21:13:35,653][] com.zaxxer.hikari.HikariDataSource:110 - WebsiteHikariCP - Starting... 
INFO  [2020-02-08 21:13:36,124][] com.zaxxer.hikari.HikariDataSource:123 - WebsiteHikariCP - Start completed. 
INFO  [2020-02-08 21:13:36,626][] org.hibernate.Version:46 - HHH000412: Hibernate Core {5.3.10.Final} 
INFO  [2020-02-08 21:13:36,628][] org.hibernate.cfg.Environment:213 - HHH000206: hibernate.properties not found 
INFO  [2020-02-08 21:13:37,612][] org.hibernate.annotations.common.reflection.java.JavaReflectionManager:49 - HCANN000001: Hibernate Commons Annotations {5.0.4.Final} 
INFO  [2020-02-08 21:13:38,015][] org.hibernate.dialect.Dialect:157 - HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 
INFO  [2020-02-08 21:13:38,503][] org.springframework.orm.jpa.AbstractEntityManagerFactoryBean:415 - Initialized JPA EntityManagerFactory for persistence unit 'default' 
INFO  [2020-02-08 21:13:38,963][] org.springframework.scheduling.concurrent.ExecutorConfigurationSupport:171 - Initializing ExecutorService 'applicationTaskExecutor' 
INFO  [2020-02-08 21:13:39,415][] org.apache.juli.logging.DirectJDKLog:173 - Starting ProtocolHandler ["http-nio-8080"] 
INFO  [2020-02-08 21:13:39,451][] org.springframework.boot.web.embedded.tomcat.TomcatWebServer:204 - Tomcat started on port(s): 8080 (http) with context path '/Website' 
INFO  [2020-02-08 21:13:39,457][] org.springframework.boot.StartupInfoLogger:59 - Started OfficialWebsiteApplication in 14.086 seconds (JVM running for 17.532) 

 

5.效能測試

有很多朋友已經親測過了,這裡我分享一個地址,大家進去比對一下各項效能,綜合自己業務以及架構需要,採用合適的連線池即可

https://blog.csdn.net/fzghjx/article/details/8877