importjava.util.HashMap;importjava.util.Map;importjavax.annotation.Resource;importjavax.sql.DataSource;importorg.springframework.boot.web.servlet.FilterRegistrationBean;importorg.springframework.boot.web.servlet.ServletRegistrationBean;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.DependsOn;importorg.springframework.context.annotation.Primary;importcom.alibaba.druid.pool.DruidDataSource;importcom.alibaba.druid.support.http.StatViewServlet;importcom.alibaba.druid.support.http.WebStatFilter;/*** 数据源配置类
*
*@authorLynch*/@Configurationpublic classDataSourceComponent {
@ResourceprivateMasterDataSourceConfig masterDataSourceConfig;
@ResourceprivateSlaveDataSourceConfig slaveDataSourceConfig;
@Bean(name= "master")publicDataSource masterDataSource() {
DruidDataSource dataSource= newDruidDataSource();
dataSource.setUrl(masterDataSourceConfig.getUrl());
dataSource.setUsername(masterDataSourceConfig.getUsername());
dataSource.setPassword(masterDataSourceConfig.getPassword());
dataSource.setDriverClassName(masterDataSourceConfig.getDriverClassName());returndataSource;
}
@Bean(name= "slave")publicDataSource slaveDataSource() {
DruidDataSource dataSource= newDruidDataSource();
dataSource.setUrl(slaveDataSourceConfig.getUrl());
dataSource.setUsername(slaveDataSourceConfig.getUsername());
dataSource.setPassword(slaveDataSourceConfig.getPassword());
dataSource.setDriverClassName(slaveDataSourceConfig.getDriverClassName());returndataSource;
}
@Primary//不加这个会报错。
@DependsOn({ "master", "slave"}) //解决数据库循环依赖问题
@Bean(name= "multiDataSource")publicMultiRouteDataSource exampleRouteDataSource() {
MultiRouteDataSource multiDataSource= newMultiRouteDataSource();
Map targetDataSources = new HashMap<>();targetDataSources.put("master", masterDataSource());
targetDataSources.put("slave", slaveDataSource());
multiDataSource.setTargetDataSources(targetDataSources);
multiDataSource.setDefaultTargetDataSource(masterDataSource());returnmultiDataSource;
}/*** 注册ServletRegistrationBean
*
*@return
*/@BeanpublicServletRegistrationBean druidServlet() {
ServletRegistrationBean reg= newServletRegistrationBean();
reg.setServlet(newStatViewServlet());
reg.addUrlMappings("/druid/*");
reg.addInitParameter("allow", ""); //白名单 return reg;
reg.addInitParameter("loginUsername", "duan");
reg.addInitParameter("loginPassword", "123456");
reg.addInitParameter("resetEnable", "false");returnreg;
}/*** 注册FilterRegistrationBean
*
*@return
*/@BeanpublicFilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean= newFilterRegistrationBean();
filterRegistrationBean.setFilter(newWebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
filterRegistrationBean.addInitParameter("profileEnable", "true");
filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");
filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");
filterRegistrationBean.addInitParameter("DruidWebStatFilter", "/*");returnfilterRegistrationBean;
}
}