写了多年业务代码,了解抽象的业务概念,可到头来最基本的技术代码都要看好久才能理解,心里蛮不是滋味(造孽啊~),没办法只能一点一点重新捡起来,记录一下简单的多数据源架构。
一、通过继承AbstractRoutingDataSource 类,重写determineCurrentLookupKey方法,实现数据源的动态切换。该方法会在执行SQL语句前执行,届时我们通过替换数据源名称的方式通知mybatis切换数据源。
package com.aikes.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final Logger log = LoggerFactory.getLogger(DynamicDataSource.class);
@Override
protected Object determineCurrentLookupKey() {
log.debug("当前数据源为:{}", DataSourceContextHolder.getDB());
return DataSourceContextHolder.getDB();
}
}
二、通过下方类代码,保证线程安全的情况下实现数据源名称的修改、获取、清除等操作。
package com.aikes.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataSourceContextHolder {
public static final Logger log = LoggerFactory.getLogger(DataSourceContextHolder.class);
/**
* 默认数据源
*/
public static final String DEFAULT_DS = "MASTER";
public static final String TEST_DS = "TEST";
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
// 设置数据源名
public static void setDB(String dbType) {
log.debug("切换数据源为:{}", dbType);
contextHolder.set(dbType);
}
// 获取数据源名
public static String getDB() {
return contextHolder.get() == null ? DEFAULT_DS : contextHolder.get();
}
// 清除数据源名
public static void clearDB() {
contextHolder.remove();
}
}
三、通过下方代码实现数据库信息的读取,并生成dataSource对象,装载sqlSessionFactory对象
package com.aikes.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DataSourceConfig {
/**
* 读取数据库配置
*/
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 读取数据库配置
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource.test")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 装载多数据源
*/
@Bean
public DynamicDataSource getDynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 默认数据源
dynamicDataSource.setDefaultTargetDataSource(this.masterDataSource());
// 配置多数据源
Map<Object, Object> dsMap = new HashMap<>(5);
dsMap.put(DataSourceContextHolder.DEFAULT_DS, this.masterDataSource());
dsMap.put(DataSourceContextHolder.TEST_DS, this.testDataSource());
dynamicDataSource.setTargetDataSources(dsMap);
return dynamicDataSource;
}
/**
* 将多数据源装载到mybatis中
*/
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(this.getDynamicDataSource());
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/com/aikes/**/*.xml"));
return sqlSessionFactoryBean.getObject();
}
/**
* 配置mybatis事务管理
*/
@Bean
public PlatformTransactionManager platformTransactionManager(){
return new DataSourceTransactionManager(this.getDynamicDataSource());
}
}
配置文件:
mybatis.mapper-locations=classpath:com/aikes/**/*.xml
server.port=8888
server.servlet.context-path=/mini
spring.datasource.username=1
spring.datasource.password=1
spring.datasource.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:21521:orcl
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.test.username=2
spring.datasource.test.password=2
spring.datasource.test.jdbcUrl=jdbc:oracle:thin:@127.0.0.1:61521:orcl
spring.datasource.test.driver-class-name=oracle.jdbc.OracleDriver
logging.config=classpath:logback.xml
logging.level.root=DEBUG
logging.level.org.mybatis.spring=ERROR
logging.level.org.springframework=ERROR
logging.level.org.apache.ibatis=ERROR
logging.level.io.netty=ERROR
logging.level.org.quartz=ERROR
logging.level.org.thymeleaf=ERROR
logging.level.com.zaxxer.hikari=ERROR
四、创建注解、Aop处理类,实现动态切换数据源
package com.aikes.config;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.METHOD
})
public @interface DS {
String value() default DataSourceContextHolder.DEFAULT_DS;
}
package com.aikes.config;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class DataSourceAop {
private static final Logger log = LoggerFactory.getLogger(DataSourceAop.class);
@Before("@annotation(com.aikes.config.DS)")
public void beforeSwitchDS(JoinPoint point) {
//获得当前访问的class
Class<?> className = point.getTarget().getClass();
//获得访问的方法名
String methodName = point.getSignature().getName();
//得到方法的参数的类型
Class[] argClass = ((MethodSignature) point.getSignature()).getParameterTypes();
String dataSource = DataSourceContextHolder.DEFAULT_DS;
try {
// 得到访问的方法对象
Method method = className.getMethod(methodName, argClass);
// 判断是否存在@DS注解
if (method.isAnnotationPresent(DS.class)) {
DS annotation = method.getAnnotation(DS.class);
// 取出注解中的数据源名
dataSource = annotation.value();
}
} catch (Exception e) {
log.info("多数据源切换失败:"+e.getMessage());
}
// 切换数据源
DataSourceContextHolder.setDB(dataSource);
}
@After("@annotation(com.aikes.config.DS)")
public void afterSwitchDS(JoinPoint point) {
DataSourceContextHolder.clearDB();
}
}
五、具体使用Demo
package com.aikes.controller;
import com.aikes.config.DS;
import com.aikes.config.DataSourceContextHolder;
import com.aikes.dao.AikesDao;
import com.aikes.vo.AikesPo;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/aikes")
@Slf4j
public class AikesController {
@Autowired
private AikesDao mAikesDao;
@DS
@PostMapping("/testMaster")
public String testAikes(@RequestBody AikesPo cAikesPo) {
log.info(JSON.toJSONString(cAikesPo));
String tResult = JSON.toJSONString(mAikesDao.getAikesInfo(cAikesPo));
log.info(tResult);
return tResult;
}
@DS(DataSourceContextHolder.TEST_DS)
@PostMapping("/testOther")
public String testOther(@RequestBody AikesPo cAikesPo) {
log.info(JSON.toJSONString(cAikesPo));
String tResult = JSON.toJSONString(mAikesDao.getAikesInfo(cAikesPo));
log.info(tResult);
return tResult;
}
}
分别调用两个接口,配置的不同数据库,使用同一个查询SQL,至此多数据源已生效。
六、注意事项
1、配置文件中的mapper文件扫描路径此时有可能不生效,在DataSourceConfig.java中会被新的路径配置覆盖,所以大家调整mapper.xml路径时需要把这里同步修改。