springboot2.0配置连接池(hikari、druid)
springboot2.0配置连接池(hikari、druid)
原文链接:https://www.cnblogs.com/blog5277/p/10660689.html
原文作者:博客园--曲高终和寡
*******************如果你看到这一行,说明爬虫在本人还没有发布完成的时候就抓走了我的文章,导致内容不完整,请去上述的原文链接查看原文****************
springboot2.0之后,自带了一个号称全世界最快的连接池hikari,直接在配置文件里输入以下配置即可:
# Hikari will use the above plus the following to setup connection pooling spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.maximum-pool-size=15 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=HikariCP spring.datasource.hikari.max-lifetime=1800000 spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.connection-test-query=SELECT 1
但是呢,我不仅仅想要连接池,我还想要一个监控页面,阿里的监控有这么多内容,我真的觉得挺实用的,直接在页面上看慢日志啊,session统计啊,我还是挺喜欢的
过去的druid想要配置监控页面,需要写一堆文件和配置,还挺麻烦的,阿里针对springboot做了druid-starter之后,监控就变得轻松容易了,首先在pom文件加入:
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.14</version> </dependency>
然后在配置文件里直接加入配置:
spring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.maxActive=20 spring.datasource.druid.maxWait=60000 spring.datasource.druid.timeBetweenEvictionRunsMillis=60000 spring.datasource.druid.minEvictableIdleTimeMillis=300000 spring.datasource.druid.validationQuery=SELECT 1 spring.datasource.druid.testWhileIdle=true spring.datasource.druid.testOnBorrow=false spring.datasource.druid.testOnReturn=false spring.datasource.druid.poolPreparedStatements=true spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.druid.filters=stat,wall spring.datasource.druid.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000 spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.web-stat-filter.url-pattern=/* spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/* spring.datasource.druid.stat-view-servlet.deny=192.168.1.73 spring.datasource.druid.stat-view-servlet.reset-enable=false #以下4项,将来上生产环境以后一定要改!!!!!一定要改!!!!!一定要改!!!!! spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.druid.stat-view-servlet.allow=127.0.0.1,* spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password=123456
然后重新启动项目就可以了,超级简单,去 localhost:ip/druid就可以看监控了,点图中的表头可以排序哦,分析慢查询是因为什么,很方便