当前位置:   article > 正文

springboot -多数据源管理方案_springboot多数据源

springboot多数据源

多数据源的配置有多种方式

方式一 、依赖dataSource的配置

1.建立多数据源配置

spring:
  # 数据源配置
  datasource:
      pdm:
        driver-class-name: oracle.jdbc.driver.OracleDriver
        jdbc-url: jdbc:oracle:thin:@10.216.xxx.xxx:3000:orcl
        username: cfpdm
        password: cape
      capp:
        driver-class-name: oracle.jdbc.driver.OracleDriver
        jdbc-url: jdbc:oracle:thin:@42.193.xx1.xx1:1521:MDMS
        username: capp
        password: cape
      cpsmds:
        driver-class-name: oracle.jdbc.driver.OracleDriver
        jdbc-url: jdbc:oracle:thin:@42.193.xx2.xx2:1521:MDMS
        username: cpsmds
        password: cape

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  1. 将数据源datasource注入到PlatformTransactionManager 事务管理器中,并注入数据源至AbstractRoutingDataSource 多数据源路由bean中
@Configuration
public class DateSourceConfig {
    @Bean("pdmDataSource")
    @ConfigurationProperties("spring.datasource.pdm")
    public DataSource masterDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("cappDataSource")
    @ConfigurationProperties("spring.datasource.capp")
    public DataSource cappDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("cpsmdsDataSource")
    @ConfigurationProperties("spring.datasource.cpsmds")
    public DataSource cpsmdsDataSource(){
        return DataSourceBuilder.create().build();
    }


    @Bean("pdm")
    public PlatformTransactionManager pdmTransactionManager(@Qualifier("pdmDataSource")DataSource pdmDataSource) {
        return new DataSourceTransactionManager(pdmDataSource);
    }

    @Bean("capp")
    public PlatformTransactionManager cappTransactionManager(@Qualifier("cappDataSource")DataSource cappDataSource) {
        return new DataSourceTransactionManager(cappDataSource);
    }

    @Bean("cpsmds")
    public PlatformTransactionManager cpsmdsTransactionManager(@Qualifier("cpsmdsDataSource")DataSource cpsmdsDataSource) {
        return new DataSourceTransactionManager(cpsmdsDataSource);
    }


    @Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource createDynamicDataSource(){
        Map<Object,Object> dataSourceMap = new HashMap<>();
        DataSource defaultDataSource = masterDataSource();
        dataSourceMap.put("pdm",defaultDataSource);
        dataSourceMap.put("capp",cappDataSource());
        dataSourceMap.put("cpsmds",cpsmdsDataSource());
        return new DynamicDataSource(defaultDataSource,dataSourceMap);
    }
}
  • 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

3.多数据源路由AbstractRoutingDataSource 实现类

package avicit.pdm.config;


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

import javax.sql.DataSource;
import java.util.Map;

public class DynamicDataSource  extends AbstractRoutingDataSource {
    public DynamicDataSource(DataSource defaultDataSource, Map<Object, Object> targetDataSources){
        super.setDefaultTargetDataSource(defaultDataSource);
        super.setTargetDataSources(targetDataSources);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDataSource();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4.建立多数据源切换切面
根据调用的dao层包名,确定使用的多数据数是哪一个,通过DataSourceContextHolder 注入制定的数据源


@Aspect
@Component
@Slf4j
public class VpdmDSAspect {



   @Pointcut("execution(* avicit..*.dao.*.*(..))")
    public void dynamicDataSource(){}

    @Around("dynamicDataSource()")
    public Object datasourceAround(ProceedingJoinPoint point) throws Throwable {
      String name =  point.getSignature().getDeclaringType().getName();
      if(name.contains("avicit.pdm")){
          DataSourceContextHolder.setDataSource("pdm");
      }else{
          MethodSignature signature = (MethodSignature)point.getSignature();
          Method method = signature.getMethod();
          DS ds = method.getAnnotation(DS.class);
          if (Objects.nonNull(ds)){
              DataSourceContextHolder.setDataSource(ds.value());
          }else{
              DataSourceContextHolder.setDataSource("capp");
          }
      }
        try {
            return point.proceed();
        } finally {
            DataSourceContextHolder.removeDataSource();
        }
    }
}

  • 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
  1. 多数据源事务配置
    项目中遇到Transactional 事务时候需要在事务中指定value中指定具体的数据源

在这里插入图片描述

  1. MapperScan扫描加载包下的dao和SpringBootApplication扫描制定包的bean
@MapperScan("QAZWSC.**.dao")
@SpringBootApplication(scanBasePackages = {"QAZWSC"})
@EnableTransactionManagement
@EnableKnife4j
public class XxxxApplication {

    public static void main(String[] args) {
        SpringApplication.run(XxxxApplication .class, args);
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

方式二、通过mybatis Plus的多数据源管理方案

通过mybatis的@DS 注解切换 数据源

  1. 指定主数据源,通过primary 指定默认的主数据源
spring:
  datasource:
    dynamic:
      primary: capp
      datasource:
        capp:
          driver-class-name: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@42.193.xx1.xx1:1521:MDMS
          username: capp
          password: cape
        cpsmds:
          driver-class-name: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@42.193.xx1.xx1:1521:MDMS
          username: cpsmds
          password: cape
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.在dao层制定要使用的数据源

@DS("cpsmds")
public interface MdsProductDAO {

}
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/929622
推荐阅读
相关标签
  

闽ICP备14008679号