|
@@ -5,7 +5,6 @@ import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
|
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
|
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
|
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
|
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
-import com.persagy.apm.common.context.AppContext;
|
|
|
|
import com.persagy.apm.common.context.poems.PoemsContext;
|
|
import com.persagy.apm.common.context.poems.PoemsContext;
|
|
import com.persagy.apm.common.helper.SpringHelper;
|
|
import com.persagy.apm.common.helper.SpringHelper;
|
|
import com.persagy.apm.mybatis.service.IDbService;
|
|
import com.persagy.apm.mybatis.service.IDbService;
|
|
@@ -21,13 +20,16 @@ import javax.sql.DataSource;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 动态数据源拦截器
|
|
* 动态数据源拦截器
|
|
|
|
+ *
|
|
* @author Charlie Yu
|
|
* @author Charlie Yu
|
|
* @date 2021-03-29
|
|
* @date 2021-03-29
|
|
*/
|
|
*/
|
|
@Component
|
|
@Component
|
|
public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
|
|
public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
|
|
|
|
|
|
- /** 忽略的url - swagger的文档不校验 */
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 忽略的url - swagger的文档不校验
|
|
|
|
+ */
|
|
private static final String[] IGNORE_URL = {".html", ".js", ".css", "/swagger-resources"};
|
|
private static final String[] IGNORE_URL = {".html", ".js", ".css", "/swagger-resources"};
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
@@ -55,20 +57,21 @@ public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
|
|
public void resetDataSource() {
|
|
public void resetDataSource() {
|
|
// 是否启用多数据源
|
|
// 是否启用多数据源
|
|
boolean isDynamic = SpringHelper.getBoolean("spring.datasource.dynamic.enabled", false);
|
|
boolean isDynamic = SpringHelper.getBoolean("spring.datasource.dynamic.enabled", false);
|
|
- if(!isDynamic) {
|
|
|
|
|
|
+ if (!isDynamic) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
// 默认为应用名
|
|
// 默认为应用名
|
|
String dbNameDefault = SpringHelper.getString("spring.application.name");
|
|
String dbNameDefault = SpringHelper.getString("spring.application.name");
|
|
// 将应用名中的中划线替换为下划线,按照公司要求应用名统一用中划线,数据库名统一用下划线
|
|
// 将应用名中的中划线替换为下划线,按照公司要求应用名统一用中划线,数据库名统一用下划线
|
|
- String newDsName = dbNameDefault.replace("-", "_");
|
|
|
|
|
|
+ dbNameDefault = dbNameDefault.replace("-", "_");
|
|
|
|
+ String newDsName = dbNameDefault;
|
|
if (StringUtils.isNotBlank(PoemsContext.getContext().getGroupCode())) {
|
|
if (StringUtils.isNotBlank(PoemsContext.getContext().getGroupCode())) {
|
|
// 创建的数据源名称: 集团编码_应用名
|
|
// 创建的数据源名称: 集团编码_应用名
|
|
- newDsName = PoemsContext.getContext().getGroupCode() + "_" + dbNameDefault;
|
|
|
|
|
|
+ newDsName = PoemsContext.getContext().getGroupCode() + "_" + newDsName;
|
|
}
|
|
}
|
|
|
|
|
|
// 数据源中是否已存在
|
|
// 数据源中是否已存在
|
|
- if(!dataSource.getCurrentDataSources().containsKey(newDsName)) {
|
|
|
|
|
|
+ if (!dataSource.getCurrentDataSources().containsKey(newDsName)) {
|
|
// 设置新数据源
|
|
// 设置新数据源
|
|
DataSourceProperty property = copyProperty(dbNameDefault, newDsName);
|
|
DataSourceProperty property = copyProperty(dbNameDefault, newDsName);
|
|
// 创建数据库实例
|
|
// 创建数据库实例
|
|
@@ -83,8 +86,9 @@ public class DynamicDataSourceHandler extends HandlerInterceptorAdapter {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 复制新的数据源
|
|
* 复制新的数据源
|
|
|
|
+ *
|
|
* @param dbNameDefault 默认连接的实例名
|
|
* @param dbNameDefault 默认连接的实例名
|
|
- * @param newDsName 新创建的实例名
|
|
|
|
|
|
+ * @param newDsName 新创建的实例名
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
private DataSourceProperty copyProperty(String dbNameDefault, String newDsName) {
|
|
private DataSourceProperty copyProperty(String dbNameDefault, String newDsName) {
|