多数据源的配置有多种方式
方式一 、依赖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
- 将数据源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);
}
}
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();
}
}
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();
}
}
}
- 多数据源事务配置
项目中遇到Transactional 事务时候需要在事务中指定value中指定具体的数据源
- 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);
}
}
方式二、通过mybatis Plus的多数据源管理方案
通过mybatis的@DS 注解切换 数据源
- 指定主数据源,通过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
2.在dao层制定要使用的数据源
@DS("cpsmds")
public interface MdsProductDAO {
}