** straightforward, the codes, the opening instant **
- *** yml contents of the configuration file ***
1 # data source configuration 2 Spring: . 3 DataSource: . 4 type: com.alibaba.druid.pool.DruidDataSource . 5 driverClassName: com.mysql.cj.jdbc.Driver . 6 Druid: . 7 # master database data source . 8 Master: . 9 URL: JDBC: MySQL: // **** 10 username: **** . 11 password: **** 12 is # library from data source 13 is Slave: 14 # switch from a data source / off by default 15 Enabled:to false 16 URL: . 17 username: 18 is password: . 19 # initial connection number 20 is initialSize:. 5 21 is the number of # minimum connection pool 22 is minIdle: 10 23 is the number of # maximum connection pool 24 for maxActive: 20 is 25 # configuration acquiring connection waiting timeout 26 is maxWait : 60000 27 # intervals the frequency of such detection, an idle connection is detected to be closed in milliseconds 28 timeBetweenEvictionRunsMillis: 60000 29 # arranged a connection pool survival minimum time in milliseconds 30 minEvictableIdleTimeMillis: 300000 31 is # arranged a connection pool maximum survival time in milliseconds 32 maxEvictableIdleTimeMillis: 900000 33 is # configuration detecting whether a valid connection 34 is validationQuery: the SELECT. 1 the FROM the DUAL 35 testWhileIdle: to true 36 testOnBorrow: to false 37 [ testOnReturn: to false 38 is webStatFilter: 39 Enabled: to true 40 statViewServlet: 41 is Enabled: to true 42 is # White list, not fill it allows all access 43 is the allow: 44 is URL-pattern: / Monitor / Druid / * 45 filter: 46 is STAT: 47 Enabled: to true 48 # slow SQL record 49 log-SLOW-SQL: to true 50 SLOW of millis--sql: 1000 51 is Merge-SQL: to true 52 is Wall: 53 is config: 54 is Multi-Statement-the allow: to true
- *** *** DruidConfig file
1 import java.io.IOException; 2 import java.util.HashMap; 3 import java.util.Map; 4 import javax.servlet.Filter; 5 import javax.servlet.FilterChain; 6 import javax.servlet.ServletException; 7 import javax.servlet.ServletRequest; 8 import javax.servlet.ServletResponse; 9 import javax.sql.DataSource; 10 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 11 import org.springframework.boot.context.properties.ConfigurationProperties; 12 import org.springframework.boot.web.servlet.FilterRegistrationBean; 13 import org.springframework.context.annotation.Bean; 14 import org.springframework.context.annotation.Configuration; 15 import org.springframework.context.annotation.Primary; 16 import com.alibaba.druid.pool.DruidDataSource; 17 import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; 18 import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; 19 import com.alibaba.druid.util.Utils; 20 import com.heater.common.enums.DataSourceType; 21 import com.heater.framework.config.properties.DruidProperties; 22 import com.heater.framework.datasource.DynamicDataSource; 23 24 /** 25 * druid 配置多数据源 26 */ 27 @Configuration 28 public class DruidConfig 29 { 30 @Bean 31 @ConfigurationProperties("spring.datasource.druid.master") 32 public DataSource masterDataSource(DruidProperties druidProperties) 33 { 34 DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); 35 return druidProperties.dataSource(dataSource); 36 } 37 38 @Bean 39 @ConfigurationProperties("spring.datasource.druid.slave") 40 @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") 41 public DataSource slaveDataSource(DruidProperties druidProperties) 42 { 43 DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); 44 return druidProperties.dataSource(dataSource); 45 } 46 47 @Bean(name = "dynamicDataSource") 48 @Primary 49 public 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); 54 return new DynamicDataSource(masterDataSource, targetDataSources); 55 } 56 57 /** 58 * 去除监控页面底部的广告 59 */ 60 @SuppressWarnings({ "rawtypes", "unchecked" }) 61 @Bean 62 @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true") 63 public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) 64 { 65 //Get web page monitoring parameters 66 DruidStatProperties.StatViewServlet config = properties.getStatViewServlet (); 67 // extract common.js configuration path 68 ! Config.getUrlPattern String pattern = () = null ? Config.getUrlPattern (): "/ Druid / * " ; 69 String commonJsPattern = pattern.replaceAll (" \\ * "," JS / common.js " ); 70 Final String filePath =" Support / HTTP / Resources / JS / common.js " ; 71 // create filter filtering 72 the filter filter = new new the filter () 73 is { 74 @Override 75 public void init(javax.servlet.FilterConfig filterConfig) throws ServletException 76 { 77 } 78 @Override 79 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 80 throws IOException, ServletException 81 { 82 chain.doFilter(request, response); 83 // 重置缓冲区,响应头不会被重置 84 response.resetBuffer(); 85 // 获取common.js 86 Text = String Utils.readFromResource (filePath); 87 // regular Alternatively banner, removing the bottom of the advertisement information 88 text = text.replaceAll ( "<Banner A * \.?"> </a> a "," " ); 89 text = text.replaceAll (" Powered shrek.wang </a> * ",". "? ); 90 response.getWriter () Write (text);. 91 is } 92 @Override 93 public void the destroy ( ) 94 { 95 } 96 }; 97 FilterRegistrationBean registrationBean = new FilterRegistrationBean(); 98 registrationBean.setFilter(filter); 99 registrationBean.addUrlPatterns(commonJsPattern); 100 return registrationBean; 101 } 102 }
- *** *** DruidProperties file
1 import org.springframework.beans.factory.annotation.Value; 2 import org.springframework.context.annotation.Configuration; 3 import com.alibaba.druid.pool.DruidDataSource; 4 5 /** 6 * druid 配置属性 7 */ 8 @Configuration 9 public class DruidProperties 10 { 11 @Value("${spring.datasource.druid.initialSize}") 12 private int initialSize; 13 14 @Value("${spring.datasource.druid.minIdle}") 15 private int minIdle; 16 17 @Value("${spring.datasource.druid.maxActive}") 18 private int maxActive; 19 20 @Value("${spring.datasource.druid.maxWait}") 21 private int maxWait; 22 23 @Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}") 24 private int timeBetweenEvictionRunsMillis; 25 26 @Value("${spring.datasource.druid.minEvictableIdleTimeMillis}") 27 private int minEvictableIdleTimeMillis; 28 29 @Value("${spring.datasource.druid.maxEvictableIdleTimeMillis}") 30 private int maxEvictableIdleTimeMillis; 31 32 @Value("${spring.datasource.druid.validationQuery}") 33 private String validationQuery; 34 35 @Value("${spring.datasource.druid.testWhileIdle}") 36 private boolean testWhileIdle; 37 38 @Value("${spring.datasource.druid.testOnBorrow}") 39 private boolean testOnBorrow; 40 41 is @Value ( "spring.datasource.druid.testOnReturn $ {}" ) 42 is Private Boolean testOnReturn; 43 is 44 is public DruidDataSource the dataSource (DruidDataSource DataSource) 45 { 46 is / ** configuration initial size, minimum, maximum * / 47 datasource.setInitialSize (initialSize); 48 datasource.setMaxActive (for maxActive); 49 datasource.setMinIdle (minIdle); 50 51 is / ** configuration acquiring connection waiting timeout time * / 52 is datasource.setMaxWait (maxWait); 53 is 54 is / **Intervals the frequency of such detection, an idle connection is detected to be closed, in milliseconds * / 55 datasource.setTimeBetweenEvictionRunsMillis (timeBetweenEvictionRunsMillis); 56 is 57 is / ** arranged a connection pool minimum, maximum survival time in milliseconds * / 58 datasource.setMinEvictableIdleTimeMillis (minEvictableIdleTimeMillis); 59 datasource.setMaxEvictableIdleTimeMillis (maxEvictableIdleTimeMillis); 60 61 is / ** 62 is * used for detecting whether a valid connection sql, a query is required, common select 'x'. If validationQuery is null, testOnBorrow, testOnReturn, testWhileIdle will not work. 63 is * / 64 datasource.setValidationQuery (validationQuery); 65 / **Recommended configuration is true, does not affect performance, and ensure safety. Detecting when the application connection, if the idle time is greater than timeBetweenEvictionRunsMillis, performs detection validationQuery connection is valid. * / 66 datasource.setTestWhileIdle (testWhileIdle); 67 / ** execute validationQuery detect whether the connection is valid when connecting application, made this configuration can degrade performance. * / 68 datasource.setTestOnBorrow (testOnBorrow); 69 / ** performed validationQuery detecting whether a valid connection is connected to return, to do this configuration can reduce performance. * / 70 datasource.setTestOnReturn (testOnReturn); 71 is return DataSource; 72 } 73 is }