Spring Boot 如何動態切換數據源


本章是一個完整的 Spring Boot 動態數據源切換示例,例如主數據庫使用 lionsea 從數據庫 lionsea_slave1、lionsea_slave2。只需要在對應的代碼上使用 DataSource("slave1") 注解來實現數據庫切換。

想要實現數據源動態切換,需要用到以下知識

  1. spring boot 中自定義注解
  2. spring boot 中的 aop 攔截
  3. mybatis 的增刪改查操作

本項目源碼 github 下載

1 新建 Spring Boot Maven 示例工程項目

注意:是用來 IDEA 開發工具

  1. File > New > Project,如下圖選擇 Spring Initializr 然后點擊 【Next】下一步
  2. 填寫 GroupId(包名)、Artifact(項目名) 即可。點擊 下一步
    groupId=com.fishpro
    artifactId=dynamicdb
  3. 選擇依賴 Spring Web Starter 前面打鈎。
  4. 項目名設置為 spring-boot-study-dynamicdb.

2 依賴引入 Pom

3 動態數據源切換

3.1 新建多數據源注解 DataSource

文件路徑(spring-boot-study/spring-boot-study-dynamicdb/src/main/java/com/fishpro/dynamicdb/datasource/annotation/DataSource.java)


/**
 * 多數據源注解
 * 在方法名上加入 DataSource('名稱')
 *
 * @author fishpro
 * */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface DataSource {
    String value() default "";
}

3.2 新建一個多數據源上下文切換 DynamicContextHolder

/**
 * 多數據源上下文
 * 
 */
public class DynamicContextHolder {
    @SuppressWarnings("unchecked")
    private static final ThreadLocal<Deque<String>> CONTEXT_HOLDER = new ThreadLocal() {
        @Override
        protected Object initialValue() {
            return new ArrayDeque();
        }
    };

    /**
     * 獲得當前線程數據源
     *
     * @return 數據源名稱
     */
    public static String getDataSource() {
        return CONTEXT_HOLDER.get().peek();
    }

    /**
     * 設置當前線程數據源
     *
     * @param dataSource 數據源名稱
     */
    public static void setDataSource(String dataSource) {
        CONTEXT_HOLDER.get().push(dataSource);
    }

    /**
     * 清空當前線程數據源
     */
    public static void clearDataSource() {
        Deque<String> deque = CONTEXT_HOLDER.get();
        deque.poll();
        if (deque.isEmpty()) {
            CONTEXT_HOLDER.remove();
        }
    }

}

3.3 新建一個多數據源切面處理類

新建一個多數據源切面處理類指定Aop處理的注解點,和處理的事件(切換數據源),至此多數據源切換的主要工作就完成了。


/**
 * 多數據源,切面處理類
 * 
 */
@Aspect
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class DataSourceAspect {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 切面點 指定注解
     * */
    @Pointcut("@annotation(com.fishpro.dynamicdb.datasource.annotation.DataSource) " +
            "|| @within(com.fishpro.dynamicdb.datasource.annotation.DataSource)")
    public void dataSourcePointCut() {

    }

    /**
     * 攔截方法指定為 dataSourcePointCut
     * */
    @Around("dataSourcePointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Class targetClass = point.getTarget().getClass();
        Method method = signature.getMethod();

        DataSource targetDataSource = (DataSource)targetClass.getAnnotation(DataSource.class);
        DataSource methodDataSource = method.getAnnotation(DataSource.class);
        if(targetDataSource != null || methodDataSource != null){
            String value;
            if(methodDataSource != null){
                value = methodDataSource.value();
            }else {
                value = targetDataSource.value();
            }

            DynamicContextHolder.setDataSource(value);
            logger.debug("set datasource is {}", value);
        }

        try {
            return point.proceed();
        } finally {
            DynamicContextHolder.clearDataSource();
            logger.debug("clean datasource");
        }
    }
}

3.4 切換數據源

當Aop方法攔截到了帶有注解 @DataSource 的方法的是,需要去執行指定的數據源,那么如何執行呢,這里我們使用阿里的 druid 鏈接池作為數據源連接池。這就要求我們需要對連接池進行一個可定制化的開發。程序安裝Aop攔截到的信息去重新設定數據庫路由,實現動態切換數據源的目標。

3.4.1 定義鏈接池的屬性

在application.yml中的配置節點

spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
            username: root
            password: 123
            initial-size: 10
            max-active: 100
            min-idle: 10
            max-wait: 60000
            pool-prepared-statements: true
            max-pool-prepared-statement-per-connection-size: 20
            time-between-eviction-runs-millis: 60000
            min-evictable-idle-time-millis: 300000
            #Oracle需要打開注釋
            #validation-query: SELECT 1 FROM DUAL
            test-while-idle: true
            test-on-borrow: false
            test-on-return: false
            stat-view-servlet:
                enabled: true
                url-pattern: /druid/*
                #login-username: admin
                #login-password: admin
            filter:
                stat:
                    log-slow-sql: true
                    slow-sql-millis: 1000
                    merge-sql: false
                wall:
                    config:
                        multi-statement-allow: true

/**
 * 多數據源主數據源屬性
 * 
 */
public class DataSourceProperties {
    private String driverClassName;
    private String url;
    private String username;
    private String password;

    /**
     * Druid默認參數
     */
    private int initialSize = 2;
    private int maxActive = 10;
    private int minIdle = -1;
    private long maxWait = 60 * 1000L;
    private long timeBetweenEvictionRunsMillis = 60 * 1000L;
    private long minEvictableIdleTimeMillis = 1000L * 60L * 30L;
    private long maxEvictableIdleTimeMillis = 1000L * 60L * 60L * 7;
    private String validationQuery = "select 1";
    private int validationQueryTimeout = -1;
    private boolean testOnBorrow = false;
    private boolean testOnReturn = false;
    private boolean testWhileIdle = true;
    private boolean poolPreparedStatements = false;
    private int maxOpenPreparedStatements = -1;
    private boolean sharePreparedStatements = false;
    private String filters = "stat,wall";

    /* 省略自動化生成部分 */
}

3.4.2 多數據源從數據源屬性類

在application.xml表示為,支持多數據庫

dynamic:
  datasource:
  #slave1 slave2 數據源已測試
    slave1:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/lionsea_slave1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: 123456
    slave2:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/lionsea_slave2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: 123456
    slave3:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      url: jdbc:sqlserver://localhost:1433;DatabaseName=renren_security
      username: sa
      password: 123456
    slave4:
      driver-class-name: org.postgresql.Driver
      url: jdbc:postgresql://localhost:5432/renren_security
      username: renren
      password: 123456

多數據源從數據源屬性類,在application中表示為以 dynamic 為節點的配置

/**
 * 多數據源屬性 在application中表示為以 dynamic 為節點的配置
 *
 */
@ConfigurationProperties(prefix = "dynamic")
public class DynamicDataSourceProperties {
    private Map<String, DataSourceProperties> datasource = new LinkedHashMap<>();

    public Map<String, DataSourceProperties> getDatasource() {
        return datasource;
    }

    public void setDatasource(Map<String, DataSourceProperties> datasource) {
        this.datasource = datasource;
    }
}

3.4.3 建立動態數據源工廠類

建立動態數據源工廠類用於創建動態數據源連接池 Druid

/**
 * DruidDataSource
 *
 */
public class DynamicDataSourceFactory {

    /**
     * 通過自定義建立 Druid的數據源
     * */
    public static DruidDataSource buildDruidDataSource(DataSourceProperties properties) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(properties.getDriverClassName());
        druidDataSource.setUrl(properties.getUrl());
        druidDataSource.setUsername(properties.getUsername());
        druidDataSource.setPassword(properties.getPassword());

        druidDataSource.setInitialSize(properties.getInitialSize());
        druidDataSource.setMaxActive(properties.getMaxActive());
        druidDataSource.setMinIdle(properties.getMinIdle());
        druidDataSource.setMaxWait(properties.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
        druidDataSource.setMaxEvictableIdleTimeMillis(properties.getMaxEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(properties.getValidationQuery());
        druidDataSource.setValidationQueryTimeout(properties.getValidationQueryTimeout());
        druidDataSource.setTestOnBorrow(properties.isTestOnBorrow());
        druidDataSource.setTestOnReturn(properties.isTestOnReturn());
        druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
        druidDataSource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements());
        druidDataSource.setSharePreparedStatements(properties.isSharePreparedStatements());

        try {
            druidDataSource.setFilters(properties.getFilters());
            druidDataSource.init();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return druidDataSource;
    }
}

3.4.3 配置多數據源配置類

/**
 * 配置多數據源
 * 
 */
@Configuration
@EnableConfigurationProperties(DynamicDataSourceProperties.class)
public class DynamicDataSourceConfig {
    @Autowired
    private DynamicDataSourceProperties properties;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSourceProperties dataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DynamicDataSource dynamicDataSource(DataSourceProperties dataSourceProperties) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(getDynamicDataSource());

