赞
踩
多数据源的配置有多种方式
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
@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(); } } }
@MapperScan("QAZWSC.**.dao")
@SpringBootApplication(scanBasePackages = {"QAZWSC"})
@EnableTransactionManagement
@EnableKnife4j
public class XxxxApplication {
public static void main(String[] args) {
SpringApplication.run(XxxxApplication .class, args);
}
}
通过mybatis的@DS 注解切换 数据源
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 {
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。