springboot整合Druid数据源

1.引入druid依赖

 

代码如下:

1 <!--druid 依赖 -->
2 <dependency>
3     <groupId>com.alibaba</groupId>
4      <artifactId>druid</artifactId>
5     <version>1.0.18</version>
6 </dependency>

2.在application.yml配置文件中修改默认的数据源配置和连接池信息

 

 

 代码如下:

 1 spring:
 2   datasource:
 3     type: com.alibaba.druid.pool.DruidDataSource
 4     driver-class-name: com.mysql.jdbc.Driver
 5     url: jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF8?autoReconnect=true
 6     username: root
 7     password: root
 8     initialSize: 3
 9     maxActive: 50
10     minIdle: 3
11     #获取连接等待超时的时间
12     maxWait: 600000
13     #超过时间限制是否回收
14     removeAbandoned: true
15     #超过时间限制多长
16     removeAbandonedTimeout: 180
17     #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
18     timeBetweenEvictionRunsMillis: 600000
19     #配置一个连接在池中最小生存的时间,单位是毫秒
20     minEvictableIdleTimeMillis: 300000
21     #用来检测连接是否有效的sql,要求是一个查询语句
22     validationQuery: SELECT 1 FROM DUAL
23     #申请连接的时候检测
24     testWhileIdle: true
25     #申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能
26     testOnBorrow: false
27     #归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能
28     testOnReturn: false
29     #打开PSCache,并且指定每个连接上PSCache的大小
30     poolPreparedStatements: true
31     maxPoolPreparedStatementPerConnectionSize: 50
32     #属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
33     #监控统计用的filter:stat 日志用的filter:log4j 防御SQL注入的filter:wall
34     filters: stat

3.druid的其他属性配置

 

 代码如下:

1 @Configuration
2 public class DruidConfig {
3 
4     @ConfigurationProperties(prefix = "spring.datasource")
5     @Bean
6     public DataSource druid() {
7         return new DruidDataSource();
8     }
9 }

4.使用druid的监控功能

 1 import com.alibaba.druid.pool.DruidDataSource;
 2 import com.alibaba.druid.support.http.StatViewServlet;
 3 import com.alibaba.druid.support.http.WebStatFilter;
 4 import org.springframework.boot.context.properties.ConfigurationProperties;
 5 import org.springframework.boot.web.servlet.FilterRegistrationBean;
 6 import org.springframework.boot.web.servlet.ServletRegistrationBean;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 import javax.sql.DataSource;
10 import java.util.Arrays;
11 import java.util.HashMap;
12 import java.util.Map;
13 
14 @Configuration
15 public class DruidConfig {
16 
17     @ConfigurationProperties(prefix = "spring.datasource")
18     @Bean
19     public DataSource druid() {
20         return new DruidDataSource();
21     }
22 
23     // 配置Druid的监控 
24     //1、配置一个管理后台的Servlet
25     @Bean
26     public ServletRegistrationBean statViewServlet() {
27         ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
28         Map<String, String> initParams = new HashMap<>();
29         initParams.put("loginUsername", "admin");
30         initParams.put("loginPassword", "123456");
31         initParams.put("allow", "");// 默认就是允许所有访问
32         initParams.put("deny", "127.0.0.1");
33         bean.setInitParameters(initParams);
34         return bean;
35     }
36 
37     // 2、配置一个web监控的filter
38     @Bean
39     public FilterRegistrationBean webStatFilter() {
40         FilterRegistrationBean bean = new FilterRegistrationBean();
41         bean.setFilter(new WebStatFilter());
42         Map<String, String> initParams = new HashMap<>();
43         initParams.put("exclusions", "*.js,*.css,/druid/*");
44         bean.setInitParameters(initParams);
45         bean.setUrlPatterns(Arrays.asList("/*"));
46         return bean;
47     }
48 
49 }
posted @ 2020-02-14 17:36  zhuitian  阅读(...)  评论(...编辑  收藏