SpringBoot双数据源配置

一、多套数据源

  1、独立数据库连接信息

  Spring Boot 的默认配置文件是 application.properties ,由于有两个数据库配置,独立配置数据库是好的实践,因此添加配置文件 jbdc.properties ,添加以下自定义的主从数据库配置:

# db01
spring.datasource.db01.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db01.jdbc-url=jdbc:mysql://localhost:3306/java-training-camp?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.db01.username=root
spring.datasource.db01.password=ENC(wcXqCViJj4dmV1YsJPJPaFUNJFH8F4DNSe0XDy/bQKMmpd2eSA+zpE3fc82qlGbT)

# db02
spring.datasource.db02.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db02.jdbc-url=jdbc:mysql://localhost:3306/xxl_job?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.db02.username=root
spring.datasource.db02.password=ENC(wcXqCViJj4dmV1YsJPJPaFUNJFH8F4DNSe0XDy/bQKMmpd2eSA+zpE3fc82qlGbT)

 

  2、多套数据源配置

  有了数据源连接信息,需要把数据源注入到 Spring 中。由于每个数据库使用独立的一套数据库连接,数据库连接使用的 SqlSession 进行会话连接,SqlSession 是由SqlSessionFactory 生成。因此,需要分别配置SqlSessionFactory 。以下操作均在 config 目录 下:

  (1)添加 DataSourceConfig 配置文件,注入主从数据源

    注解 PropertySource 指定配置信息文件

    注解 ConfigurationProperties 指定不同数据源配置前缀

    分别指定数据源名称

@Configuration
@PropertySource("classpath:jdbc.properties")
public class DatasourceConfig {

    @Bean("db01")
    @ConfigurationProperties(prefix = "spring.datasource.db01")
    public DataSource db01DataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("db02")
    @ConfigurationProperties(prefix = "spring.datasource.db02")
    public DataSource db02DataSource(){
        return DataSourceBuilder.create().build();
    }
}

 

  (2)添加 MasterMybatisConfig 配置文件,注入各个数据源的SqlSessionFactory注解 MapperScan 指定那些包下的 mapper 使用本数据源,并指定使用哪个SqlSessionFactory,注意,此处的 sqlSessionFactoryRef 即本配置中的注入的 SqlSessionFactory。

    设置指定的数据源,使用 Qualifier 指定。

    如果使用的是MyBatis Plus, 对应的 Mapper 若有自定义的 mapper.xml, 则使用 setMapperLocations 指定。若需要对实体进行别名处理,则使用 setTypeAliasesPackage 指定。

@Configuration
@MapperScan(basePackages = "com.lcl.mysqldemo.mapper.db01",sqlSessionFactoryRef = "db01SqlSessionFactory")
public class Db01MabatisConfig {

    @Bean("db01SqlSessionFactory")
    public SqlSessionFactory db01SqlSessionFactory(@Qualifier("db01") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        String locationPattern = "classpath*:/mapper/db01/*.xml";
        mybatisSqlSessionFactoryBean.setMapperLocations(resolver.getResources(locationPattern));
        String typeAliasesPackage = "com.lcl.mysqldemo.entity.db01";
        mybatisSqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
        return mybatisSqlSessionFactoryBean.getObject();
    }
}

 

@Configuration
@MapperScan(basePackages = "com.lcl.mysqldemo.mapper.db02",sqlSessionFactoryRef = "db02SqlSessionFactory")
public class Db02MabatisConfig {

