use of org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource in project chao-cloud by chaojunzi.
the class ShardingActualNodesComplete method sourceOfTableName.
/**
* 根据表名生成表结构<br>
* 格式: table:ds.table_202001
*
* @param shardingDataSource 数据源
* @param tableNodes 源表_节点
* @param defaultDsName 默认数据源
*/
default void sourceOfTableName(ShardingDataSource shardingDataSource, Map<String, List<String>> tableNodes, String defaultDsName) {
DbType type = DbType.getDbType(shardingDataSource.getDatabaseType().getName());
switch(type) {
case // 目前只支持mysql
MYSQL:
Map<String, DataSource> dsMap = shardingDataSource.getDataSourceMap();
// 节点分组
Map<String, Map<String, List<String>>> dsTableNodes = build(tableNodes);
// 生成表结构
dsTableNodes.forEach((dsName, tNodesMap) -> {
DataSource ds = dsMap.get(dsName);
List<String> tables = MetaUtil.getTables(ds);
// 生成数据表
tNodesMap.forEach((sourceTable, nodes) -> {
// 整合需要缓存表节点
cacheTableNodes(dsName, sourceTable, nodes, tables);
// 需要生成的表
Collection<String> needTables = CollUtil.subtract(nodes, tables);
// 生成表节点
if (CollUtil.isEmpty(needTables)) {
return;
}
String sourceTableDDL = getSourceTableDDL(dsMap.get(defaultDsName), sourceTable);
createTable(sourceTable, sourceTableDDL, dsName, ds, needTables);
});
});
default:
break;
}
}
use of org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource in project chao-cloud by chaojunzi.
the class ShardingActualNodesComplete method refreshDatasource.
/**
* 动态刷新数据源
*
* @param dsName 数据源名称
* @param tableName 逻辑表
* @param nodes 真实表
*/
/**
* 动态刷新数据源
*/
static void refreshDatasource(String dsName, String tableName, Collection<String> nodes) {
ShardingDataSource shardingDataSource = SpringUtil.getBean(ShardingDataSource.class);
TableRule tableRule = shardingDataSource.getRuntimeContext().getRule().getTableRule(tableName);
// 1.动态刷新:actualDataNodes
List<DataNode> actualDataNodes = tableRule.getActualDataNodes();
nodes.forEach(n -> {
DataNode node = new DataNode(dsName, n);
if (!CollUtil.contains(actualDataNodes, node)) {
actualDataNodes.add(node);
}
});
//
Set<String> actualTables = Sets.newHashSet();
Map<DataNode, Integer> dataNodeIndexMap = Maps.newHashMap();
// 修改节点索引
CollUtil.forEach(actualDataNodes, (n, i) -> {
actualTables.add(n.getTableName());
dataNodeIndexMap.put(n, i);
});
EntityUtil.setProperty(tableRule, TableRule::getActualDataNodes, actualDataNodes);
// 2.动态刷新:actualTables
EntityUtil.setProperty(tableRule, new TypeReference<Set<String>>() {
}, actualTables);
// 3.动态刷新:dataNodeIndexMap
EntityUtil.setProperty(tableRule, new TypeReference<Map<DataNode, Integer>>() {
}, dataNodeIndexMap);
// 4.动态刷新:datasourceToTablesMap
Map<String, Collection<String>> datasourceToTablesMap = EntityUtil.getProperty(tableRule, new TypeReference<Map<String, Collection<String>>>() {
});
if (datasourceToTablesMap.containsKey(dsName)) {
Collection<String> tables = datasourceToTablesMap.get(dsName);
tables.addAll(nodes);
} else {
datasourceToTablesMap.put(dsName, actualTables);
}
}
Aggregations