动态数据源玩起来
作者:互联网
package com.xf.config; import java.sql.SQLException; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import javax.annotation.PostConstruct; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.stat.DruidDataSourceStatManager; @Configuration public class DynamicDataSource extends AbstractRoutingDataSource { @Value("${spring.datasource.druid.url}") String jdbcurl; @Value("${spring.datasource.druid.username}") String username; @Value("${spring.datasource.druid.password}") String password; public Map<Object, Object> dataSources; /** * 每次请求动态请求哪一个数据源 * * @return */ @Override protected Object determineCurrentLookupKey() { return DataSourceHolder.getDataSource(); } /** * 配置文件yml中的默认数据源 * * @return */ // @Bean(name = "defaultDataSource") // @ConfigurationProperties(prefix = "spring.datasource") // public DataSource getDefaultDataSource() { // return DruidDataSourceBuilder.create().build(); // } // public DynamicDataSource() { // // } /** * 添加数据源到目标数据源map中 * * @param datasource */ public void addDataSource(String key, DruidDataSource datasource) { dataSources.put(key, datasource); super.setTargetDataSources(dataSources); super.afterPropertiesSet(); } // 删除数据源 public synchronized boolean delDatasources(String datasourceid) { if (dataSources.containsKey(datasourceid)) { Set<DruidDataSource> druidDataSourceInstances = DruidDataSourceStatManager.getDruidDataSourceInstances(); for (DruidDataSource l : druidDataSourceInstances) { if (datasourceid.equals(l.getName())) { dataSources.remove(datasourceid); DruidDataSourceStatManager.removeDataSource(l); // 将map赋值给父类的TargetDataSources setTargetDataSources(dataSources); // 将TargetDataSources中的连接信息放入resolvedDataSources管理 super.afterPropertiesSet(); return true; } } return false; } else { return false; } } /** * 此处数据库信配置,可以来源于redis等,然后再初始化所有数据源 重点说明:一个DruidDataSource数据源,它里面本身就是线程池了, * 所以我们不需要考虑线程池的问题 * * @param no * @return */ public DataSource druidDataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(jdbcurl); datasource.setUsername(username); datasource.setPassword(password); // datasource.setDriverClassName("com.mysql.cj.jdbc.Driver"); datasource.setInitialSize(5); datasource.setMinIdle(5); datasource.setMaxActive(100); // datasource.setDbType("com.alibaba.druid.pool.DruidDataSource"); datasource.setMaxWait(60000); datasource.setTimeBetweenEvictionRunsMillis(60000); datasource.setMinEvictableIdleTimeMillis(300000); datasource.setValidationQuery("SELECT 1 FROM DUAL"); datasource.setTestWhileIdle(true); datasource.setTestOnBorrow(false); datasource.setTestOnReturn(false); try { // datasource.setFilters("stat,wall,log4j"); datasource.setFilters("wall"); } catch (SQLException e) { e.printStackTrace(); } return datasource; } }
标签:玩起来,return,数据源,datasource,DruidDataSource,import,动态,public 来源: https://www.cnblogs.com/wujf/p/15661550.html