        //默認數據源
        DruidDataSource defaultDataSource = DynamicDataSourceFactory.buildDruidDataSource(dataSourceProperties);
        dynamicDataSource.setDefaultTargetDataSource(defaultDataSource);

        return dynamicDataSource;
    }

    private Map<Object, Object> getDynamicDataSource(){
        Map<String, DataSourceProperties> dataSourcePropertiesMap = properties.getDatasource();
        Map<Object, Object> targetDataSources = new HashMap<>(dataSourcePropertiesMap.size());
        dataSourcePropertiesMap.forEach((k, v) -> {
            DruidDataSource druidDataSource = DynamicDataSourceFactory.buildDruidDataSource(v);
            targetDataSources.put(k, druidDataSource);
        });

        return targetDataSources;
    }

}

3.5 測試多數據源

使用 Spring Boot 的測試類進行測試,建立一個基於 Mybatis 的 CRUD。

3.5.1 新建三個數據一個主庫,2個從庫

新建三個數據一個主庫,2個從庫,每個數據庫都新建下面的表

DROP TABLE IF EXISTS `demo_test`;
CREATE TABLE `demo_test` (
  `id` bigint(20) NOT NULL,
  `name` varchar(255) NOT NULL,
  `status` tinyint(4) DEFAULT NULL,
  `is_deleted` tinyint(4) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `create_user_id` bigint(20) DEFAULT NULL,
  `age` bigint(20) DEFAULT NULL,
  `content` text,
  `body` longtext,
  `title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_name` (`name`) USING BTREE,
  KEY `idx_title` (`title`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

3.5.2 建立一個基於 Mybatis 的 CRUD

這里主要使用代碼生成器生產一個增刪改查(也可以手動)主要包括了 dao/domain/service/impl

/domain/DemoTestDO.java

public class DemoTestDO implements Serializable {
	private static final long serialVersionUID = 1L;
	
	//
	private Long id;
	//
	private String name;
	//
	private Integer status;
	//
	private Integer isDeleted;
	//
	private Date createTime;
	//
	private Long createUserId;
	//
	private Long age;
	//
	private String content;
	//
	private String body;
	//
	private String title;
    //省略自動生成的部分

}

/dao/DemoTestDao.java

@Mapper
public interface DemoTestDao {

	DemoTestDO get(Long id);
	
	List<DemoTestDO> list(Map<String, Object> map);
	
	int count(Map<String, Object> map);
	
	int save(DemoTestDO demoTest);
	
	int update(DemoTestDO demoTest);
	
	int remove(Long id);
	
	int batchRemove(Long[] ids);
}

具體見 本項目源碼 github 下載

3.5.3 編寫測試代碼

動態代碼測試方法類

@Service
//@DataSource("slave1")
public class DynamicDataSourceTestService {
    @Autowired
    private DemoTestDao demoTestDao;

    @Transactional
    public void updateDemoTest(Long id){
        DemoTestDO user = new DemoTestDO();
        user.setId(id);
        user.setTitle("13500000000");
        demoTestDao.update(user);
    }

    @Transactional
    @DataSource("slave1")
    public void updateDemoTestBySlave1(Long id){
        DemoTestDO user = new DemoTestDO();
        user.setId(id);
        user.setTitle("13500000001");
        demoTestDao.update(user);
    }

    @DataSource("slave2")
    @Transactional
    public void updateDemoTestBySlave2(Long id){
        DemoTestDO user = new DemoTestDO();
        user.setId(id);
        user.setTitle("13500000002");
        demoTestDao.update(user);

        //測試事物
//        int i = 1/0;
    }
}

動態測試代碼

@RunWith(SpringRunner.class)
@SpringBootTest
public class DynamicdbApplicationTests {

    @Autowired
    private DynamicDataSourceTestService dynamicDataSourceTestService;

    /**
     * 觀察三個數據源中的數據是否正確
     * */
    @Test
    public void testDaynamicDataSource(){
        Long id = 1L;

        dynamicDataSourceTestService.updateDemoTest(id);
        dynamicDataSourceTestService.updateDemoTestBySlave1(id);
        dynamicDataSourceTestService.updateDemoTestBySlave2(id);
    }

}

3.6 測試

右鍵 DynamicDataSourceTest 執行 Run DynamicDataSourceTest

默認數據庫操作成功
slave1數據庫操作成功
slave2數據庫操作成功

Process finished with exit code 0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM