数据源切换

目录

应用场景

疑问

将就吧

数据源管理方案

AbstractRoutingDataSource

使用MyBatis注册多个SqlSessionFactory

使用dynamic-datasource框架


应用场景

疑问

我感觉这个好像没啥应用场景啊,一个服务对应一个数据库不是挺好的嘛,你这个服务还可以访问其他的数据库资源,那不是乱套了嘛

将就吧

就这么用呗,借鉴一下这个思想

数据源管理方案

AbstractRoutingDataSource

使用Spring提供的AbstractRoutingDataSource,这种方式的核心是使用Spring提供的AbstractRoutingDataSource抽象类,注入多个数据源

@Component
@Primary   // 将该Bean设置为主要注入Bean
public class DynamicDataSource extends AbstractRoutingDataSource {

    // 当前使用的数据源标识
    public static ThreadLocal<String> name=new ThreadLocal<>();

    // 写
    @Autowired
    DataSource dataSource1;
    // 读
    @Autowired
    DataSource dataSource2;


    // 返回当前数据源标识
    @Override
    protected Object determineCurrentLookupKey() {
        return name.get();

    }

    @Override
    public void afterPropertiesSet() {

        // 为targetDataSources初始化所有数据源
        Map<Object, Object> targetDataSources=new HashMap<>();
        targetDataSources.put("W",dataSource1);
        targetDataSources.put("R",dataSource2);

        super.setTargetDataSources(targetDataSources);

        // 为defaultTargetDataSource 设置默认的数据源
        super.setDefaultTargetDataSource(dataSource1);

        super.afterPropertiesSet();
    }
}

将自己实现的DynamicDataSource注册成为默认的DataSource实例后,只需要在每次使用 DataSource时,提前改变一下其中的name标识,就可以快速切换数据源。

@Component
@Aspect
public class DynamicDataSourceAspect implements Ordered {

    // 前置
    // 在每个访问数据库的方法执行前执行。
    @Before("within(com.tuling.dynamic.datasource.service.impl.*) && @annotation(wr)")
    public void before(JoinPoint point, WR wr){
        String name = wr.value();
        DynamicDataSource.name.set(name);

        System.out.println(name);
    }

    @Override
    public int getOrder() {
        return 0;
    }

    // 环绕通知
}

处理流程

1. 首先是通过接口进行访问,准备读数据库

2. 然后到了前置aop进行功能增强

这里就顺便看一下入参都是啥东西,point我感觉就是代理的方法,这边这代理是cglib代理,而下面那个注解用的代理则是动态代理

这里面就是通过你注解里面设的值放到DynamicDataSource.name里面去

然后幸亏之前看了点mybatis源码,datasource设置的清清楚楚

两个数据源,每个数据源里面已经定义好了连接地址等配置信息,还有我们之前配置的targetDatasource和defaultTargetDataSource 

我们顺便再来看一下,mybatis底层这个数据源是啥选择的,是吧都已经看到这里了,就再往下看看呗

public class DynamicDataSource extends AbstractRoutingDataSource

还是得从这里看起,因为我们选择的是路由数据源的方式,因为我们继承了abstractRoutingDataSource,我们选择重写这两个方法,一个是选择路由到哪个数据源,一个是看名字我猜是,在所有配置信息完成后的操作,我们这里面配置了目标数据源map的定义,然后是默认数据源,以及再走父类的afterProperties方法

// 返回当前数据源标识
    @Override
    protected Object determineCurrentLookupKey() {
        return name.get();

    }
    
    @Override
    public void afterPropertiesSet() {

        // 为targetDataSources初始化所有数据源
        Map<Object, Object> targetDataSources=new HashMap<>();
        targetDataSources.put("W",dataSource1);
        targetDataSources.put("R",dataSource2);

        super.setTargetDataSources(targetDataSources);

        // 为defaultTargetDataSource 设置默认的数据源
        super.setDefaultTargetDataSource(dataSource1);

        super.afterPropertiesSet();
    }

这里还看不出来那个lookupkey是干嘛用的,继续往下看,走到父类的方法中

public void afterPropertiesSet() {
        if (this.targetDataSources == null) {
            throw new IllegalArgumentException("Property 'targetDataSources' is required");
        } else {
            this.resolvedDataSources = CollectionUtils.newHashMap(this.targetDataSources.size());
            this.targetDataSources.forEach((key, value) -> {
                Object lookupKey = this.resolveSpecifiedLookupKey(key);
                DataSource dataSource = this.resolveSpecifiedDataSource(value);
                this.resolvedDataSources.put(lookupKey, dataSource);
            });
            if (this.defaultTargetDataSource != null) {
                this.resolvedDefaultDataSource = this.resolveSpecifiedDataSource(this.defaultTargetDataSource);
            }

        }
    }

