引言: 在Spring Boot下默认提供了若干种可用的连接池,Druid来自于阿里系的一个开源连接池,在连接池之外,还提供了非常优秀的监控功能,这里讲解如何与Spring Boot实现集成。
1. 环境描述
spring Boot 1.4.0.RELEASE, JDK 1.8
2. Druid介绍
Druid是一个JDBC组件,它包括三部分:
- DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系。
- DruidDataSource 高效可管理的数据库连接池。
- SQLParser
Druid可以做什么?
可以监控数据库访问性能,Druid内置提供了一个功能强大的StatFilter插件,能够详细统计SQL的执行性能,这对于线上分析数据库访问性能有帮助。
替换DBCP和C3P0。Druid提供了一个高效、功能强大、可扩展性好的数据库连接池。
数据库密码加密。直接把数据库密码写在配置文件中,这是不好的行为,容易导致安全问题。DruidDruiver和DruidDataSource都支持PasswordCallback。
SQL执行日志,Druid提供了不同的LogFilter,能够支持Common-Logging、Log4j和JdkLog,你可以按需要选择相应的LogFilter,监控你应用的数据库访问情况。
扩展JDBC,如果你要对JDBC层有编程的需求,可以通过Druid提供的Filter-Chain机制,很方便编写JDBC层的扩展插件。
项目地址: https://github.com/alibaba/druid
3. Spring Boot与Druid的集成
MySQL Driver驱动包:
1
2
3
4
5
|
< dependency >
< groupId >mysql</ groupId >
< artifactId >mysql-connector-java</ artifactId >
< scope >runtime</ scope >
</ dependency >
|
Spring Boot的JPA依赖包:
1
2
3
4
|
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-data-jpa</ artifactId >
</ dependency >
|
阿里系的Druid依赖包:
1
2
3
4
5
|
< dependency >
< groupId >com.alibaba</ groupId >
< artifactId >druid</ artifactId >
< version >1.0.25</ version >
</ dependency >
|
Spring Boot中的application.properties配置信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 驱动配置信息
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/mealsystem?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#连接池的配置信息
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
|
在Spring Boot1.4.0中驱动配置信息没有问题,但是连接池的配置信息不再支持这里的配置项,即无法通过配置项直接支持相应的连接池;这里列出的这些配置项可以通过定制化DataSource来实现。
目前Spring Boot中默认支持的连接池有dbcp,dbcp2, tomcat, hikari三种连接池。
由于Druid暂时不在Spring Bootz中的直接支持,故需要进行配置信息的定制:
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
@Configuration
public class DruidDBConfig {
private Logger logger = LoggerFactory.getLogger(DruidDBConfig. class );
@Value ( "${spring.datasource.url}" )
private String dbUrl;
@Value ( "${spring.datasource.username}" )
private String username;
@Value ( "${spring.datasource.password}" )
private String password;
@Value ( "${spring.datasource.driverClassName}" )
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.maxPoolPreparedStatementPerConnectionSize}" )
private int maxPoolPreparedStatementPerConnectionSize;
@Value ( "${spring.datasource.filters}" )
private String filters;
@Value ( "{spring.datasource.connectionProperties}" )
private String connectionProperties;
@Bean //声明其为Bean实例
@Primary //在同样的DataSource中,首先使用被标注的DataSource
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl( this .dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
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);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error( "druid configuration initialization filter" , e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}
|
DruidDBConfig类被@Configuration标注,用作配置信息; DataSource对象被@Bean声明,为Spring容器所管理, @Primary表示这里定义的DataSource将覆盖其他来源的DataSource。
1
2
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true
|
需要注意的是:spring.datasource.type旧的spring boot版本是不能识别的。
配置StatView的Servlet:
Filter的实现类:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;
@WebFilter (filterName= "druidWebStatFilter" ,urlPatterns= "/*" ,
initParams={
@WebInitParam (name= "exclusions" ,value= "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*" ) //忽略资源
}
)
public class DruidStatFilter extends WebStatFilter {
}
|
StatViewServlet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import com.alibaba.druid.support.http.StatViewServlet;
@WebServlet (urlPatterns= "/druid/*" ,
initParams={
@WebInitParam (name= "allow" ,value= "127.0.0.1,192.168.163.1" ), // IP白名单(没有配置或者为空,则允许所有访问)
@WebInitParam (name= "deny" ,value= "192.168.1.73" ), // IP黑名单 (存在共同时,deny优先于allow)
@WebInitParam (name= "loginUsername" ,value= "admin" ), // 用户名
@WebInitParam (name= "loginPassword" ,value= "123456" ), // 密码
@WebInitParam (name= "resetEnable" ,value= "false" ) // 禁用HTML页面上的“Reset All”功能
})
public class DruidStatViewServlet extends StatViewServlet {
private static final long serialVersionUID = -2688872071445249539L;
}
|
这两个类相当于在web.xml中声明了一个servlet, 等价于如下的配置信息(web.xml):
1
2
3
4
5
6
7
8
|
< servlet >
< servlet-name >DruidStatView</ servlet-name >
< servlet-class >com.alibaba.druid.support.http.StatViewServlet</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name >DruidStatView</ servlet-name >
< url-pattern >/druid/*</ url-pattern >
</ servlet-mapping >
|
filter的配置信息:
1
2
3
4
5
6
7
8
9
10
11
12
|
< filter >
< filter-name >DruidWebStatFilter</ filter-name >
< filter-class >com.alibaba.druid.support.http.WebStatFilter</ filter-class >
< init-param >
< param-name >exclusions</ param-name >
< param-value >*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</ param-value >
</ init-param >
</ filter >
< filter-mapping >
< filter-name >DruidWebStatFilter</ filter-name >
< url-pattern >/*</ url-pattern >
</ filter-mapping >
|
然后相应的配置工作就完成了,直接启动即可看到相应的应用了。
4. 运行界面以及介绍
访问地址: http://192.168.163.1:8080/druid/index.html
5. 参考资料
http://blog.csdn.net/xiaoyu411502/article/details/51392237
http://*.com/questions/32833641/not-able-to-set-spring-datasource-type
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/blueheart20/article/details/52384032