    @Bean("db02SqlSessionFactory")
    public SqlSessionFactory db01SqlSessionFactory(@Qualifier("db02") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        String locationPattern = "classpath*:/mapper/db02/*.xml";
        mybatisSqlSessionFactoryBean.setMapperLocations(resolver.getResources(locationPattern));
        String typeAliasesPackage = "com.lcl.mysqldemo.entity.db02";
        mybatisSqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
        return mybatisSqlSessionFactoryBean.getObject();
    }
}

 

  (3)多套实体

    在 MyBatis 配置中,实体设置 typeAliases 可以简化 xml 的配置,前面提到,使用 typeAliasesPackage 设置实体路径,在 entity 包下分别设置 db01 和 db02 包,存放两个库对应的表实体。

  (4)多套 mapper xml 文件

    在 resources/mapper 下,同样设置 db01 及 db02 目录,分别存放对应的mapper xml 文件。

  3、多数据源使用

    经过上面的多套数据源配置,可知道,若需要操作哪个数据库,直接使用对应的 mapper 进行 CRUD 操作即可。如下为 Controller 中分别查询两个库,获取到的数据合在一起返回:

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private XxlJobInfoMapper xxlJobInfoMapper;

    @GetMapping("/03")
    public String test02(){
        Map<String, String> map = new HashMap<>();
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(1);
        XxlJobInfo xxlJobInfo = xxlJobInfoMapper.selectByPrimaryKey(2);
        map.put("orderInfo", JSON.toJSONString(orderInfo));
        map.put("xxlJobInfo", JSON.toJSONString(xxlJobInfo));
        return JSON.toJSONString(map);
    }
}

 

   4、优缺点

    (1)优点

      简单、直接:一个库对应一套处理方式,很好理解。

      符合开闭原则( OCP ):开发的设计模式告诉我们,对扩展开放,对修改关闭,添加多一个数据库,原来的那一套不需要改动,只添加即可。

    (2)缺点

      资源浪费:针对每一个数据源写一套操作,连接数据库的资源也是独立的,分别占用同样多的资源。SqlSessionFactory 是一个工厂,建议是使用单例,完全可以重用,不需要建立多个,只需要更改数据源即可,跟多线程,使用线程池减少资源消耗是同一道理。

      代码冗余:在前面的多数据源配置中可以看出,其实 db01 和 db02 的很多操作是一样的,只是改个名称而已,因此会造成代码冗余。

      缺乏灵活:所有需要使用的地方都需要引入对应的mapper,对于很多操作,只是选择数据源的不一样,代码逻辑是一致的。另外,对于是主从数据库的配置,一主多从的情况,若需要对多个从库进行负载均衡,相对比较麻烦。

    正因为有上述的缺点,所以还有改进的空间。于是就有了动态数据源,至于动态数据源如何实现,下回分解。

二、动态数据源流程说明

  Spring Boot 的动态数据源,本质上是把多个数据源存储在一个 Map 中,当需要使用某个数据源时,从 Map 中获取此数据源进行处理。而在 Spring 中,已提供了抽象类 AbstractRoutingDataSource 来实现此功能。因此,我们在实现动态数据源的,只需要继承它,实现自己的获取数据源逻辑即可。

  1、添加动态数据源的配置

  (1)配置相关

# db01
spring.datasource.dynamicdb01.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamicdb01.jdbc-url=jdbc:mysql://localhost:3306/java-training-camp?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.dynamicdb01.username=root
spring.datasource.dynamicdb01.password=ENC(wcXqCViJj4dmV1YsJPJPaFUNJFH8F4DNSe0XDy/bQKMmpd2eSA+zpE3fc82qlGbT)

# db02
spring.datasource.dynamicdb02.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamicdb02.jdbc-url=jdbc:mysql://localhost:3306/xxl_job?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.dynamicdb02.username=root
spring.datasource.dynamicdb02.password=ENC(wcXqCViJj4dmV1YsJPJPaFUNJFH8F4DNSe0XDy/bQKMmpd2eSA+zpE3fc82qlGbT)

  (2)把数据源常量写在 DataSourceConstants 类中

public class DataSourceConstants {
    public static final String DS_KEY_DB01 = "dynamicdb01";
    public static final String DS_KEY_DB02 = "dynamicdb02";
}

 

  根据连接信息,把数据源注入到 Spring 中,添加 DynamicDataSourceConfig 文件,配置如下:

    此处使用 PropertySource 指定配置文件,ConfigurationProperties 指定数据源配置前缀

    使用 MapperScan 指定包,自动注入相应的 mapper 类。

    从此配置可以看到,已经把 SqlSessionFactory 这个配置从代码中擦除,直接使用 Spring Boot 自动配置的 SqlSessionFactory 即可,无需我们自己配置。

  (3)在 dynamicDataSource 方法中使用 Map 保存多个数据源,并设置到动态数据源对象中。设置默认的数据源是 db01 数据源,使用注解 Primary 优先从动态数据源中获取。

@Configuration
@PropertySource("classpath:jdbc.properties")
@MapperScan(basePackages = "com.lcl.mysqldemo.mapper.dynamic")
public class DynamicDataSourceConfig {

    @Bean(DataSourceConstants.DS_KEY_DB01)
    @ConfigurationProperties(prefix = "spring.datasource.dynamicdb01")
    public DataSource db01DataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(DataSourceConstants.DS_KEY_DB02)
    @ConfigurationProperties(prefix = "spring.datasource.dynamicdb02")
    public DataSource db02DataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public DataSource dynamicDataSource(){
        Map<Object, Object> datasourceMap = new HashMap<>();
        datasourceMap.put(DataSourceConstants.DS_KEY_DB01, db01DataSource());
        datasourceMap.put(DataSourceConstants.DS_KEY_DB02, db02DataSource());
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(datasourceMap);
        dynamicDataSource.setDefaultTargetDataSource(db01DataSource());
        return dynamicDataSource;
    }

}

 

  2、动态数据源设置

  前面的配置已把多个数据源注入到 Spring 中,接着对动态数据源进行配置。

    (1)数据源 key 的上下文

    为了可以动态切换路由策略,需要有一个动态获取数据源 key 的地方(我们称为上下文),对于 web 应用,访问以线程为单位,使用 ThreadLocal 就比较合适

