SpringBoot数据库连接池(8)
一、使用Druid数据库连接池
首先是,阿里巴巴的数据库连接池版本与SpringBoot版本会有一定的关系。
(1)导入依赖。
<properties> <springboot.druid.starter.version>1.1.21</springboot.druid.starter.version> <springboot.mybatis.starter.version>2.1.0</springboot.mybatis.starter.version> </properties> <!-- druid start--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${springboot.druid.starter.version}</version> </dependency> <!-- mybatis依赖可拿到了JDBC得依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${springboot.mybatis.starter.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.55</version> </dependency> <!-- druid end-->
(2)然后在yml中加入如下配置。
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver druid: # 配置数据源类型 url: jdbc:mysql://localhost:3306/basessm?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 username: root password: 3333 # 初始化,最小,最大连接数 initialSize: 10 minidle: 10 maxActive: 18 # 获取数据库连接等待的超时时间 maxWait: 60000 # 配置多久进行一次检测,检测需要关闭的空闲连接 单位毫秒 timeBetweenEvictionRunsMillis: 60000 #配置测试查询语句 validationQuery: SELECT 1 FROM dual # 配置监控统计拦截的filters,去掉后,监控界面的sql无法统计 filters: stat,wall
(3)配置SQL监控的控制台
package com.hanzhenya.learnspringboot.database; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 参考https://www.cnblogs.com/NeverCtrl-C/p/8024580.html */ @Configuration public class DruidConfig { @Bean public ServletRegistrationBean statViewServlet() { //创建servlet注册实体 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); //设置ip白名单 servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow servletRegistrationBean.addInitParameter("deny", "192.168.0.19"); //设置控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "admin"); //是否可以重置数据 servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean statFilter() { //创建过滤器 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //设置过滤器过滤路径 filterRegistrationBean.addUrlPatterns("/*"); //忽略过滤的形式 filterRegistrationBean. addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
二、测试
访问SQL监控页面:http://localhost:8081/study/springboot/druid/login.html。输入账号密码admin、admin。
三、加密
因为连接的是阿里巴巴数据源的jar是druid-1.1.21.jar,因此可以输入命令
java -cp druid-1.1.21.jar com.alibaba.druid.filter.config.ConfigTools 你的密码
因此,需要配置password与connectionProperties与开启解密功能config.enabled。
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver druid: # 配置数据源类型 url: jdbc:mysql://localhost:3306/basessm?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 username: root password: fD520yBjHVmHK/jBzMvJNnhUhigWgDgkxLChwzwWvO3Eco3++cO+zq0RGqo1qWb9rFF/A+rfiAJJoKrtv9BZhA== # 初始化,最小,最大连接数 initialSize: 10 minidle: 10 maxActive: 18 # 获取数据库连接等待的超时时间 maxWait: 60000 # 配置多久进行一次检测,检测需要关闭的空闲连接 单位毫秒 timeBetweenEvictionRunsMillis: 60000 #配置测试查询语句 validationQuery: SELECT 1 FROM dual # 配置监控统计拦截的filters,去掉后,监控界面的sql无法统计 filters: stat,wall connectProperties: config.decrypt: true #公钥${publicKey} config.decrypt.key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJU/4SOVwtf/vbv11EvHjxwshhgR9SpL30I/LPvuOFL28e7sEpW48m3IhDMksLGetqLp/sb3x5AC+N0p48TUwZsCAwEAAQ== #开启 druid 密码解密功能 filter: config: enabled: true #password: fD520yBjHVmHK/jBzMvJNnhUhigWgDgkxLChwzwWvO3Eco3++cO+zq0RGqo1qWb9rFF/A+rfiAJJoKrtv9BZhA==