SpringBoot 动态数据源切换 + 手动数据源切换(最有效的)

注意:笔者使用的是 Mybatis ,MyBatisPlus自带的有多数据源

手动数据源切换

先看下配置类一个是本地的IP一个是我的服务器上docker容器中的mysql数据库。

server.port=8080
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.mapper-locations=classpath:mapper/*.xml
spring.datasource.aa.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.aa.username=root
spring.datasource.aa.password=123
spring.datasource.aa.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false

spring.datasource.bb.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.bb.username=root
spring.datasource.bb.password=123
spring.datasource.bb.url=jdbc:mysql://39.1xx.1xx.1xx:3306/test?serverTimezone=UTC&useSSL=false

然后最主要的就是先编写一个DynamicDatasourceConfig类去继承,AbstractRoutingDataSource这个抽象类。至于为什么继承这个类后续会为大家讲解。

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceChange.getDataSourceKey();
    }
}

然后就是这个DynamicDataSourceChange的切换类,这个类负责切换数据源。


import java.util.ArrayList;
import java.util.List;

public class DynamicDataSourceChange {

/**
* 这个ThreadLocal  是用来储存当前线程中的数据源的key
*/
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>() {
        /**
         * 将 master 数据源的 key作为默认数据源的 key
         */
        @Override
        protected String initialValue() {
            return "aa";
        }
    };
    /**
     * 切换数据源
     * @param key
     */
    public static void setDataSourceKey(String key) {
        contextHolder.set(key);
    }
    /**
     * 获取数据源
     * @return
     */
    public static String getDataSourceKey() {
        return contextHolder.get();
    }

    /**
     * 重置数据源
     */
    public static void clearDataSourceKey() {
        contextHolder.remove();
    }
}

这个类中的ThreadLocal用来存储当前进程中的数据源的key。上面的额initValue 是我自己设置的默认数据源的key。然后就是数据源的注册,这里也是很关键的一步。

package com.llq.datasource.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;

@Configuration
public class DataSourceRegister {

    private static final Logger logger = LoggerFactory.getLogger(DataSourceRegister.class);

    @Bean(name = "aa")
    @Primary
    public static DataSource registerDataSource() throws IOException {
        InputStream asStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
        Properties properties = new Properties();
        properties.load(asStream);
        String username = properties.getProperty("spring.datasource.aa.username");
        String password = properties.getProperty("spring.datasource.aa.password");
        String url = properties.getProperty("spring.datasource.aa.url");
        String driverClass = properties.getProperty("spring.datasource.aa.driver-class-name");
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }
    @Bean(name = "bb")
    public static DataSource registerDataSource2() throws IOException {
        InputStream asStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
        Properties properties = new Properties();
        properties.load(asStream);
        String username = properties.getProperty("spring.datasource.bb.username");
        String password = properties.getProperty("spring.datasource.bb.password");
        String url = properties.getProperty("spring.datasource.bb.url");
        String driverClass = properties.getProperty("spring.datasource.bb.driver-class-name");
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }

    @Bean("dynamicDataSource")
    public DataSource registerAllDataSource() throws IOException {
        DataSource dataSource1 = registerDataSource();
        DataSource dataSource2 = registerDataSource2();
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        HashMap<Object, Object> map = new HashMap<>();
        map.put("aa",dataSource1);
        map.put("bb",dataSource2);
       //这里敲一下黑板,这里必须要指定默认的数据源不然,在注册时候会出现发现两个数据源的异常问题,所以需要去设置默认数据源,也就是setDefalutTargetDataSource(),这个方法是哪儿来的呢?这个方法是因为DynamicDataSource继承了AbstractRoutingDataSource这个类。
        dynamicDataSource.setDefaultTargetDataSource(dataSource1);
        //这里是将所有的数据源放入
        dynamicDataSource.setTargetDataSources(map);
        logger.info("数据源注册成功,一共注册{}个数据源",map.size());
        return dynamicDataSource;
    }


    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception{
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(registerAllDataSource());
        sessionFactory.setTypeAliasesPackage("com.llq.pojo");
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        return sessionFactory;
    }
}

然后这样就好了,其余的就是自己写代码测试了。还有就是这里再强调一下,在手动切换数据源的时候你需要去先去清除一下数据源,然后再进行设置。


    @RequestMapping("getUsersAA")
    public String shardingCrudAA(){
        DynamicDataSourceChange.clearDataSourceKey();
        DynamicDataSourceChange.setDataSourceKey("aa");
        List<User>  allTables = userService.getAllTables();
        return allTables.toString();
    }

    @RequestMapping("getUsersBB")
    public String shardingCrudBB(){
        DynamicDataSourceChange.clearDataSourceKey();
        DynamicDataSourceChange.setDataSourceKey("bb");
        List<User>  allTables = userService.getAllTablesbb();
        return allTables.toString();
    }
