你的道路是什么,老兄?乖孩子的路,疯子的路,五彩的路,浪荡子的路,任何的路。到底在什么地方,给什么人,怎么走呢?——杰克·凯鲁亚克《在路上》
项目地址:https://github.com/alibaba/druid/wiki
这和fastjson
一样也是温绍写的
首先引入gav
1 2 3 4 5
| <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid-version}</version> </dependency>
|
我当前引入的版本是1.2.8

在配置文件中开启统计监控和可视化面板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| spring.application.name=simple-druid
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=密码
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.stat-view-servlet.enabled=true
|
我们配置好了,启动项目,访问:http://localhost:8787/druid/datasource.html
我们可以看到对我们数据库的监控

包括能看到连接池的信息等

非常好用
我们访问测试一下

可以看到连接数确实有监控到
我们也可以限制一下线程数
1 2 3 4
| spring.datasource.druid.max-active=5
spring.datasource.druid.max-wait=5000
|
然后我们再次请求,当超过这个连接数时,如果其他连接没释放,则会抛出异常
