
最新更新:2020年9月22日08:21:25 结合这篇文章看:【Spring Boot】008-Spring Boot整合JDBC
一、Druid简介
Java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池;
Druid 是阿里巴巴开源平台上一个数据库连接池实现,结合了 C3P0、DBCP 等 DB 池的优点,同时加入了日志监控;
Druid 可以很好的监控 DB 池连接和 SQL 的执行情况,天生就是针对监控而生的 DB 连接池;
Druid已经在阿里巴巴部署了超过600个应用,经过一年多生产环境大规模部署的严苛考验;
Spring Boot 2.0 以上默认使用 Hikari 数据源,可以说 Hikari 与 Driud 都是当前 Java Web 上最优秀的数据源,我们来重点介绍 Spring Boot 如何集成 Druid 数据源,如何实现数据库监控;
https://github.com/alibaba/druid/



<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/zibo?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource # 自定义数据源spring:
datasource:
username: root
password: zibo15239417242
url: jdbc:mysql://localhost:3306/zibo?serverTimezone=UTC
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 <!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
package com.zibo.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;
import java.util.Map;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource(){
return new DruidDataSource();
}
//后台监控功能:固定写法
//说明:因为SpringBoot内置了Servlet,没有web.xml,替代方法就是ServletRegistrationBean
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
//后台需要有人登录:固定写法
Map<String, String> initParameters = new HashMap<>();
//添加配置,key是固定的
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","666666");
//允许谁能访问
initParameters.put("allow","");//如果此value为空,就表示谁都可以访问
//禁止谁访问
// initParameters.put("账号","密码");
bean.setInitParameters(initParameters);
return bean;
}
}




有点意思了吧!
package com.zibo.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.servlet.Filter;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druidDataSource(){
return new DruidDataSource();
}
//后台监控功能:固定写法
//说明:因为SpringBoot内置了Servlet,没有web.xml,替代方法就是ServletRegistrationBean
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
//后台需要有人登录:固定写法
Map<String, String> initParameters = new HashMap<>();
//添加配置,key是固定的
initParameters.put("loginUsername","admin");
initParameters.put("loginPassword","666666");
//允许谁能访问
initParameters.put("allow","");//如果此value为空,就表示谁都可以访问
//禁止谁访问
// initParameters.put("账号","密码");
bean.setInitParameters(initParameters);
return bean;
}
//配置Filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
//设置过滤器
bean.setFilter(new WebStatFilter());
//可以过滤哪些请求
Map<String,String> initParameters = new HashMap<>();
//这些东西不进行统计
initParameters.put("exclusions",".js,.css,/druid/*");
bean.setInitParameters(initParameters);
return bean;
}
}链接:https://pan.baidu.com/s/1Fs-WTIPS3o7uzbvfPhM7HQ 提取码:zibo