服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot基于AbstractRoutingDataSource实现多数据源动态切换

SpringBoot基于AbstractRoutingDataSource实现多数据源动态切换

2022-12-25 16:37未来设计师 Java教程

本文主要介绍了SpringBoot基于AbstractRoutingDataSource实现多数据源动态切换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、场景

在生产业务中,有一些任务执行了耗时较长的查询操作,在实时性要求不高的时候,我们希望将这些查询sql分离出来,去从库查询,以减少应用对主数据库的压力。

一种方案是在配置文件中配置多个数据源,然后通过配置类来获取数据源以及mapper相关的扫描配置,不同的数据源配置不佟的mapper扫描位置,然后需要哪一个数据源就注入哪一个mapper接口即可,这种方法比较简单。特征是通过mapper扫描位置区分数据源。

第二种方案是配置一个默认使用的数据源,然后定义多个其他的数据源,使用aop形成注解式选择数据源。此种方案实现的核心是对AbstractRoutingDataSource 类的继承。这是本文的重点。

二、原理

AbstractRoutingDataSource的多数据源动态切换的核心逻辑是:在程序运行时,把数据源数据源通过 AbstractRoutingDataSource 动态织入到程序中,灵活的进行数据源切换。
基于AbstractRoutingDataSource的多数据源动态切换,可以实现读写分离。逻辑如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
     * 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();

通过实现抽象方法determineCurrentLookupKey指定需要切换的数据源

三、代码示例

示例中主要依赖 

com.alibaba.druid;tk.mybatis

定义一个类用于关联数据源。通过 TheadLocal 来保存每个线程选择哪个数据源的标志(key)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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);
    }
}

继承

AbstractRoutingDataSource

?
1
2
3
4
5
6
7
8
public class DynamicDataSource  extends AbstractRoutingDataSource {
 
    @Override
    protected Object determineCurrentLookupKey() {
 
        return  DynamicDataSourceContextHolder.getDataSourceType();
    }
}

配置主数据库master 与从数据库slave(略)。数据源配置可以从简

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@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;
    }
}

注意主从数据库的bean name

配置DynamicDataSource 

  • targetDataSources 存放数据源的k-v对
  • defaultTargetDataSource 存放默认数据源

配置事务管理器和SqlSessionFactoryBean

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@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;
    }
}

定义一个注解用于指定数据源

?
1
2
3
4
5
6
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TargetDataSource {
    String value();
}

切面的业务逻辑。注意指定order,以确保在开启事务之前执行 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@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和application.yml

使用示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Resource
    private ShopBillDOMapper shopBillDOMapper;
 
//使用默认数据源
    public ShopBillBO queryTestData(Integer id){
 
        return shopBillDOMapper.getByShopBillId(id);
    }
 
//切换到指定的数据源
    @TargetDataSource("slaveDataSource")
    public ShopBill queryTestData2(Integer id){
        return shopBillDOMapper.getByShopBillId(id);
    }

如果返回不同的结果就成功了!

到此这篇关于SpringBoot基于AbstractRoutingDataSource实现多数据源动态切换的文章就介绍到这了,更多相关SpringBoot 多数据源动态切换内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/long710910256/article/details/124945630

延伸 · 阅读

精彩推荐