|
@@ -7,7 +7,7 @@ import com.xin.shardingspherejdbcdemo.utils.JdbcUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.compress.utils.Sets;
|
|
|
import org.apache.ibatis.javassist.Modifier;
|
|
|
-import org.apache.shardingsphere.core.config.ShardingConfigurationException;
|
|
|
+
|
|
|
import org.apache.shardingsphere.core.rule.DataNode;
|
|
|
import org.apache.shardingsphere.core.rule.ShardingRule;
|
|
|
import org.apache.shardingsphere.core.rule.TableRule;
|
|
@@ -103,12 +103,10 @@ public class JdbcServiceImpl {
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (NoSuchFieldException | IllegalAccessException | SQLException e) {
|
|
|
log.error(String.format("逻辑表:%s 动态分表配置错误!"));
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
@@ -120,7 +118,7 @@ public class JdbcServiceImpl {
|
|
|
|
|
|
Map<String, Collection<String>> datasourceToTablesMap = Maps.newHashMap();
|
|
|
|
|
|
- List<DataNode> newDataNodesAll=new ArrayList<>();
|
|
|
+ List<DataNode> newDataNodesAll = new ArrayList<>();
|
|
|
|
|
|
Set<String> actualTablesAll = Sets.newHashSet();
|
|
|
|
|
@@ -161,47 +159,6 @@ public class JdbcServiceImpl {
|
|
|
Field datasourceToTablesMapField = TableRule.class.getDeclaredField("datasourceToTablesMap");
|
|
|
datasourceToTablesMapField.setAccessible(true);
|
|
|
datasourceToTablesMapField.set(tableRule, datasourceToTablesMap);
|
|
|
- System.out.println(tableRule);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 动态刷新数据源
|
|
|
- */
|
|
|
- private void dynamicRefreshDatasource(String dataSourceName, TableRule tableRule, List<DataNode> newDataNodes)
|
|
|
- throws NoSuchFieldException, IllegalAccessException {
|
|
|
- Set<String> actualTables = Sets.newHashSet();
|
|
|
- Map<DataNode, Integer> dataNodeIndexMap = Maps.newHashMap();
|
|
|
- AtomicInteger index = new AtomicInteger(0);
|
|
|
- newDataNodes.forEach(dataNode -> {
|
|
|
- actualTables.add(dataNode.getTableName());
|
|
|
- if (index.intValue() == 0) {
|
|
|
- dataNodeIndexMap.put(dataNode, 0);
|
|
|
- } else {
|
|
|
- dataNodeIndexMap.put(dataNode, index.intValue());
|
|
|
- }
|
|
|
- index.incrementAndGet();
|
|
|
- });
|
|
|
-
|
|
|
- Field actualDataNodesField = TableRule.class.getDeclaredField("actualDataNodes");
|
|
|
- Field modifiersField = Field.class.getDeclaredField("modifiers");
|
|
|
- modifiersField.setAccessible(true);
|
|
|
- modifiersField.setInt(actualDataNodesField, actualDataNodesField.getModifiers() & ~Modifier.FINAL);
|
|
|
- actualDataNodesField.setAccessible(true);
|
|
|
- actualDataNodesField.set(tableRule, newDataNodes);
|
|
|
-
|
|
|
- Field actualTablesField = TableRule.class.getDeclaredField("actualTables");
|
|
|
- actualTablesField.setAccessible(true);
|
|
|
- actualTablesField.set(tableRule, actualTables);
|
|
|
-
|
|
|
- Field dataNodeIndexMapField = TableRule.class.getDeclaredField("dataNodeIndexMap");
|
|
|
- dataNodeIndexMapField.setAccessible(true);
|
|
|
- dataNodeIndexMapField.set(tableRule, dataNodeIndexMap);
|
|
|
-
|
|
|
- Map<String, Collection<String>> datasourceToTablesMap = Maps.newHashMap();
|
|
|
- datasourceToTablesMap.put(dataSourceName, actualTables);
|
|
|
- Field datasourceToTablesMapField = TableRule.class.getDeclaredField("datasourceToTablesMap");
|
|
|
- datasourceToTablesMapField.setAccessible(true);
|
|
|
- datasourceToTablesMapField.set(tableRule, datasourceToTablesMap);
|
|
|
}
|
|
|
|
|
|
}
|