SpringBoot性能优化之HikariCP连接池

并发队列之LinkedBlockingQueue

以前一直使用阿里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,好像没报啥错,但是启动失败了

SpringBoot性能优化之HikariCP连接池

 

【编程的乐趣-用python解算法谜题系列】谜题一 保持一致

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/88777442

BaseAdapter的三种表达式分析,startActivityForResult的使用

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享