


How SpringBoot implements dynamic switching of multiple data sources based on AbstractRoutingDataSource
1. Scenario
In the production business, some tasks perform long-time query operations. When the real-time requirements are not high, we hope to separate these query SQLs from Database queries to reduce application pressure on the main database.
One solution is to configure multiple data sources in the configuration file, and then obtain the data source and mapper-related scanning configuration through the configuration class. Different data sources configure different mapper scanning positions, and then which ones are needed A data source can be injected into any mapper interface. This method is relatively simple. The feature is to distinguish data sources through mapper scanning locations.
A feasible solution is to set a default data source in advance, define multiple other data sources at the same time, and use aop to implement annotations to switch data sources. Inheriting the AbstractRoutingDataSource class is the key to implementing this solution. This is the focus of this article.
2. Principle
The core logic of the dynamic switching of multiple data sources of AbstractRoutingDataSource is: when the program is running, the data source is dynamically woven into the program through AbstractRoutingDataSource, and the data is flexibly processed. Source switching.
Dynamic switching of multiple data sources based on AbstractRoutingDataSource can achieve separation of reading and writing. The logic is as follows:
/** * Retrieve the current target DataSource. Determines the * {@link #determineCurrentLookupKey() current lookup key}, performs * a lookup in the {@link #setTargetDataSources targetDataSources} map, * falls back to the specified * {@link #setDefaultTargetDataSource default target DataSource} if necessary. * @see #determineCurrentLookupKey() */ protected DataSource determineTargetDataSource() { Assert.notNull(this.resolvedDataSources, "DataSource router not initialized"); Object lookupKey = determineCurrentLookupKey(); 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 + "]"); } return dataSource; } /** * Determine the current lookup key. This will typically be * implemented to check a thread-bound transaction context. * <p>Allows for arbitrary keys. The returned key needs * to match the stored lookup key type, as resolved by the * {@link #resolveSpecifiedLookupKey} method. */ @Nullable protected abstract Object determineCurrentLookupKey();
Specify the data source that needs to be switched by implementing the abstract method determineCurrentLookupKey
3. Code example
The example mainly relies on
com.alibaba .druid;tk.mybatis
Define a class used to associate data sources. Use TheadLocal to save the flag (key) of which data source each thread selects
@Slf4j public class DynamicDataSourceContextHolder { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static List<String> dataSourceIds = new ArrayList<String>(); public static void setDataSourceType(String dataSourceType) { log.info("设置当前数据源为{}",dataSourceType); contextHolder.set(dataSourceType); } public static String getDataSourceType() { return contextHolder.get() ; } public static void clearDataSourceType() { contextHolder.remove(); } public static boolean containsDataSource(String dataSourceId){ log.info("list = {},dataId={}", JSON.toJSON(dataSourceIds),dataSourceId); return dataSourceIds.contains(dataSourceId); } }
Inheritance
AbstractRoutingDataSource
public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DynamicDataSourceContextHolder.getDataSourceType(); } }
Configure the main database master and slave database slave (omitted). The data source configuration can be simplified
@Configuration @tk.mybatis.spring.annotation.MapperScan(value = {"com.server.dal.dao"}) @ConditionalOnProperty(name = "java.druid.datasource.master.url") public class JavaDruidDataSourceConfiguration { private static final Logger logger = LoggerFactory.getLogger(JavaDruidDataSourceConfiguration.class); @Resource private JavaDruidDataSourceProperties druidDataSourceProperties; @Primary @Bean(name = "masterDataSource", initMethod = "init", destroyMethod = "close") @ConditionalOnMissingBean(name = "masterDataSource") public DruidDataSource javaReadDruidDataSource() { DruidDataSource result = new DruidDataSource(); try { // result.setName(druidDataSourceProperties.getName()); result.setUrl(druidDataSourceProperties.getUrl()); result.setUsername(druidDataSourceProperties.getUsername()); result.setPassword(druidDataSourceProperties.getPassword()); result.setConnectionProperties( "config.decrypt=false;config.decrypt.key=" + druidDataSourceProperties.getPwdPublicKey()); result.setFilters("config"); result.setMaxActive(druidDataSourceProperties.getMaxActive()); result.setInitialSize(druidDataSourceProperties.getInitialSize()); result.setMaxWait(druidDataSourceProperties.getMaxWait()); result.setMinIdle(druidDataSourceProperties.getMinIdle()); result.setTimeBetweenEvictionRunsMillis(druidDataSourceProperties.getTimeBetweenEvictionRunsMillis()); result.setMinEvictableIdleTimeMillis(druidDataSourceProperties.getMinEvictableIdleTimeMillis()); result.setValidationQuery(druidDataSourceProperties.getValidationQuery()); result.setTestWhileIdle(druidDataSourceProperties.isTestWhileIdle()); result.setTestOnBorrow(druidDataSourceProperties.isTestOnBorrow()); result.setTestOnReturn(druidDataSourceProperties.isTestOnReturn()); result.setPoolPreparedStatements(druidDataSourceProperties.isPoolPreparedStatements()); result.setMaxOpenPreparedStatements(druidDataSourceProperties.getMaxOpenPreparedStatements()); if (druidDataSourceProperties.isEnableMonitor()) { StatFilter filter = new StatFilter(); filter.setLogSlowSql(druidDataSourceProperties.isLogSlowSql()); filter.setMergeSql(druidDataSourceProperties.isMergeSql()); filter.setSlowSqlMillis(druidDataSourceProperties.getSlowSqlMillis()); List<Filter> list = new ArrayList<>(); list.add(filter); result.setProxyFilters(list); } } catch (Exception e) { logger.error("数据源加载失败:", e); } finally { result.close(); } return result; } }
Pay attention to the bean name of the master-slave database
Configure DynamicDataSource
targetDataSources stores the k-v pair of the data source
defaultTargetDataSource stores the default data source
Configure the transaction manager and SqlSessionFactoryBean
@Configuration public class DynamicDataSourceConfig { private static final String MAPPER_LOCATION = "classpath*:sqlmap/dao/*Mapper.xml"; @Bean(name = "dynamicDataSource") public DynamicDataSource dynamicDataSource(@Qualifier("masterDataSource") DruidDataSource masterDataSource, @Qualifier("slaveDataSource") DruidDataSource slaveDataSource) { Map<Object, Object> targetDataSource = new HashMap<>(); DynamicDataSourceContextHolder.dataSourceIds.add("masterDataSource"); targetDataSource.put("masterDataSource", masterDataSource); DynamicDataSourceContextHolder.dataSourceIds.add("slaveDataSource"); targetDataSource.put("slaveDataSource", slaveDataSource); DynamicDataSource dataSource = new DynamicDataSource(); dataSource.setTargetDataSources(targetDataSource); dataSource.setDefaultTargetDataSource(masterDataSource); return dataSource; } @Primary @Bean(name = "javaTransactionManager") @ConditionalOnMissingBean(name = "javaTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDataSource") DynamicDataSource druidDataSource) { return new DataSourceTransactionManager(druidDataSource); } @Bean(name = "sqlSessionFactoryBean") public SqlSessionFactoryBean myGetSqlSessionFactory(@Qualifier("dynamicDataSource") DynamicDataSource dataSource) { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { sqlSessionFactoryBean.setMapperLocations(resolver.getResources(MAPPER_LOCATION)); } catch (IOException e) { e.printStackTrace(); } sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean; } }
Define an annotation to specify the data source
@Target({ ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface TargetDataSource { String value(); }
Aspect business logic. Pay attention to specifying order to ensure execution before starting the transaction.
@Aspect @Slf4j @Order(-1) @Component public class DataSourceAop { @Before("@annotation(targetDataSource)") public void changeDataSource(JoinPoint point, TargetDataSource targetDataSource) { String dsId = targetDataSource.value(); if (!DynamicDataSourceContextHolder.containsDataSource(dsId)) { log.error("数据源[{}]不存在,使用默认数据源 > {}" + targetDataSource.value() + point.getSignature()); } else { log.info("UseDataSource : {} > {}" + targetDataSource.value() + point.getSignature()); DynamicDataSourceContextHolder.setDataSourceType(targetDataSource.value()); } } @After("@annotation(targetDataSource)") public void restoreDataSource(JoinPoint point, TargetDataSource targetDataSource) { log.info("RevertDataSource : {} > {}"+targetDataSource.value()+point.getSignature()); DynamicDataSourceContextHolder.clearDataSourceType(); } }
Pom.xml and application.yml are omitted above
Usage example
@Resource private ShopBillDOMapper shopBillDOMapper; //使用默认数据源 public ShopBillBO queryTestData(Integer id){ return shopBillDOMapper.getByShopBillId(id); } //切换到指定的数据源 @TargetDataSource("slaveDataSource") public ShopBill queryTestData2(Integer id){ return shopBillDOMapper.getByShopBillId(id); }
If different results are returned, it will be successful!
The above is the detailed content of How SpringBoot implements dynamic switching of multiple data sources based on AbstractRoutingDataSource. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!