1、Druid是数据库连接池,功能、性能、扩展性方面都算不错。最大的亮点是为监控而生的数据库连接池。
2、数据库、数据源、数据库连接池、JDBC、JDBC实现是什么关系?
https://mvnrepository.com/search?q=druid
<!-- Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
修改application.yml文件,增加type属性
type: com.alibaba.druid.pool.DruidDataSource
数据源:class com.alibaba.druid.pool.DruidDataSource 数据库连接:com.mysql.cj.jdbc.ConnectionImpl@202898d7
# druid 配置
dbType: mysql # 指定数据库类型 mysql
initialSize: 5 # 启动初始化连接数量
minIdle: 5 # 最小空闲连接
maxActive: 20 # 最大连接数量(包含使用中的和空闲的)
maxWait: 60000 # 最大连接等待时间 ,超出时间报错
timeBetweenEvictionRunsMillis: 60000 # 设置执行一次连接回收器的时间
minEvictableIdleTimeMillis: 300000 # 设置时间: 该时间内没有任何操作的空闲连接会被回收
validationQuery: select 'x' # 验证连接有效性的sql
testWhileIdle: true # 空闲时校验
testOnBorrow: false # 使用中是否校验有效性
testOnReturn: false # 归还连接池时是否校验
poolPreparedStatements: false # mysql 不推荐打开预处理连接池
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j # 设置过滤器 stat用于接收状态,wall防止sql注入,logback说明使用logback进行日志输出
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true # 统计所有数据源状态
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # sql合并统计 设置慢sql时间为500,超过500 会有记录提示
package com.jackson0714.springboot.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
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;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid() {
return new DruidDataSource();
}
// 配置Durid监控
// 1、配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
Map<String, String> servletInitParams = new HashMap<>();
servletInitParams.put("loginUserName", "Admin");
servletInitParams.put("loginPassword", "abc123");
servletInitParams.put("deny","192.168.10.160"); // 拒绝访问
servletInitParams.put("allow",""); // 默认允许所有
servletRegistrationBean.setInitParameters(servletInitParams);
return servletRegistrationBean;
}
// 2、配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
Map<String, String> filterInitParams = new HashMap<>();
filterInitParams.put("exclusions", "*.js,*.css,/druid/*");// 不拦截js、css文件请求,不拦截/druid/*的请求
filterRegistrationBean.setInitParameters(filterInitParams);
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); // 拦截所有请求
return filterRegistrationBean;
}
}
运行测试程序,可以看到DataSource中的属性值已经与配置文件中的相同
http://192.168.10.160:8082/druid
会提示没有权限访问该后台
查看监控:
有一条查询请求的记录
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'spring.datasource' to javax.sql.DataSource:
Property: spring.datasource.filters
Value: stat,wall,log4j
Origin: class path resource [application.yml]:23:14
Reason: org.apache.log4j.Logger
Action:
Update your application's configuration
Process finished with exit code 1
解决方案:pom.xml文件中添加log4j的配置
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
servletInitParams.put("loginUsername", "Admin");
servletInitParams.put("loginPassword", "abc123");
未找到解决方案
下一篇我们来讲Spring Boot 整合 MyBatis
关注公众号:悟空聊架构,回复pmp,领取pmp资料!回复悟空,领取架构师资料!