一:引入Druid依赖
打开上篇文章 从头开始搭建自己的springboot后台框架(一)中提到的pom.xml
找到<dependencies></dependencies>标签,在标签中添加Druid依赖
1
2
3
4
5
6 1<dependency>
2 <groupId>com.alibaba</groupId>
3 <artifactId>druid</artifactId>
4 <version>1.0.29</version>
5</dependency>
6
刚添加时 <version>1.0.29</version> 显示红色
然后鼠标右键选择Maven→Reimport进行依赖下载
下载成功后 <version>1.0.29</version> 显示黑色
二:配置数据源连接池信息
在application.properties文件中添加如下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
2#连接池配置
3spring.datasource.initialSize=5
4spring.datasource.minIdle=5
5spring.datasource.maxActive=20
6#连接等待超时时间
7spring.datasource.maxWait=60000
8#配置隔多久进行一次检测(检测可以关闭的空闲连接)
9spring.datasource.timeBetweenEvictionRunsMillis=60000
10#配置连接在池中的最小生存时间
11spring.datasource.minEvictableIdleTimeMillis=300000
12spring.datasource.validationQuery=SELECT 1 FROM DUAL
13spring.datasource.testWhileIdle=true
14spring.datasource.testOnBorrow=false
15spring.datasource.testOnReturn=false
16# 打开PSCache,并且指定每个连接上PSCache的大小
17spring.datasource.poolPreparedStatements=true
18spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
19# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
20spring.datasource.filters=stat,wall,slf4j
21# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
22spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
23
三:添加配置文件
在core→configurer下创建DruidDataSourceConfigurer.java 和DruidMonitorConfigurer.java
内容如下:
DruidDataSourceConfigurer
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 1package com.example.demo.core.configurer;
2
3import com.alibaba.druid.pool.DruidDataSource;
4import org.slf4j.Logger;
5import org.slf4j.LoggerFactory;
6import org.springframework.beans.factory.annotation.Value;
7import org.springframework.context.annotation.Bean;
8import org.springframework.context.annotation.Configuration;
9
10import javax.sql.DataSource;
11import java.sql.SQLException;
12
13/**
14 * @author phubing
15 * @Description:
16 * @time 2018/9/18 15:16
17 */
18@Configuration
19public class DruidDataSourceConfigurer {
20
21 private Logger logger = LoggerFactory.getLogger(DruidDataSourceConfigurer.class);
22
23 @Value("${spring.datasource.url}")
24 private String dbUrl;
25
26 @Value("${spring.datasource.username}")
27 private String username;
28
29 @Value("${spring.datasource.password}")
30 private String password;
31
32 @Value("${spring.datasource.driverClassName}")
33 private String driverClassName;
34
35 @Value("${spring.datasource.initialSize}")
36 private int initialSize;
37
38 @Value("${spring.datasource.minIdle}")
39 private int minIdle;
40
41 @Value("${spring.datasource.maxActive}")
42 private int maxActive;
43
44 @Value("${spring.datasource.maxWait}")
45 private int maxWait;
46
47 @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
48 private int timeBetweenEvictionRunsMillis;
49
50 @Value("${spring.datasource.minEvictableIdleTimeMillis}")
51 private int minEvictableIdleTimeMillis;
52
53 @Value("${spring.datasource.validationQuery}")
54 private String validationQuery;
55
56 @Value("${spring.datasource.testWhileIdle}")
57 private boolean testWhileIdle;
58
59 @Value("${spring.datasource.testOnBorrow}")
60 private boolean testOnBorrow;
61
62 @Value("${spring.datasource.testOnReturn}")
63 private boolean testOnReturn;
64
65 @Value("${spring.datasource.poolPreparedStatements}")
66 private boolean poolPreparedStatements;
67
68 @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
69 private int maxPoolPreparedStatementPerConnectionSize;
70
71 @Value("${spring.datasource.filters}")
72 private String filters;
73
74 @Value("{spring.datasource.connectionProperties}")
75 private String connectionProperties;
76
77 @Bean
78 public DataSource getDataSource() {
79 DruidDataSource datasource = new DruidDataSource();
80 datasource.setUrl(this.dbUrl);
81 datasource.setUsername(username);
82 datasource.setPassword(password);
83 datasource.setDriverClassName(driverClassName);
84
85 //configuration
86 datasource.setInitialSize(initialSize);
87 datasource.setMinIdle(minIdle);
88 datasource.setMaxActive(maxActive);
89 datasource.setMaxWait(maxWait);
90 datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
91 datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
92 datasource.setValidationQuery(validationQuery);
93 datasource.setTestWhileIdle(testWhileIdle);
94 datasource.setTestOnBorrow(testOnBorrow);
95 datasource.setTestOnReturn(testOnReturn);
96 datasource.setPoolPreparedStatements(poolPreparedStatements);
97 datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
98 try {
99 datasource.setFilters(filters);
100 } catch (SQLException e) {
101 logger.error("druid configuration initialization filter", e);
102 }
103 datasource.setConnectionProperties(connectionProperties);
104
105 return datasource;
106 }
107}
108
109
DruidMonitorConfigurer
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 1package com.example.demo.core.configurer;
2
3import org.springframework.boot.web.servlet.FilterRegistrationBean;
4import org.springframework.boot.web.servlet.ServletRegistrationBean;
5import org.springframework.context.annotation.Bean;
6import org.springframework.context.annotation.Configuration;
7import com.alibaba.druid.support.http.StatViewServlet;
8import com.alibaba.druid.support.http.WebStatFilter;
9
10/**
11 * @author phubing
12 * @Description:
13 * @time 2018/9/18 15:16
14 */
15@Configuration
16public class DruidMonitorConfigurer {
17
18 /**
19 * 注册ServletRegistrationBean
20 * @return
21 */
22 @Bean
23 public ServletRegistrationBean registrationBean() {
24 ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
25 /** 初始化参数配置,initParams**/
26 //白名单
27 bean.addInitParameter("allow", "127.0.0.1");//多个ip逗号隔开
28 //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
29 //bean.addInitParameter("deny", "192.168.1.110");
30 //登录查看信息的账号密码.
31 bean.addInitParameter("loginUsername", "admin");
32 bean.addInitParameter("loginPassword", "123456");
33 //是否能够重置数据.
34 bean.addInitParameter("resetEnable", "false");
35 return bean;
36 }
37
38 /**
39 * 注册FilterRegistrationBean
40 * @return
41 */
42 @Bean
43 public FilterRegistrationBean druidStatFilter() {
44 FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter());
45 //添加过滤规则.
46 bean.addUrlPatterns("/*");
47 //添加不需要忽略的格式信息.
48 bean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
49 return bean;
50 }
51
52}
53
四:启动服务器,访问数据连接池管理页面
打开浏览器,输入**http://localhost:8080/druid **出现以下页面,为配置成功
账号为admin,密码为123456
可以查看数据源,SQL监控,URL监控等信息