SpringBoot配置多数据源(druid)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_22271479/article/details/82691228

分析

spring本身是支持多数据源动态切换的,AbstractRoutingDataSource这个抽象类就是spring提供的一个数据源路由的一个入口,该抽象类暴露了一个determineCurrentLookupKey()的方法,该方法返回值是Object,该返回值作为key去取Map中的DataSource。

  • AbstractRoutingDataSource
    • getConnection()
      • determineTargetDataSource() 从Map中通过key获取DataSource
        • determineCurrentLookupKey() 获取key

1.创建一个线程线程安全的Holder来切换Key

public class DynamicDataSourceHolder {

    public static ThreadLocal<DataSourceKey> keyThreadLocal = new ThreadLocal<>();

    public static void clear(){
        keyThreadLocal.remove();
    }

    public static void set(DataSourceKey key){
        keyThreadLocal.set(key);
    }

    public static DataSourceKey get(){
        DataSourceKey key = keyThreadLocal.get();
        return null==key?DataSourceKey.DB:key;
    }

}

2.继承AbstractRoutingDataSource设置key

public class DynamicRoutingDataSource extends AbstractRoutingDataSource{
    @Nullable
    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceHolder.get();
    }
}

3.编写Config来初始化DataSource

@Configuration
public class DynamicDatasourceConfig {

    @Autowired
    ApplicationContext applicationContext;

    @Bean("druid_db")//必须加上该注解,否则 @ConfigurationProperties无效
    @ConfigurationProperties(prefix = "dynamic-datasource.druid-datasources.db")
    public DataSource db(StandardEnvironment env){
        DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build();
        return common(env,druidDataSource);
    }
    @Bean("druid_db1")
    @ConfigurationProperties(prefix = "dynamic-datasource.druid-datasources.db1")
    public DataSource db1(StandardEnvironment env){
        DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build();
        return common(env,druidDataSource);
    }
    @Bean("druid_db2")
    @ConfigurationProperties(prefix = "dynamic-datasource.druid-datasources.db2")
    public DataSource db2(StandardEnvironment env){
        DruidDataSource druidDataSource = DruidDataSourceBuilder.create().build();
        return common(env,druidDataSource);
    }

    @Bean("dataSource")
    public DataSource dynamicDataSource(StandardEnvironment env) {
        DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
        Map<Object,Object> map = new HashMap<>();
        map.put(DataSourceKey.DB,applicationContext.getBean("druid_db"));
        map.put(DataSourceKey.DB1,applicationContext.getBean("druid_db1"));
        map.put(DataSourceKey.DB2,applicationContext.getBean("druid_db2"));
        dynamicRoutingDataSource.setDefaultTargetDataSource(applicationContext.getBean("druid_db"));
        dynamicRoutingDataSource.setTargetDataSources(map);
        return dynamicRoutingDataSource;
    }

    public DataSource common(StandardEnvironment env, DruidDataSource druidDataSource){
        Properties properties = new Properties();
        PropertySource<?> appProperties =  env.getPropertySources().get("applicationConfig: [classpath:/application.yml]");
        Map<String,Object> source = (Map<String, Object>) appProperties.getSource();
        properties.putAll(source);
        druidDataSource.configFromPropety(properties);
        return druidDataSource;
    }

}

4.通过aop动态去切换key

该注解确定的该方法使用哪一个数据源

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TargetDataSource {
    DataSourceKey value() default DataSourceKey.DB;
}

数据源的key

public enum  DataSourceKey {
    DB,DB1,DB2,DB3,DB4;
}

一个是拦截特定的方法和拦截有TargetDataSource注解的方法去动态切换

@Aspect
@Component
@Order(-100)//提高优先级
public class DynamicDataSourceAop {

    @Pointcut(value = "execution(public * com.yzz.boot..*.mapper ..*.*(..))")
    public void defaultDataSource(){}

    @Before(value = "defaultDataSource()")
    public void setDefaultDataSource(){

    }
    //没有注解,就选择默认的数据源
    @Before(value = "@annotation(dataSource)&&defaultDataSource()")
    public void setDynamicDataSource(TargetDataSource dataSource){
        if (null == dataSource){
            System.err.println("设置默认数据源"+DataSourceKey.DB);
            DynamicDataSourceHolder.set(DataSourceKey.DB);
        }else {
            System.err.println("切换数据源"+dataSource.value());
            DynamicDataSourceHolder.set(dataSource.value());
        }
    }
//清除该线程当前的数据
    @After(value = "defaultDataSource()&&@annotation(com.yzz.boot.dyConfig.ann.TargetDataSource)")
    public void clean(){
        System.err.println("清除当前线程的数据源");
        DynamicDataSourceHolder.clear();
    }
}

5.Mapper通过方法上通过注解去动态选择数据源

@Mapper
public interface TestMapper {
    @TargetDataSource(value = DataSourceKey.DB1)
    @Select("select * from t_es_test limit 5")
    List<HashMap> getAll();

    @TargetDataSource(value = DataSourceKey.DB2)
    @Select("select * from t_es_test limit 5")
    List<HashMap> getAll1();
}

6.程序入口去除默认的连接池的配置类

//去除默认的连接池
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
//扫描响应的包
@MapperScan("com.yzz.boot.*.mapper")
public class BootApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class);
    }
}

7.配置文件

spring:
  profiles:
    active: system

dynamic-datasource:
  druid:
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 30000
    minIdle: 10
    maxIdle: 15
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    maxOpenPreparedStatements: 20
    removeAbandoned: true
    removeAbandonedTimeout: 1800
    logAbandoned: true
  druid-datasources:
    db:
      url: jdbc:mysql://192.168.1.12:3306/yzz
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
    db1:
      url: jdbc:mysql://192.168.1.12:3306/yzz
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
    db2:
      url: jdbc:mysql://192.168.1.12:3306/yzz
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver


jdbc-conn:
      url: jdbc:mysql://192.168.1.12:3306/yzz
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver

总结

通过注解来动态原则key,spring的DataSource路由选择器通过key从之前设置进去的DataSource Map中获取响应的DataSource,从而达到了动态切换的目的。通过ThreadLocal来存储key,保证了数=数据在多线程环境下的正确性。

猜你喜欢

转载自blog.csdn.net/qq_22271479/article/details/82691228