13.1 DruidDataSource
DRUID 简介
Druid 是阿里巴巴开源平台上的 一个数据库连接池的实现,结合了 C3P0、DBCP、PROXOOL 等 数据库池子 的优点,同时加入了 日志监控。
Druid 可以很好的 监控 数据库池子的连接和 SQL 的执行情况(非常符合咱们国人的开发要求吧,哈哈 ~),天生就是针对监控而生的 DB 连接池。
Spring Boot 2.0 以上的版本 默认 使用的 都是 Hikari
数据源,可以说 Hikari
与 Driud
都是当前 Java WEB 上 优秀的连接池数据源。
依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
配置文件里 指定 数据源类型 type: com.alibaba.druid.pool.DruidDataSource
即可。
了解 一些 Druid 的常用配置
spring:
datasource:
username: root
password: 123123
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许报错,java.lang.ClassNotFoundException: org.apache.Log4j.Properity
#则导入log4j 依赖就行
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.Properity
#则导入log4j 依赖就行
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionoProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
最牛逼的地方在于 DruidConfig
可以自定义一些 配置。
做一个 后台监控,就是 我们可能要做一个 较为安全的后台监控。
package top.muquanyu.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource(){
return new DruidDataSource();
}
// 后台监控功能
@Bean
public ServletRegistrationBean statViewServlet(){
// 这段代码是写死的,不需要去理解 和 记忆。它就是拿到 Druid 的 后台监控
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
// 后台 需要有人 登录,账号密码配置
HashMap<String, String> map = new HashMap<>();
// 这俩也都是 固定的 key,不能随便写
map.put("loginUsername","admin");
map.put("loginPassword","123123");
// 允许谁可以访问
map.put("allow",""); // 这个 最好不要 乱写哟,哈哈
// 禁止谁能访问 也就是黑名单
map.put("mqy","192.168.1.123"); // 后面这个 就是 这个人的 IP 地址
bean.setInitParameters(map); // 设置初始化参数
return bean;
}
}
这个 ServletRegistrationBean 从 英文单词的意思上可以看出来,就是我们无法 直接 操纵 web.xml 所以只能通过 这种方式 来 注册 一个 Servlet。
编写 作用域 Druid 上的 Filter ,也是一样的道理,无法直接在 web.xml 中注册
@Bean
// 这个过滤器 只能作用在 Druid 后台页面上
public FilterRegistrationBean webStartFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
// 可以过滤哪些 请求
Map<String,String> map = new HashMap<>();
// 这些东西不进行 统计 ~
map.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(map);
return bean;
}