从零搭建自己的SpringBoot后台框架(二)

释放双眼,带上耳机,听听看~!

一:引入Druid依赖

打开上篇文章  从头开始搭建自己的springboot后台框架(一)中提到的pom.xml

找到<dependencies></dependencies>标签,在标签中添加Druid依赖


1
2
3
4
5
6
1&lt;dependency&gt;
2   &lt;groupId&gt;com.alibaba&lt;/groupId&gt;
3   &lt;artifactId&gt;druid&lt;/artifactId&gt;
4   &lt;version&gt;1.0.29&lt;/version&gt;
5&lt;/dependency&gt;
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无法统计,&#x27;wall&#x27;用于防火墙
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(&quot;${spring.datasource.url}&quot;)
24    private String dbUrl;
25
26    @Value(&quot;${spring.datasource.username}&quot;)
27    private String username;
28
29    @Value(&quot;${spring.datasource.password}&quot;)
30    private String password;
31
32    @Value(&quot;${spring.datasource.driverClassName}&quot;)
33    private String driverClassName;
34
35    @Value(&quot;${spring.datasource.initialSize}&quot;)
36    private int initialSize;
37
38    @Value(&quot;${spring.datasource.minIdle}&quot;)
39    private int minIdle;
40
41    @Value(&quot;${spring.datasource.maxActive}&quot;)
42    private int maxActive;
43
44    @Value(&quot;${spring.datasource.maxWait}&quot;)
45    private int maxWait;
46
47    @Value(&quot;${spring.datasource.timeBetweenEvictionRunsMillis}&quot;)
48    private int timeBetweenEvictionRunsMillis;
49
50    @Value(&quot;${spring.datasource.minEvictableIdleTimeMillis}&quot;)
51    private int minEvictableIdleTimeMillis;
52
53    @Value(&quot;${spring.datasource.validationQuery}&quot;)
54    private String validationQuery;
55
56    @Value(&quot;${spring.datasource.testWhileIdle}&quot;)
57    private boolean testWhileIdle;
58
59    @Value(&quot;${spring.datasource.testOnBorrow}&quot;)
60    private boolean testOnBorrow;
61
62    @Value(&quot;${spring.datasource.testOnReturn}&quot;)
63    private boolean testOnReturn;
64
65    @Value(&quot;${spring.datasource.poolPreparedStatements}&quot;)
66    private boolean poolPreparedStatements;
67
68    @Value(&quot;${spring.datasource.maxPoolPreparedStatementPerConnectionSize}&quot;)
69    private int maxPoolPreparedStatementPerConnectionSize;
70
71    @Value(&quot;${spring.datasource.filters}&quot;)
72    private String filters;
73
74    @Value(&quot;{spring.datasource.connectionProperties}&quot;)
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(&quot;druid configuration initialization filter&quot;, 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(), &quot;/druid/*&quot;);
25        /** 初始化参数配置,initParams**/
26        //白名单
27        bean.addInitParameter(&quot;allow&quot;, &quot;127.0.0.1&quot;);//多个ip逗号隔开
28        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
29        //bean.addInitParameter(&quot;deny&quot;, &quot;192.168.1.110&quot;);
30        //登录查看信息的账号密码.
31        bean.addInitParameter(&quot;loginUsername&quot;, &quot;admin&quot;);
32        bean.addInitParameter(&quot;loginPassword&quot;, &quot;123456&quot;);
33        //是否能够重置数据.
34        bean.addInitParameter(&quot;resetEnable&quot;, &quot;false&quot;);
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(&quot;/*&quot;);
47        //添加不需要忽略的格式信息.
48        bean.addInitParameter(&quot;exclusions&quot;,&quot;*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*&quot;);
49        return bean;
50    }
51
52}
53

四:启动服务器,访问数据连接池管理页面

打开浏览器,输入**http://localhost:8080/druid    **出现以下页面,为配置成功

账号为admin,密码为123456

可以查看数据源,SQL监控,URL监控等信息

 

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++异常

2022-1-11 12:36:11

安全技术

springboot集成druid、mybatis以及pagehelper

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索