源
=数据的源头几个数据库
连接信息,自己获取配置文件中的这些配置,然后在springBoot启动的使用想办法自动创建这 几个数据源实例
;变量
进行动态创建数据源实例,然后在切换到该数据源上
org.springframework.boot
spring-boot-starter-aop
com.alibaba
druid-spring-boot-starter
1.1.6
db:
default:
#url: jdbc:mysql://localhost:3306/product_master?useSSL=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
driver-class-name: com.mysql.jdbc.Driver
url-base: jdbc:mysql://
host: localhost
port: 3306
dbname: ljyun_share
url-other: ?useSSL=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
username: common
password: common
#蓝景商城数据库
dbMall:
driver-class-name: com.mysql.jdbc.Driver
url-base: jdbc:mysql://
host: localhost
port: 3306
dbname: db_mall
url-other: ?useSSL=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
username: common
password: common
#云平台私有库
privateDB:
driver-class-name: com.mysql.jdbc.Driver
url-base: jdbc:mysql://
host: localhost
port: 3306
dbname: ljyun_{id}_merchant
url-other: ?useSSL=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
username: common
password: common
多数据源-1.jpg
DynamicDataSource
需要继承 AbstractRoutingDataSource
package domain.dbs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* 动态数据设置以及获取,本类属于单例
* @author lxf 2018-09-29
*/
@Component
public class DynamicDataSource extends AbstractRoutingDataSource {
private final Logger logger = LoggerFactory.getLogger(getClass());
//单例句柄
private static DynamicDataSource instance;
private static byte[] lock=new byte[0];
//用于存储已实例的数据源map
private static Map
DataSourceConfigurer
在tomcat启动时触发,在该类中生成多个数据源实例并将其注入到 ApplicationContext 中;@Configuration
和 @Bean
注解,将创建好的多数据源实例自动注入到 ApplicationContext
上下中,供后期切换数据库用;
package domain.dbs;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* 数据源配置类,在tomcat启动时触发,在该类中生成多个数据源实例并将其注入到 ApplicationContext 中
* @author lxf 2018-09-27
*/
@Configuration
@EnableConfigurationProperties(MybatisProperties.class)
public class DataSourceConfigurer {
//日志logger句柄
private final Logger logger = LoggerFactory.getLogger(getClass());
//自动注入环境类,用于获取配置文件的属性值
@Autowired
private Environment evn;
private MybatisProperties mybatisProperties;
public DataSourceConfigurer(MybatisProperties properties) {
this.mybatisProperties = properties;
}
/**
* 创建数据源对象
* @param dbType 数据库类型
* @return data source
*/
private DruidDataSource createDataSource(String dbType) {
//如果不指定数据库类型,则使用默认数据库连接
String dbName = dbType.trim().isEmpty() ? "default" : dbType.trim();
DruidDataSource dataSource = new DruidDataSource();
String prefix = "db." + dbName +".";
String dbUrl = evn.getProperty( prefix + "url-base")
+ evn.getProperty( prefix + "host") + ":"
+ evn.getProperty( prefix + "port") + "/"
+ evn.getProperty( prefix + "dbname") + evn.getProperty( prefix + "url-other");
logger.info("+++default默认数据库连接url = " + dbUrl);
dataSource.setUrl(dbUrl);
dataSource.setUsername(evn.getProperty( prefix + "username"));
dataSource.setPassword(evn.getProperty( prefix + "password"));
dataSource.setDriverClassName(evn.getProperty( prefix + "driver-class-name"));
return dataSource;
}
/**
* spring boot 启动后将自定义创建好的数据源对象放到TargetDataSources中用于后续的切换数据源用
* (比如:DynamicDataSourceContextHolder.setDataSourceKey("dbMall"),手动切换到dbMall数据源
* 同时指定默认数据源连接
* @return 动态数据源对象
*/
@Bean
public DynamicDataSource dynamicDataSource() {
//获取动态数据库的实例(单例方式)
DynamicDataSource dynamicDataSource = DynamicDataSource.getInstance();
//创建默认数据库连接对象
DruidDataSource defaultDataSource = createDataSource("default");
//创建db_mall数据库连接对象
DruidDataSource mallDataSource = createDataSource("dbMall");
Map map = new HashMap<>();
//自定义数据源key值,将创建好的数据源对象,赋值到targetDataSources中,用于切换数据源时指定对应key即可切换
map.put("default", defaultDataSource);
map.put("dbMall", mallDataSource);
dynamicDataSource.setTargetDataSources(map);
//设置默认数据源
dynamicDataSource.setDefaultTargetDataSource(defaultDataSource);
return dynamicDataSource;
}
/**
* 配置mybatis的sqlSession连接动态数据源
* @param dynamicDataSource
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactory sqlSessionFactory(
@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dynamicDataSource);
bean.setMapperLocations(mybatisProperties.resolveMapperLocations());
bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
bean.setConfiguration(mybatisProperties.getConfiguration());
return bean.getObject();
}
@Bean(name = "sqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory)
throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
/**
* 将动态数据源添加到事务管理器中,并生成新的bean
* @return the platform transaction manager
*/
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dynamicDataSource());
}
}
DynamicDataSourceContextHolder
类的实现
package domain.dbs;
/**
* 通过 ThreadLocal 获取和设置线程安全的数据源 key
*/
public class DynamicDataSourceContextHolder {
/**
* Maintain variable for every thread, to avoid effect other thread
*/
private static final ThreadLocal contextHolder = new ThreadLocal() {
/**
* 将 default 数据源的 key 作为默认数据源的 key
*/
// @Override
// protected String initialValue() {
// return "default";
// }
};
/**
* To switch DataSource
*
* @param key the key
*/
public static synchronized void setDataSourceKey(String key) {
contextHolder.set(key);
}
/**
* Get current DataSource
*
* @return data source key
*/
public static String getDataSourceKey() {
return contextHolder.get();
}
/**
* To set DataSource as default
*/
public static void clearDataSourceKey() {
contextHolder.remove();
}
}
package domain.dbs;
/**
* 动态数据源切换的切面,切 DAO 层,通过 DAO 层方法名判断使用哪个数据源,实现数据源切换
* 关于切面的 Order 可以可以不设,因为 @Transactional 是最低的,取决于其他切面的设置,
* 并且在 org.springframework.core.annotation.AnnotationAwareOrderComparator 会重新排序
*
* 注意:本项目因为是外部传递进来的云编号,根据动态创建数据源实例,并且进行切换,而这种只用dao层切面的方式,
* 适用于进行多个master/slave读写分类用的场景,所以我们的项目用不到这种方式(我们如果使用这种方式,
* 就需要修改daoAai入参方式,在前置处理器获取dao的方法参数,根据参数切换数据库,这样就需要修改dao接口,
* 以及对应mapper.xml,需要了解动态代理的知识,所以目前我们没有使用该方式,目前我们使用的是
* 在service或controller层手动切库)
*/
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//@Aspect
//@Component
public class DynamicDataSourceAspect {
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class);
private final String[] QUERY_PREFIX = {"select"};
@Pointcut("execution( * domain.dao.impl.*.*(..))")
public void daoAspect() {
}
@Before("daoAspect()")
public void switchDataSource(JoinPoint point) {
Object[] params = point.getArgs();
System.out.println(params.toString());
String param = (String) params[0];
for (Object string:params
) {
System.out.println(string.toString());
}
System.out.println("###################################################");
System.out.println(point.getSignature().getName());
Boolean isQueryMethod = isQueryMethod(point.getSignature().getName());
//DynamicDataSourceContextHolder.setDataSourceKey("slave");
if (isQueryMethod) {
DynamicDataSourceContextHolder.setDataSourceKey("slave");
logger.info("Switch DataSource to [{}] in Method [{}]",
DynamicDataSourceContextHolder.getDataSourceKey(), point.getSignature());
}
}
@After("daoAspect())")
public void restoreDataSource(JoinPoint point) {
DynamicDataSourceContextHolder.clearDataSourceKey();
logger.info("Restore DataSource to [{}] in Method [{}]",
DynamicDataSourceContextHolder.getDataSourceKey(), point.getSignature());
}
private Boolean isQueryMethod(String methodName) {
for (String prefix : QUERY_PREFIX) {
if (methodName.startsWith(prefix)) {
return true;
}
}
return false;
}
}
Controller
和 Service
需要切换数据库的使用,需要使用 SwitchDB.change()
方法.
package domain.dbs;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.transaction.PlatformTransactionManager;
import java.util.HashMap;
import java.util.Map;
/**
* 切换数据库类
* @author lxf 2018-09-28
*/
@Configuration
@Slf4j
public class SwitchDB {
@Autowired
private Environment evn;
//私有库数据源key
private static String ljyunDataSourceKey = "ljyun_" ;
@Autowired
DynamicDataSource dynamicDataSource;
@Autowired
private PlatformTransactionManager transactionManager;
/**
* 切换数据库对外方法,如果私有库id参数非0,则首先连接私有库,否则连接其他已存在的数据源
* @param dbName 已存在的数据库源对象
* @param ljyunId 私有库主键
* @return 返回当前数据库连接对象对应的key
*/
public String change(String dbName,int ljyunId)
{
if( ljyunId == 0){
toDB(dbName);
}else {
toYunDB(ljyunId);
}
//获取当前连接的数据源对象的key
String currentKey = DynamicDataSourceContextHolder.getDataSourceKey();
log.info("=====当前连接的数据库是:" + currentKey);
return currentKey;
}
/**
* 切换已存在的数据源
* @param dbName
*/
private void toDB(String dbName)
{
//如果不指定数据库,则直接连接默认数据库
String dbSourceKey = dbName.trim().isEmpty() ? "default" : dbName.trim();
//获取当前连接的数据源对象的key
String currentKey = DynamicDataSourceContextHolder.getDataSourceKey();
//如果当前数据库连接已经是想要的连接,则直接返回
if(currentKey == dbSourceKey) return;
//判断储存动态数据源实例的map中key值是否存在
if( DynamicDataSource.isExistDataSource(dbSourceKey) ){
DynamicDataSourceContextHolder.setDataSourceKey(dbSourceKey);
log.info("=====普通库: "+dbName+",切换完毕");
}else {
log.info("切换普通数据库时,数据源key=" + dbName + "不存在");
}
}
/**
* 创建新的私有库数据源
* @param ljyunId
*/
private void toYunDB(int ljyunId){
//组合私有库数据源对象key
String dbSourceKey = ljyunDataSourceKey+String.valueOf(ljyunId);
//获取当前连接的数据源对象的key
String currentKey = DynamicDataSourceContextHolder.getDataSourceKey();
if(dbSourceKey == currentKey) return;
//创建私有库数据源
createLjyunDataSource(ljyunId);
//切换到当前数据源
DynamicDataSourceContextHolder.setDataSourceKey(dbSourceKey);
log.info("=====私有库: "+ljyunId+",切换完毕");
}
/**
* 创建私有库数据源,并将数据源赋值到targetDataSources中,供后切库用
* @param ljyunId
* @return
*/
private DruidDataSource createLjyunDataSource(int ljyunId){
//创建新的数据源
if(ljyunId == 0)
{
log.info("动态创建私有库数据时,私有库主键丢失");
}
String yunId = String.valueOf(ljyunId);
DruidDataSource dataSource = new DruidDataSource();
String prefix = "db.privateDB.";
String dbUrl = evn.getProperty( prefix + "url-base")
+ evn.getProperty( prefix + "host") + ":"
+ evn.getProperty( prefix + "port") + "/"
+ evn.getProperty( prefix + "dbname").replace("{id}",yunId) + evn.getProperty( prefix + "url-other");
log.info("+++创建云平台私有库连接url = " + dbUrl);
dataSource.setUrl(dbUrl);
dataSource.setUsername(evn.getProperty( prefix + "username"));
dataSource.setPassword(evn.getProperty( prefix + "password"));
dataSource.setDriverClassName(evn.getProperty( prefix + "driver-class-name"));
//将创建的数据源,新增到targetDataSources中
Map map = new HashMap<>();
map.put(ljyunDataSourceKey+yunId, dataSource);
DynamicDataSource.getInstance().setTargetDataSources(map);
return dataSource;
}
}
SwitchDB.change()
TestTransaction
实现
package domain.service.impl.exhibition;
import domain.dao.impl.ExhibitionDao;
import domain.dbs.DynamicDataSource;
import domain.dbs.DynamicDataSourceContextHolder;
import domain.dbs.SwitchDB;
import domain.domain.DomainResponse;
import domain.domain.Exhibition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.HashMap;
import java.util.Map;
/**
* 测试切库后的事务类
* @author lxf 2018-09-28
*/
@Service
@Slf4j
public class TestTransaction {
@Autowired
private ExhibitionDao dao;
@Autowired
private SwitchDB switchDB;
@Autowired
DynamicDataSource dynamicDataSource;
public DomainResponse testProcess(int kaiguan, int ljyunId, String dbName){
switchDB.change(dbName,ljyunId);
//获取当前已有的数据源实例
System.out.println("%%%%%%%%"+dynamicDataSource.getDataSourceMap());
return process(kaiguan,ljyunId,dbName);
}
/**
* 事务测试
* 注意:(1)有@Transactional注解的方法,方法内部不可以做切换数据库操作
* (2)在同一个service其他方法调用带@Transactional的方法,事务不起作用,(比如:在本类中使用testProcess调用process())
* 可以用其他service中调用带@Transactional注解的方法,或在controller中调用.
* @param kaiguan
* @param ljyunId
* @param dbName
* @return
*/
//propagation 传播行为 isolation 隔离级别 rollbackFor 回滚规则
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public DomainResponse process(int kaiguan, int ljyunId, String dbName ) {
String currentKey = DynamicDataSourceContextHolder.getDataSourceKey();
log.info("=====service当前连接的数据库是:" + currentKey);
Exhibition exhibition = new Exhibition();
exhibition.setExhibitionName("A-001-003");
//return new DomainResponse(1, "新增成功", "");
int addRes = dao.insert(exhibition);
if(addRes>0 && kaiguan==1){
exhibition.setExhibitionName("B-001-002");
int addRes2 = dao.insert(exhibition);
return new DomainResponse(1, "新增成功", "");
}else
{
Map map = new HashMap<>();
String a = map.get("hello");
//log.info("-----a="+a.replace("a","b"));
//手动回滚事务
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return new DomainResponse(0, "新增错误,事务已回滚", "");
}
}
}
需要在 DataSourceConfigurer
类中添加如下配置,让事务管理器与动态数据源对应起来;
/**
* 将动态数据源添加到事务管理器中,并生成新的bean
* @return the platform transaction manager
*/
@Bean
public PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dynamicDataSource());
}
```
转载:
链接:https://www.jianshu.com/p/7f1b785cd986