Druid 连接池是阿里巴巴开源的数据库连接池项目,后来贡献给 Apache 开源;
Druid 的作用是负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;
Druid 连接池内置强大的监控功能,其中的 StatFilter 功能,能采集非常完备的连接池执行信息,方便进行监控,而监控特性不影响性能。
Druid 连接池内置了一个监控页面,提供了非常完备的监控信息,可以快速诊断系统的瓶颈。
SpringBoot 1.x 版本默认使用的的 tomcat 的j dbc 连接池,由于 jdbc 性能,稳定性,监控能力都不不太好,所以 SpringBoot 2.x 版本后 默认连接池已经替换成了 HikariCP , HikariCP 性能强、速度快、口碑好、代码少和稳定,暂时不推荐替换成成其他连接池。
这里记录 springboot 项目整合 druid 数据库连接池中间件:
资源准备及版本说明
编程工具:IDEA
JDK版本:1.8
Maven版本:Apache Maven 3.6.3
springboot版本:2.4.4
mybatis版本:1.3.2
mysql版本:5.1.48
druid版本:1.1.21
创建mavem项目
通过 IDEA 创建很便捷,参考《 IDEA创建SpringBoot的maven项目 》,springboot项目整合mybatis参考《 springboot项目整合mybatis 》。
配置pom.xml
druid 的 pom 依赖有两个版本,一个需要编写配置文件,一个是自动配置的,这里选择自动配置版本
1 2 3 4 5 |
< dependency > < groupId >com.alibaba</ groupId > < artifactId >druid-spring-boot-starter</ artifactId > < version >1.1.21</ version > </ dependency > |
完整pom.xml配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<? 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 http://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.4.4</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < groupId >org.example</ groupId > < artifactId >springboot-druid</ artifactId > < version >1.0-SNAPSHOT</ version >
< properties > < java.version >1.8</ java.version > < mybatis.version >1.3.2</ mybatis.version > < mysql.version >5.1.48</ mysql.version > < druid.version >1.1.9</ druid.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-jdbc</ artifactId > </ dependency > < dependency > < groupId >org.mybatis.spring.boot</ groupId > < artifactId >mybatis-spring-boot-starter</ artifactId > < version >${mybatis.version}</ version > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >${mysql.version}</ version > </ dependency > <!-- <dependency>--> <!-- <groupId>com.alibaba</groupId>--> <!-- <artifactId>druid</artifactId>--> <!-- <version>${druid.version}</version>--> <!-- </dependency>--> < dependency > < groupId >com.alibaba</ groupId > < artifactId >druid-spring-boot-starter</ artifactId > < version >1.1.21</ version > </ dependency > < dependency > < groupId >org.projectlombok</ groupId > < artifactId >lombok</ artifactId > < optional >true</ optional > </ dependency > </ dependencies > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > </ project > |
配置application.yml
application.yml 配置文件中需要配置 druid 的相关信息
配置说明如下:
完整application.yml配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
server: port: 8888 spring: application: name: springboot-druid datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/spring-boot-test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 数据库连接池类别 druid: initial-size: 5 # 初始化大小 min-idle: 10 # 最小连接数 max-active: 20 # 最大连接数 max-wait: 60000 # 获取连接时的最大等待时间 min-evictable-idle-time-millis: 300000 # 一个连接在池中最小生存的时间,单位是毫秒 time-between-eviction-runs-millis: 60000 # 多久才进行一次检测需要关闭的空闲连接,单位是毫秒 filters: stat,wall # 配置扩展插件:stat-监控统计,log4j-日志,wall-防火墙(防止SQL注入),去掉后,监控界面的sql无法统计 validation-query: SELECT 1 # 检测连接是否有效的 SQL语句,为空时以下三个配置均无效 test-on-borrow: true # 申请连接时执行validationQuery检测连接是否有效,默认true,开启后会降低性能 test-on-return: true # 归还连接时执行validationQuery检测连接是否有效,默认false,开启后会降低性能 test-while-idle: true # 申请连接时如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效,默认false,建议开启,不影响性能 stat-view-servlet: enabled: true # 是否开启 StatViewServlet allow: 127.0.0.1 # 访问监控页面 白名单,默认127.0.0.1 deny: 192.168.56.1 # 访问监控页面 黑名单 login-username: admin # 访问监控页面 登陆账号 login-password: admin # 访问监控页面 登陆密码 filter: stat: enabled: true # 是否开启 FilterStat,默认true log-slow-sql: true # 是否开启 慢SQL 记录,默认false slow-sql-millis: 5000 # 慢 SQL 的标准,默认 3000,单位:毫秒 merge-sql: false # 合并多个连接池的监控数据,默认false
# mybatis配置 mybatis: mapper-locations: classpath:mapper/*Mapper.xml type-aliases-package: com.dandelion.model
# 输出sql语句日志 logging: level: com: springboot: dao: debug |
访问druid监控中心
启动项目:
在浏览器中输入 http://IP:端口号/druid/index.html 访问监控中心
如果有配置登录账号密码,则需要进行登录:
定义测试接口查询数据库:
监控中心记录访问情况
到此这篇关于springboot项目整合druid数据库连接池的实现的文章就介绍到这了,更多相关springboot druid连接池内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://www.cnblogs.com/dandelion200/p/14672761.html
查看更多关于springboot项目整合druid数据库连接池的实现的详细内容...