| | |
| | | package com.gkhy.safePlatform.config.database; |
| | | |
| | | import com.alibaba.druid.filter.stat.StatFilter; |
| | | import com.alibaba.druid.support.http.StatViewServlet; |
| | | import com.alibaba.druid.support.http.WebStatFilter; |
| | | import com.alibaba.druid.wall.WallConfig; |
| | | import com.alibaba.druid.wall.WallFilter; |
| | | import com.atomikos.icatch.jta.UserTransactionImp; |
| | | import com.atomikos.icatch.jta.UserTransactionManager; |
| | | import com.mysql.cj.jdbc.MysqlXADataSource; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean; |
| | |
| | | @Configuration |
| | | public class DruidConfig { |
| | | |
| | | @Value("${spring.datasource.type}") |
| | | private String datasourceClass; |
| | | @Autowired |
| | | private DataCourceProperties dataCourceProperties; |
| | | |
| | | @Bean(name = "dsGoalManage") |
| | | @Primary |
| | | @Autowired |
| | | public DataSource dsGoalManage(Environment env) { |
| | | AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); |
| | | final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource(); |
| | | Properties prop = build(env, "spring.datasource.goalmanage."); |
| | | // ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource"); |
| | | ds.setXaDataSourceClassName(datasourceClass); |
| | | mysqlXADataSource.setUrl(dataCourceProperties.getGoalmanage().getUrl()); |
| | | mysqlXADataSource.setUser(dataCourceProperties.getGoalmanage().getUsername()); |
| | | mysqlXADataSource.setPassword(dataCourceProperties.getGoalmanage().getPassword()); |
| | | final AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); |
| | | ds.setXaDataSource(mysqlXADataSource); |
| | | ds.setUniqueResourceName("goalmanage"); |
| | | ds.setPoolSize(2); |
| | | ds.setPoolSize(dataCourceProperties.getGoalmanage().getInitialSize()); |
| | | ds.setMinPoolSize(dataCourceProperties.getGoalmanage().getMinIdle()); |
| | | ds.setMaxPoolSize(dataCourceProperties.getGoalmanage().getMaxActive()); |
| | | ds.setXaProperties(prop); |
| | | ds.setMaxIdleTime(3600000); |
| | | ds.setMaxLifetime(3000000); |
| | | ds.setMaxIdleTime(3600); |
| | | ds.setMaxLifetime(3000); |
| | | ds.setTestQuery(dataCourceProperties.getGoalmanage().getValidationQuery()); |
| | | return ds; |
| | | |
| | | } |
| | | |
| | | |
| | |
| | | @Primary |
| | | @Bean(name = "dsEmergency") |
| | | public AtomikosDataSourceBean dsEmergency(Environment env) { |
| | | AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); |
| | | final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource(); |
| | | Properties prop = build(env, "spring.datasource.emergency."); |
| | | // ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource"); |
| | | ds.setXaDataSourceClassName(datasourceClass); |
| | | mysqlXADataSource.setUrl(dataCourceProperties.getEmergency().getUrl()); |
| | | mysqlXADataSource.setUser(dataCourceProperties.getEmergency().getUsername()); |
| | | mysqlXADataSource.setPassword(dataCourceProperties.getEmergency().getPassword()); |
| | | final AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); |
| | | ds.setXaDataSource(mysqlXADataSource); |
| | | ds.setUniqueResourceName("emergency"); |
| | | ds.setPoolSize(2); |
| | | ds.setPoolSize(dataCourceProperties.getEmergency().getInitialSize()); |
| | | ds.setMinPoolSize(dataCourceProperties.getEmergency().getMinIdle()); |
| | | ds.setMaxPoolSize(dataCourceProperties.getEmergency().getMaxActive()); |
| | | ds.setXaProperties(prop); |
| | | ds.setMaxIdleTime(3600000); |
| | | ds.setMaxLifetime(3000000); |
| | | ds.setMaxIdleTime(3600); |
| | | ds.setMaxLifetime(3000); |
| | | ds.setTestQuery(dataCourceProperties.getEmergency().getValidationQuery()); |
| | | return ds; |
| | | } |
| | | |
| | |
| | | @Primary |
| | | @Bean(name = "dsIncidentManage") |
| | | public AtomikosDataSourceBean dsIncidentManage(Environment env) { |
| | | AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); |
| | | final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource(); |
| | | Properties prop = build(env, "spring.datasource.incidentmanage."); |
| | | ds.setXaDataSourceClassName(datasourceClass); |
| | | // ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource"); |
| | | mysqlXADataSource.setUrl(dataCourceProperties.getIncidentmanage().getUrl()); |
| | | mysqlXADataSource.setUser(dataCourceProperties.getIncidentmanage().getUsername()); |
| | | mysqlXADataSource.setPassword(dataCourceProperties.getIncidentmanage().getPassword()); |
| | | final AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); |
| | | ds.setXaDataSource(mysqlXADataSource); |
| | | ds.setUniqueResourceName("incidentmanage"); |
| | | ds.setPoolSize(2); |
| | | ds.setPoolSize(dataCourceProperties.getIncidentmanage().getInitialSize()); |
| | | ds.setMinPoolSize(dataCourceProperties.getIncidentmanage().getMinIdle()); |
| | | ds.setMaxPoolSize(dataCourceProperties.getIncidentmanage().getMaxActive()); |
| | | ds.setXaProperties(prop); |
| | | ds.setMaxIdleTime(3600000); |
| | | ds.setMaxLifetime(3000000); |
| | | ds.setMaxIdleTime(3600); |
| | | ds.setMaxLifetime(3000); |
| | | ds.setTestQuery(dataCourceProperties.getIncidentmanage().getValidationQuery()); |
| | | return ds; |
| | | } |
| | | |
| | |
| | | @Primary |
| | | @Bean(name = "dsEquipment") |
| | | public AtomikosDataSourceBean dsEquipment(Environment env) { |
| | | AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); |
| | | final MysqlXADataSource mysqlXADataSource = new MysqlXADataSource(); |
| | | Properties prop = build(env, "spring.datasource.equipment."); |
| | | ds.setXaDataSourceClassName(datasourceClass); |
| | | // ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource"); |
| | | mysqlXADataSource.setUrl(dataCourceProperties.getEquipment().getUrl()); |
| | | mysqlXADataSource.setUser(dataCourceProperties.getEquipment().getUsername()); |
| | | mysqlXADataSource.setPassword(dataCourceProperties.getEquipment().getPassword()); |
| | | final AtomikosDataSourceBean ds = new AtomikosDataSourceBean(); |
| | | ds.setXaDataSource(mysqlXADataSource); |
| | | ds.setUniqueResourceName("equipment"); |
| | | ds.setPoolSize(2); |
| | | ds.setPoolSize(dataCourceProperties.getEquipment().getInitialSize()); |
| | | ds.setMinPoolSize(dataCourceProperties.getEquipment().getMinIdle()); |
| | | ds.setMaxPoolSize(dataCourceProperties.getEquipment().getMaxActive()); |
| | | ds.setXaProperties(prop); |
| | | ds.setMaxIdleTime(3600000); |
| | | ds.setMaxLifetime(3000000); |
| | | ds.setMaxIdleTime(3600); |
| | | ds.setMaxLifetime(3000); |
| | | ds.setTestQuery(dataCourceProperties.getEquipment().getValidationQuery()); |
| | | return ds; |
| | | } |
| | | |
| | |
| | | // prop.put("filters", env.getProperty(prefix + "filters")); |
| | | |
| | | return prop; |
| | | } |
| | | |
| | | // @Bean |
| | | public ServletRegistrationBean druidServlet() { |
| | | ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); |
| | | |
| | | //控制台管理用户,加入下面2行 进入druid后台就需要登录 |
| | | //servletRegistrationBean.addInitParameter("loginUsername", "admin"); |
| | | //servletRegistrationBean.addInitParameter("loginPassword", "admin"); |
| | | return servletRegistrationBean; |
| | | } |
| | | |
| | | // @Bean |
| | | public FilterRegistrationBean filterRegistrationBean() { |
| | | FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); |
| | | filterRegistrationBean.setFilter(new WebStatFilter()); |
| | | filterRegistrationBean.addUrlPatterns("/*"); |
| | | filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); |
| | | filterRegistrationBean.addInitParameter("profileEnable", "true"); |
| | | return filterRegistrationBean; |
| | | } |
| | | |
| | | // @Bean |
| | | public StatFilter statFilter(){ |
| | | StatFilter statFilter = new StatFilter(); |
| | | statFilter.setLogSlowSql(true); //slowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢。 |
| | | statFilter.setMergeSql(true); //SQL合并配置 |
| | | statFilter.setSlowSqlMillis(1000);//slowSqlMillis的缺省值为3000,也就是3秒。 |
| | | return statFilter; |
| | | } |
| | | |
| | | // @Bean |
| | | public WallFilter wallFilter(){ |
| | | WallFilter wallFilter = new WallFilter(); |
| | | //允许执行多条SQL |
| | | WallConfig config = new WallConfig(); |
| | | config.setMultiStatementAllow(true); |
| | | wallFilter.setConfig(config); |
| | | return wallFilter; |
| | | } |
| | | |
| | | } |