1 | initial version |
In ShardingSphere-JDBC, sub database rules can be dynamically loaded using the following steps:
Create a new XML configuration file for the sub database rules. The file should contain the necessary database configuration details and the sub database rules.
Use the ShardingSphere-JDBC API to load the new XML configuration file:
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTables().add(new TableRuleConfiguration("table_name", "ds_name${0..1}.table_name_${0..1}"));
shardingRuleConfig.getBindingTableGroups().add("table_name");
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("id", new ModuloShardingDatabaseAlgorithm()));
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("id", new ModuloShardingTableAlgorithm()));
shardingRuleConfig.getShardingRuleExtensions().put("key", "value");
shardingRuleConfig.getBroadcastTables().add("table_name");
DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(shardingRuleConfig), new Properties());
Replace the existing ShardingSphere-JDBC configuration file with the newly created configuration file.
Restart the application to apply the changes.