![TOC]
集成Druid
Druid简介
Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池。
Druid 是阿里巴巴开源平台上一个数据库连接池实现,结合了 C3P0、DBCP 等 DB 池的优点,同时加入了日志监控。
Druid 可以很好的监控 DB 池连接和 SQL 的执行情况,天生就是针对监控而生的 DB 连接池。
Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验。
Spring Boot 2.0 以上默认使用 Hikari 数据源,可以说 Hikari 与 Driud 都是当前 Java Web 上最优秀的数据源,我们来重点介绍 Spring Boot 如何集成 Druid 数据源,如何实现数据库监控。
Github地址:https://github.com/alibaba/druid/
com.alibaba.druid.pool.DruidDataSource 基本配置参数如下:



配置数据源
1、添加上 Druid 数据源依赖。
| 12
 3
 4
 5
 6
 
 | <dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>druid</artifactId>
 <version>1.1.21</version>
 </dependency>
 
 | 
2、切换数据源;之前已经说过 Spring Boot 2.0 以上默认使用 com.zaxxer.hikari.HikariDataSource 数据源,但可以 通过 spring.datasource.type 指定数据源。
| 12
 3
 4
 5
 6
 7
 
 | spring:datasource:
 username: root
 password: 123456
 url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
 driver-class-name: com.mysql.cj.jdbc.Driver
 type: com.alibaba.druid.pool.DruidDataSource
 
 | 
3、数据源切换之后,在测试类中注入 DataSource,然后获取到它,输出一看便知是否成功切换;

4、切换成功!既然切换成功,就可以设置数据源连接初始化大小、最大连接数、等待时间、最小连接数 等设置项;可以查看源码
| 12
 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
 
 | spring:datasource:
 username: root
 password: 123456
 #?serverTimezone=UTC解决时区的报错
 url: jdbc:mysql:
 driver-class-name: com.mysql.cj.jdbc.Driver
 type: com.alibaba.druid.pool.DruidDataSource
 
 #Spring Boot 默认是不注入这些属性值的,需要自己绑定
 #druid 数据源专有配置
 initialSize: 5
 minIdle: 5
 maxActive: 20
 maxWait: 60000
 timeBetweenEvictionRunsMillis: 60000
 minEvictableIdleTimeMillis: 300000
 validationQuery: SELECT 1 FROM DUAL
 testWhileIdle: true
 testOnBorrow: false
 testOnReturn: false
 poolPreparedStatements: true
 
 #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
 #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
 #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
 filters: stat,wall,log4j
 maxPoolPreparedStatementPerConnectionSize: 20
 useGlobalDataSourceStat: true
 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
 
 | 
5、导入Log4j 的依赖
| 12
 3
 4
 5
 6
 
 | <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
 </dependency>
 
 | 
6、现在需要程序员自己为 DruidDataSource 绑定全局配置文件中的参数,再添加到容器中,而不再使用 Spring Boot 的自动生成了;我们需要 自己添加 DruidDataSource 组件到容器中,并绑定属性;
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | package com.kuang.config;
 import com.alibaba.druid.pool.DruidDataSource;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 import javax.sql.DataSource;
 
 @Configuration
 public class DruidConfig {
 
 
 
 
 
 
 
 @ConfigurationProperties(prefix = "spring.datasource")
 @Bean
 public DataSource druidDataSource() {
 return new DruidDataSource();
 }
 
 }
 
 | 
7、去测试类中测试一下;看是否成功!
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | @SpringBootTestclass SpringbootDataJdbcApplicationTests {
 
 
 @Autowired
 DataSource dataSource;
 
 @Test
 public void contextLoads() throws SQLException {
 
 System.out.println(dataSource.getClass());
 
 Connection connection =   dataSource.getConnection();
 System.out.println(connection);
 
 DruidDataSource druidDataSource = (DruidDataSource) dataSource;
 System.out.println("druidDataSource 数据源最大连接数:" + druidDataSource.getMaxActive());
 System.out.println("druidDataSource 数据源初始化连接数:" + druidDataSource.getInitialSize());
 
 
 connection.close();
 }
 }
 
 | 
输出结果 :可见配置参数已经生效!

配置Druid数据源监控
Druid 数据源具有监控的功能,并提供了一个 web 界面方便用户查看,类似安装 路由器 时,人家也提供了一个默认的 web 页面。
所以第一步需要设置 Druid 的后台管理页面,比如 登录账号、密码 等;配置后台管理;
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 
 | 
 @Bean
 public ServletRegistrationBean statViewServlet() {
 ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
 
 
 
 Map<String, String> initParams = new HashMap<>();
 initParams.put("loginUsername", "admin");
 initParams.put("loginPassword", "123456");
 
 
 
 
 initParams.put("allow", "");
 
 
 
 
 bean.setInitParameters(initParams);
 return bean;
 }
 
 | 
配置完毕后,我们可以选择访问 :http://localhost:8080/druid/login.html

 进入之后

 配置 Druid web 监控 filter 过滤器
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | 
 @Bean
 public FilterRegistrationBean webStatFilter() {
 FilterRegistrationBean bean = new FilterRegistrationBean();
 bean.setFilter(new WebStatFilter());
 
 
 Map<String, String> initParams = new HashMap<>();
 initParams.put("exclusions", "*.js,*.css,/druid/*,/jdbc/*");
 bean.setInitParameters(initParams);
 
 
 bean.setUrlPatterns(Arrays.asList("/*"));
 return bean;
 }
 
 | 
平时在工作中,按需求进行配置即可,主要用作监控!