动态数据源

动态数据源的话其实和手动的大差不差,只不过动态的是交给了AOP根据切入点的时机去实现切换数据源。
上面的代码可以不用动,先去创建一个枚举类来配置需要用到的数据源的key以及自定义注解。

public enum DataSourceKey {
    DB_MASTER,
    DB_SLAVE1,
    DB_SLAVE2,
    DB_OTHER
}

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TargetDataSource {
    DataSourceKey dataSourceKey() default DataSourceKey.DB_MASTER;
}

然后就是AOP切面实现类

import com.apedad.example.annotation.TargetDataSource;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
 
@Aspect
@Order(-1)
@Component
public class DynamicDataSourceAspect {
    private static final Logger LOG = Logger.getLogger(DynamicDataSourceAspect.class);
 
    @Pointcut("execution(* com.apedad.example.service.*.list*(..))")
    public void pointCut() {
    }
 
    /**
     * 执行方法前更换数据源
     *
     * @param joinPoint        切点
     * @param targetDataSource 动态数据源
     */
    @Before("@annotation(targetDataSource)")
    public void doBefore(JoinPoint joinPoint, TargetDataSource targetDataSource) {
        DataSourceKey dataSourceKey = targetDataSource.dataSourceKey();
        if (dataSourceKey == DataSourceKey.DB_OTHER) {
            LOG.info(String.format("设置数据源为  %s", DataSourceKey.DB_OTHER));
            DynamicDataSourceChange.setDataSourceKey(DataSourceKey.DB_OTHER);
        } else {
            LOG.info(String.format("使用默认数据源  %s", DataSourceKey.DB_MASTER));
            DynamicDataSourceChange.setDataSourceKey(DataSourceKey.DB_MASTER);
        }
    }
 
    /**
     * 执行方法后清除数据源设置
     *
     * @param joinPoint        切点
     * @param targetDataSource 动态数据源
     */
    @After("@annotation(targetDataSource)")
    public void doAfter(JoinPoint joinPoint, TargetDataSource targetDataSource) {
        LOG.info(String.format("当前数据源  %s  执行清理方法", targetDataSource.dataSourceKey()));
        DynamicDataSourceChange.clearDataSourceKey();
    }
 
    @Before(value = "pointCut()")
    public void doBeforeWithSlave(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //获取当前切点方法对象
        Method method = methodSignature.getMethod();
        if (method.getDeclaringClass().isInterface()) {//判断是否为借口方法
            try {
                //获取实际类型的方法对象
                method = joinPoint.getTarget().getClass()
                        .getDeclaredMethod(joinPoint.getSignature().getName(), method.getParameterTypes());
            } catch (NoSuchMethodException e) {
                LOG.error("方法不存在!", e);
            }
        }
        if (null == method.getAnnotation(TargetDataSource.class)) {
			DynamicDataSourceChange.setDataSourceKey(DataSourceKey.DB_MASTER);
        }
    }
}

  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
SpringBoot 中实现动态数据源切换有多种方式,其中一种比较常用的方式是通过使用 AbstractRoutingDataSource 类和 ThreadLocal 来实现。 具体步骤如下: 1. 创建一个继承 AbstractRoutingDataSource 类的类,重写 determineCurrentLookupKey 方法,该方法返回一个字符串作为数据源的 key。 2. 在配置文件中配置多个数据源,每个数据源都需要配置一个 key 作为标识。 3. 在需要进行数据源切换的地方,调用 DataSourceContextHolder 类的 setDataSourceKey 方法设置当前数据源的 key。 4. 在需要使用数据源的地方,通过调用 DataSourceContextHolder 类的 getDataSourceKey 方法获取当前数据源的 key。 5. 在配置类中配置事务管理器时,需要将动态数据源作为参数传递给事务管理器。 示例代码如下: 1. 创建 DynamicDataSource 类 ```java public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDataSourceKey(); } } ``` 2. 在配置文件中配置多个数据源 ```yaml spring: datasource: master: url: jdbc:mysql://localhost:3306/master username: root password: root driver-class-name: com.mysql.jdbc.Driver slave: url: jdbc:mysql://localhost:3306/slave username: root password: root driver-class-name: com.mysql.jdbc.Driver ``` 3. 设置当前数据源的 key ```java public class DataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<>(); public static void setDataSourceKey(String key) { contextHolder.set(key); } public static String getDataSourceKey() { return contextHolder.get(); } public static void clearDataSourceKey() { contextHolder.remove(); } } ``` 4. 获取当前数据源的 key ```java String dataSourceKey = DataSourceContextHolder.getDataSourceKey(); ``` 5. 配置事务管理器 ```java @Bean public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) { return new DataSourceTransactionManager(dataSource); } ``` 以上是一种常用的动态数据源切换方式,可以根据具体需求进行扩展和优化。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值