参考
https://blog.csdn.net/zhaoyachao123/article/details/78413467
https://segmentfault.com/a/1190000013997259
准备:druid是阿里巴巴开源的高性能,多功能的连接池,其主要配置参数如下:
配置 | 缺省值 | 说明 |
name | 配置这个属性的意义在于,如果存在多个数据源,监控的时候可以通过名字来区分开来。如果没有配置,将会生成一个名字,格式是:"DataSource-" + System.identityHashCode(this). 另外配置此属性至少在1.0.5版本中是不起作用的,强行设置name会出错。详情-点此处。 | |
url | 连接数据库的url,不同数据库不一样。例如:mysql : jdbc:mysql://127.0.0.1:3306/druid2 oracle : jdbc:oracle:thin:@127.0.0.1:1521:ocnauto |
|
username | 连接数据库的用户名 | |
password | 连接数据库的密码。如果你不希望密码直接写在配置文件中,可以使用ConfigFilter。详细看这里 | |
driverClassName | 根据url自动识别 这一项可配可不配,如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName | |
initialSize | 0 | 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 |
maxActive | 8 | 最大连接池数量 |
maxIdle | 8 | 已经不再使用,配置了也没效果 |
minIdle | 最小连接池数量 | |
maxWait | 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。 | |
poolPreparedStatements | false | 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。 |
maxPoolPreparedStatementPerConnectionSize | -1 | 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100 |
validationQuery | 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。 | |
validationQueryTimeout | 单位:秒,检测连接是否有效的超时时间。底层调用jdbc Statement对象的void setQueryTimeout(int seconds)方法 | |
testOnBorrow | true | 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 |
testOnReturn | false | 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 |
testWhileIdle | false | 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 |
keepAlive | false | (1.0.28) 连接池中的minIdle数量以内的连接,空闲时间超过minEvictableIdleTimeMillis,则会执行keepAlive操作。 |
timeBetweenEvictionRunsMillis | 1分钟(1.0.14) | 有两个含义: 1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。 2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明 |
numTestsPerEvictionRun | 30分钟(1.0.14) | 不再使用,一个DruidDataSource只支持一个EvictionRun |
minEvictableIdleTimeMillis | 连接保持空闲而不被驱逐的最小时间 | |
connectionInitSqls | 物理连接初始化的时候执行的sql | |
exceptionSorter | 根据dbType自动识别 | 当数据库抛出一些不可恢复的异常时,抛弃连接 |
filters | 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall |
|
proxyFilters | 类型是List<com.alibaba.druid.filter.Filter>,如果同时配置了filters和proxyFilters,是组合关系,并非替换关系 |
springboot 中整合druid 需要添加druid jar 这里spring-boot 的版本是1.5.7
添加druid maven 配置:
-
<dependency>
-
<groupId>com.alibaba
</groupId>
-
<artifactId>druid
</artifactId>
-
<version>1.1.5
</version>
-
</dependency>
配置类如下:其中用到了
@Primary注解,在spring 中使用注解,常使用@Autowired, 默认是根据类型Type来自动注入的。但有些特殊情况,对同一个接口,可能会有几种不同的实现类,而默认只会采取其中一种的情况下 @Primary 的作用就出来了。下面是个简单的使用例子。
-
package com.zyc.config;
-
-
import java.sql.SQLException;
-
import javax.sql.DataSource;
-
import org.springframework.beans.factory.annotation.Value;
-
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 org.springframework.context.annotation.Primary;
-
import com.alibaba.druid.pool.DruidDataSource;
-
import com.alibaba.druid.support.http.StatViewServlet;
-
import com.alibaba.druid.support.http.WebStatFilter;
-
-
@Configuration
-
public
class DruidConfig {
-
-
@Value(
"${spring.datasource.url}")
-
private String dbUrl;
-
-
@Value(
"${spring.datasource.username}")
-
private String username;
-
-
@Value(
"${spring.datasource.password}")
-
private String password;
-
-
@Value(
"${spring.datasource.driver-class-name}")
-
private String driverClassName;
-
-
@Value(
"${spring.datasource.initialSize}")
-
private
int initialSize;
-
-
@Value(
"${spring.datasource.minIdle}")
-
private
int minIdle;
-
-
@Value(
"${spring.datasource.maxActive}")
-
private
int maxActive;
-
-
@Value(
"${spring.datasource.maxWait}")
-
private
int maxWait;
-
-
@Value(
"${spring.datasource.timeBetweenEvictionRunsMillis}")
-
private
int timeBetweenEvictionRunsMillis;
-
-
@Value(
"${spring.datasource.minEvictableIdleTimeMillis}")
-
private
int minEvictableIdleTimeMillis;
-
-
@Value(
"${spring.datasource.validationQuery}")
-
private String validationQuery;
-
-
@Value(
"${spring.datasource.testWhileIdle}")
-
private
boolean testWhileIdle;
-
-
@Value(
"${spring.datasource.testOnBorrow}")
-
private
boolean testOnBorrow;
-
-
@Value(
"${spring.datasource.testOnReturn}")
-
private
boolean testOnReturn;
-
-
@Value(
"${spring.datasource.poolPreparedStatements}")
-
private
boolean poolPreparedStatements;
-
-
@Value(
"${spring.datasource.filters}")
-
private String filters;
-
-
@Value(
"${spring.datasource.logSlowSql}")
-
private String logSlowSql;
-
-
@Bean
-
@Primary
-
public DataSource dataSource(){
-
//@Primary 注解作用是当程序选择dataSource时选择被注解的这个
-
DruidDataSource datasource =
new DruidDataSource();
-
datasource.setUrl(dbUrl);
-
datasource.setUsername(username);
-
datasource.setPassword(password);
-
datasource.setDriverClassName(driverClassName);
-
datasource.setInitialSize(initialSize);
-
datasource.setMinIdle(minIdle);
-
datasource.setMaxActive(maxActive);
-
datasource.setMaxWait(maxWait);
-
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
-
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
-
datasource.setValidationQuery(validationQuery);
-
datasource.setTestWhileIdle(testWhileIdle);
-
datasource.setTestOnBorrow(testOnBorrow);
-
datasource.setTestOnReturn(testOnReturn);
-
datasource.setPoolPreparedStatements(poolPreparedStatements);
-
try {
-
datasource.setFilters(filters);
-
}
catch (SQLException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
return datasource;
-
}
-
-
@Bean
-
public ServletRegistrationBean druidServlet() {
-
ServletRegistrationBean reg =
new ServletRegistrationBean();
-
reg.setServlet(
new StatViewServlet());
-
reg.addUrlMappings(
"/druid/*");
-
reg.addInitParameter(
"loginUsername", username);
-
reg.addInitParameter(
"loginPassword", password);
-
reg.addInitParameter(
"logSlowSql", logSlowSql);
-
return reg;
-
}
-
-
@Bean
-
public FilterRegistrationBean filterRegistrationBean() {
-
FilterRegistrationBean filterRegistrationBean =
new FilterRegistrationBean();
-
filterRegistrationBean.setFilter(
new WebStatFilter());
-
filterRegistrationBean.addUrlPatterns(
"/*");
-
filterRegistrationBean.addInitParameter(
"exclusions",
"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
-
filterRegistrationBean.addInitParameter(
"profileEnable",
"true");
-
return filterRegistrationBean;
-
}
-
}
application.properties配置如下:
-
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
-
spring.datasource.url = jdbc:oracle:thin:@
127.0.0.1/orcl
-
spring.datasource.username = root
-
spring.datasource.password =
123456
-
spring.datasource.driver-
class-name = oracle.jdbc.driver.OracleDriver
-
spring.datasource.filters = stat,wall,log4j
-
spring.datasource.maxActive =
20
-
spring.datasource.initialSize =
1
-
spring.datasource.maxWait =
60000
-
spring.datasource.minIdle =
1
-
spring.datasource.timeBetweenEvictionRunsMillis =
60000
-
spring.datasource.minEvictableIdleTimeMillis =
300000
-
spring.datasource.validationQuery = select
'x'
-
spring.datasource.testWhileIdle =
true
-
spring.datasource.testOnBorrow =
false
-
spring.datasource.testOnReturn =
false
-
spring.datasource.poolPreparedStatements =
true
-
spring.datasource.maxOpenPreparedStatements =
20
-
spring.datasource.logSlowSql=
true
spring.datasource.type 是spring-boot 指定datasource类型的属性不是druid的属性,使用时需要注意,个别版本的spring-boot 不支持spring.datasource.type这个属性建议使用1.4.1版本以上的spring-boot
启动项目访问http://ip:port/druid/login.html