这里面代码也很好懂,定义最后要用的数据源的map和默认最后用哪个数据源的map,我自己理解的

看到这里我就去找怎么确认最后使用哪个数据源,发现有这么个方法,我猜这个是决定使用哪个数据源,看看下面就是通过这个lookupkey找数据源

protected DataSource determineTargetDataSource() {
        Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
        Object lookupKey = this.determineCurrentLookupKey();
        DataSource dataSource = (DataSource)this.resolvedDataSources.get(lookupKey);
        if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
            dataSource = this.resolvedDefaultDataSource;
        }

        if (dataSource == null) {
            throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
        } else {
            return dataSource;
        }
    }
Object lookupKey = this.determineCurrentLookupKey();// 我们看到有这么一句话

我们来看看这个设置的是个啥,接着找实现类

@Nullable
    protected abstract Object determineCurrentLookupKey();

哦豁,正好是我们重写的方法,那就直接进去呗,就是我们之前定义的w,r

@Override
    protected Object determineCurrentLookupKey() {
        return name.get();
    }

我们这里面还是这么定义的,通过threadLocal,每个线程有这么一份变量

public static ThreadLocal<String> name=new ThreadLocal<>();

我们通过这么一设置,到时候通过以上的步骤,等到使用mybatis操作数据库的时候,就知道最终使用的是哪个数据源了

使用MyBatis注册多个SqlSessionFactory

看一下配置类定义的

@Configuration
// 继承mybatis:
// 1. 指定扫描的mapper接口包(主库)
// 2. 指定使用sqlSessionFactory是哪个(主库)
@MapperScan(basePackages = "com.tuling.datasource.dynamic.mybatis.mapper.r",
        sqlSessionFactoryRef="rSqlSessionFactory")
public class RMyBatisConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.datasource2")
    public DataSource dataSource2() {
        // 底层会自动拿到spring.datasource中的配置, 创建一个DruidDataSource
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory rSqlSessionFactory()
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 指定主库
        sessionFactory.setDataSource(dataSource2());
        // 指定主库对应的mapper.xml文件
        /*sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/r/*.xml"));*/
        return sessionFactory.getObject();
    }



    @Bean
    public DataSourceTransactionManager rTransactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource2());
        return dataSourceTransactionManager;
    }

    @Bean
    public TransactionTemplate rTransactionTemplate(){
        return new TransactionTemplate(rTransactionManager());
    }
}

没搞明白他这个事务管理器和模板干啥用的

配置完事好像直接就能使用了

使用dynamic-datasource框架

dynamic-datasource是MyBaits-plus作者设计的一个多数据源开源方案。使用这个框架需要引入对应的pom依赖

<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.5.0</version>
        </dependency>

这样就可以在SpringBoot的配置文件中直接配置多个数据源。

spring:
  datasource:
    dynamic:
      #设置默认的数据源或者数据源组,默认值即为master
      primary: master
      #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      strict: false
      datasource:
        master:
          url: jdbc:mysql://127.0.0.1:3306/datasource1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
          username: root
          password: root
          initial-size: 1
          min-idle: 1
          max-active: 20
          test-on-borrow: true
          driver-class-name: com.mysql.cj.jdbc.Driver
        slave_1:
          url: jdbc:mysql://127.0.0.1:3306/datasource2?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF8&useSSL=false
          username: root
          password: root
          initial-size: 1
          min-idle: 1
          max-active: 20
          test-on-borrow: true
          driver-class-name: com.mysql.cj.jdbc.Driver

这样就配置完成了master和slave_1两个数据库。

接下来在使用时,只要在对应的方法或者类上添加@DS注解即可。例如

@Service
public class FriendImplService implements FriendService {

    @Autowired
    FriendMapper friendMapper;


    @Override
    @DS("slave_1")  // 从库, 如果按照下划线命名方式配置多个  , 可以指定前缀即可(组名)
    public List<Friend> list() {
        return friendMapper.list();
    }

    @Override
    @DS("master")
    public void save(Friend friend) {
        friendMapper.save(friend);
    }


//    @DS("master")
//    @DSTransactional
//    public void saveAll(){
//        // 执行多数据源的操作
//    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值