项目地址:https://github.com/alibaba/druid/wiki
这和fastjson
一样也是温绍写的
首先引入gav
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid-version}</version>
</dependency>
我当前引入的版本是1.2.8
在配置文件中开启统计监控和可视化面板
# 应用名称
spring.application.name=simple-druid
# 应用服务 WEB 访问端口
server.port=8787
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://192.168.0.1:3306/ruben?autoReconnect=true&zeroDateTimeBehavior=CONVERT_TO_NULL&useUnicode=true&characterEncoding=utf-8&useSSL=false&nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true&allowPublicKeyRetrieval=true
# 数据库用户名&密码:
spring.datasource.username=用户名
spring.datasource.password=密码
# druid配置
# Druid内置提供一个StatFilter,用于统计监控信息
spring.datasource.druid.web-stat-filter.enabled=true
# Druid内置提供了一个StatViewServlet用于展示Druid的统计信息
spring.datasource.druid.stat-view-servlet.enabled=true
我们配置好了,启动项目,访问:http://localhost:8787/druid/datasource.html
我们可以看到对我们数据库的监控
包括能看到连接池的信息等
非常好用
我们访问测试一下
可以看到连接数确实有监控到
我们也可以限制一下线程数
# 最大线程数
spring.datasource.druid.max-active=5
# 最大等待时间
spring.datasource.druid.max-wait=5000
然后我们再次请求,当超过这个连接数时,如果其他连接没释放,则会抛出异常