Spring配置Druid详细说明**简单直接,上代码,开封即⾷**
***yml⽂件内容配置***
1 # 数据源配置
2 spring:
3 datasource:
4 type: com.alibaba.druid.pool.DruidDataSource
5 driverClassName: sql.cj.jdbc.Driver
6 druid:
7 # 主库数据源
8 master:
9 url: jdbc:mysql://****
10 username: ****
11 password: ****
12 # 从库数据源
13 slave:
14 # 从数据源开关/默认关闭
15 enabled: false
16 url:
17 username:
18 password:
19 # 初始连接数
20 initialSize: 5
21 # 最⼩连接池数量
22 minIdle: 10
23 # 最⼤连接池数量
24 maxActive: 20
25 # 配置获取连接等待超时的时间
26 maxWait: 60000
27 # 配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒
28 timeBetweenEvictionRunsMillis: 60000
29 # 配置⼀个连接在池中最⼩⽣存的时间,单位是毫秒
30 minEvictableIdleTimeMillis: 300000
31 # 配置⼀个连接在池中最⼤⽣存的时间,单位是毫秒
32 maxEvictableIdleTimeMillis: 900000
33 # 配置检测连接是否有效
34 validationQuery: SELECT 1 FROM DUAL
35 testWhileIdle: true
36 testOnBorrow: false
37 testOnReturn: false
38 webStatFilter:
39 enabled: true
40 statViewServlet:
41 enabled: true
42 # 设置⽩名单,不填则允许所有访问
43 allow:
44 url-pattern: /monitor/druid/*
45 filter:
46 stat:
47 enabled: true
48 # 慢SQL记录
49 log-slow-sql: true
50 slow-sql-millis: 1000
51 merge-sql: true
52 wall:
53 config:
54 multi-statement-allow: true
***DruidConfig⽂件***
1import java.io.IOException;
2import java.util.HashMap;
3import java.util.Map;
4import javax.servlet.Filter;
5import javax.servlet.FilterChain;
6import javax.servlet.ServletException;
7import javax.servlet.ServletRequest;
8import javax.servlet.ServletResponse;
9import javax.sql.DataSource;
10import org.springframework.dition.ConditionalOnProperty;
11import org.t.properties.ConfigurationProperties;
12import org.springframework.boot.web.servlet.FilterRegistrationBean;
13import t.annotation.Bean;
14import t.annotation.Configuration;
15import t.annotation.Primary;
16import com.alibaba.druid.pool.DruidDataSource;
17import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
18import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
19import com.alibaba.druid.util.Utils;
20import ums.DataSourceType;
21import com.fig.properties.DruidProperties;
22import com.heater.framework.datasource.DynamicDataSource;
23
24/**
25 * druid 配置多数据源
26*/
27 @Configuration
28public class DruidConfig
spring framework网络系统参数29 {
30 @Bean
31 @ConfigurationProperties("spring.datasource.druid.master")
32public DataSource masterDataSource(DruidProperties druidProperties)
33 {
34 DruidDataSource dataSource = ate().build();
35return druidProperties.dataSource(dataSource);
36 }
37
38 @Bean
39 @ConfigurationProperties("spring.datasource.druid.slave")
40 @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") 41public DataSource slaveDataSource(DruidProperties druidProperties)
42 {
43 DruidDataSource dataSource = ate().build();
44return druidProperties.dataSource(dataSource);
45 }
46
47 @Bean(name = "dynamicDataSource")
48 @Primary
49public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource)
50 {
51 Map<Object, Object> targetDataSources = new HashMap<>();
52 targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
53 targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
54return new DynamicDataSource(masterDataSource, targetDataSources);
55 }
56
57/**
58 * 去除监控页⾯底部的⼴告
59*/
60 @SuppressWarnings({ "rawtypes", "unchecked" })
61 @Bean
62 @ConditionalOnProperty(name = "spring.datasource.abled", havingValue = "true") 63public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
64 {
65// 获取web监控页⾯的参数
66 DruidStatProperties.StatViewServlet config = StatViewServlet();
67// 提取common.js的配置路径
68 String pattern = UrlPattern() != null ? UrlPattern() : "/druid/*";
69 String commonJsPattern = placeAll("\\*", "js/common.js");
70final String filePath = "support/http/resources/js/common.js";
71// 创建filter进⾏过滤
72 Filter filter = new Filter()
73 {
74 @Override
75public void init(javax.servlet.FilterConfig filterConfig) throws ServletException
76 {
77 }
78 @Override
79public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
80throws IOException, ServletException
81 {
82 chain.doFilter(request, response);
83// 重置缓冲区,响应头不会被重置
84 setBuffer();
85// 获取common.js
86 String text = adFromResource(filePath);
87// 正则替换banner, 除去底部的⼴告信息
88 text = placeAll("<a.*?banner\"></a><br/>", "");
89 text = placeAll("powered.*?shrek.wang</a>", "");
90 Writer().write(text);
91 }
92 @Override
93public void destroy()
94 {
95 }
96 };
97 FilterRegistrationBean registrationBean = new FilterRegistrationBean();
98 registrationBean.setFilter(filter);
99 registrationBean.addUrlPatterns(commonJsPattern);
100return registrationBean;
101 }
102 }
***DruidProperties⽂件***
1import org.springframework.beans.factory.annotation.Value;
2import t.annotation.Configuration;
3import com.alibaba.druid.pool.DruidDataSource;
4
5/**
6 * druid 配置属性
7*/
8 @Configuration
9public class DruidProperties
10 {
11 @Value("${spring.datasource.druid.initialSize}")
12private int initialSize;
13
14 @Value("${spring.datasource.druid.minIdle}")
15private int minIdle;
16
17 @Value("${spring.datasource.druid.maxActive}")
18private int maxActive;
19
20 @Value("${spring.datasource.druid.maxWait}")
21private int maxWait;
22
23 @Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
24private int timeBetweenEvictionRunsMillis;
25
26 @Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
27private int minEvictableIdleTimeMillis;
28
29 @Value("${spring.datasource.druid.maxEvictableIdleTimeMillis}")
30private int maxEvictableIdleTimeMillis;
31
32 @Value("${spring.datasource.druid.validationQuery}")
33private String validationQuery;
34
35 @Value("${spring.stWhileIdle}")
36private boolean testWhileIdle;
37
38 @Value("${spring.stOnBorrow}")
39private boolean testOnBorrow;
40
41 @Value("${spring.stOnReturn}")
42private boolean testOnReturn;
43
44public DruidDataSource dataSource(DruidDataSource datasource)
45 {
46/** 配置初始化⼤⼩、最⼩、最⼤ */
47 datasource.setInitialSize(initialSize);
48 datasource.setMaxActive(maxActive);
49 datasource.setMinIdle(minIdle);
50
51/** 配置获取连接等待超时的时间 */
52 datasource.setMaxWait(maxWait);
53
54/** 配置间隔多久才进⾏⼀次检测,检测需要关闭的空闲连接,单位是毫秒 */
55 datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
56
57/** 配置⼀个连接在池中最⼩、最⼤⽣存的时间,单位是毫秒 */
58 datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
59 datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis);
60
61/**
62 * ⽤来检测连接是否有效的sql,要求是⼀个查询语句,常⽤select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作⽤。
63*/
64 datasource.setValidationQuery(validationQuery);
65/** 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间⼤于timeBetweenEvictionRunsMillis,执⾏validationQuery检测连接是否有效。 */ 66 datasource.setTestWhileIdle(testWhileIdle);
67/** 申请连接时执⾏validationQuery检测连接是否有效,做了这个配置会降低性能。 */
68 datasource.setTestOnBorrow(testOnBorrow);
69/** 归还连接时执⾏validationQuery检测连接是否有效,做了这个配置会降低性能。 */
70 datasource.setTestOnReturn(testOnReturn);
71return datasource;
72 }
73 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论