public class DynamicDataSourceContextHolder {

    /**
     * 动态数据源名称上下文
     */
    private static final ThreadLocal<String> DATA_SOURCE_CONTEXT_KEY_HOLDER = new ThreadLocal<>();

    /**
     * 设置/切换数据源
     */
    public static void setContextKey(String key){
        DATA_SOURCE_CONTEXT_KEY_HOLDER.set(key);
    }
    /**
     * 获取数据源名称
     */
    public static String getContextKey(){
        String key = DATA_SOURCE_CONTEXT_KEY_HOLDER.get();
        return key == null?DataSourceConstants.DS_KEY_DB01:key;
    }

    /**
     * 删除当前数据源名称
     */
    public static void removeContextKey(){
        DATA_SOURCE_CONTEXT_KEY_HOLDER.remove();
    }
}

 

  (2)添加动态数据源类

    继承抽象类 AbstractRoutingDataSource ,需要实现方法 determineCurrentLookupKey,即路由策略。该路由策略从上面的 DynamicDataSourceContextHolder 中获取。 

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

 

   3、验证

    默认是使用 db01 数据源查询,使用上下文的 setContextKey 来切换数据源,使用完后使用 removeContextKey 进行恢复

    @GetMapping("/03")
    public String test02(){
        Map<String, String> map = new HashMap<>();
        OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(1);
        map.put("orderInfo", JSON.toJSONString(orderInfo));
        DynamicDataSourceContextHolder.setContextKey(DataSourceConstants.DS_KEY_DB02);
        XxlJobInfo xxlJobInfo = xxlJobInfoMapper.selectByPrimaryKey(2);
        map.put("xxlJobInfo", JSON.toJSONString(xxlJobInfo));
        DynamicDataSourceContextHolder.removeContextKey();
        return JSON.toJSONString(map);
    }
}

  4、使用 AOP 选择数据源

    经过上面的动态数据源配置,可以实现动态数据源切换,但我们会发现,在进行数据源切换时,都需要做 setContextKey 和 removeContextKey 操作,如果需要切换的方法比多,就会发现很多重复的代码,如何消除这些重复的代码,就需要用到动态代理了

  (1)定义数据源注解

    在annotation包中,添加数据源注解 DS,此注解可以写在类中,也可以写在方法定义中。

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DS {
    String value() default DataSourceConstants.DS_KEY_DB01;
}

  (2)定义切面

    注解 Pointcut 使用 annotation 指定注解,注解 Around 使用环绕通知处理,使用上下文进行对使用注解 DS 的值进行数据源切换,处理完后,恢复数据源。  

@Aspect
@Component
public class DynamicDatasourceAspect {

    @Pointcut(value = "@annotation(com.lcl.mysqldemo.annotation.DS)")
    public void pointCut(){

    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String dsKey = getDsAnnotation(joinPoint).value();
        try{
            DynamicDataSourceContextHolder.setContextKey(dsKey);
            return joinPoint.proceed();
        }finally {
            DynamicDataSourceContextHolder.removeContextKey();
        }
    }


    private DS getDsAnnotation(ProceedingJoinPoint joinPoint){
        Class<?> targetClass = joinPoint.getTarget().getClass();
        DS annotation = targetClass.getAnnotation(DS.class);
        if(Objects.nonNull(annotation)){
            return annotation;
        }else {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            return signature.getMethod().getAnnotation(DS.class);
        }
    }
}

 

   (3)使用 AOP 进行数据源切换

    在service层,定义一个 Service ,里面有三个方法,分别使用默认值和设置值从 db01 和 db02 中获取数据,使用了注解DS

@Service
public class TestService {

    @Autowired
    private OrderInfoMapper1 orderInfoMapper;
    @Autowired
    private XxlJobInfoMapper1 xxlJobInfoMapper;

    @DS
    public OrderInfo getOrderInfo(int key){
        return orderInfoMapper.selectByPrimaryKey(key);
    }

    @DS(DataSourceConstants.DS_KEY_DB01)
    public OrderInfo getOrderInfo2(int key){
        return orderInfoMapper.selectByPrimaryKey(key);
    }

    @DS(DataSourceConstants.DS_KEY_DB02)
    public XxlJobInfo getXxljobInfo(int key){
        return xxlJobInfoMapper.selectByPrimaryKey(key);
    }

}

 

posted @ 2022-06-18 16:58  李聪龙  阅读(9134)  评论(0编辑  收藏  举报